Pass Vue js search filter functionality through single file components with EventBus

前端 未结 2 1751
傲寒
傲寒 2021-01-26 13:14

I have the following components:

/components/SearchBlogs.vue Search component to filter on blog.title and blog.description.

相关标签:
2条回答
  • 2021-01-26 13:41

    Couple of issues but essentially the computed prop filteredData will look like:

    computed: {
        filteredData() {
          return this.experiences.filter(
            el => el.category.indexOf(this.search) > -1
          );
        }
    }
    

    Also, used quotes around 'this.search' when passing its value back which made it a string.

    Fixed sandbox

    https://codesandbox.io/s/reverent-lamarr-is8jz

    0 讨论(0)
  • 2021-01-26 13:42

    Look at my solution condesandbox

    Here is an explanation: You don't need to use EventBus. You can communicate with Search Component by v-model, using prop value and emiting updated value from the Input.

    Then your Main (List) Component is responsible for all the logic.

    1. It keeps the state of a Search
    2. It keeps the items and filtered Items

    Thanks to that your Search Component is very clear and has no data, that means it has very little responsibility.

    Please ask questions if I can add something to help you understand

    0 讨论(0)
提交回复
热议问题