javascript - How can one component in vue better listen to the events of another component?
習慣沉默
習慣沉默 2017-06-14 10:53:44
0
1
661

There are 4 routing components A, B, C, and D under the parent component APP. There are A1, B1, C1, and D1 subcomponents under these 4 routing components. There is also an E component under the app. When these components A1, B1, C1, and D1 are clicked, the methods in the E component are triggered and the corresponding parameters are passed to the methods in the E component. Now my method is to use

this.$emit一层一层往上传递到父组件然后使用this.$refs 这样的方法激活E组件的方法和传递数据.
后来觉得这样麻烦就使用vuex, 把要传递的数据都放进store里面,但是不知道怎么使用vuex让E组件的方法在我点击后自动触发
習慣沉默
習慣沉默

reply all(1)
学习ing

You can set the value of E component computed store, and then watch this value, and then trigger watch's handler according to the value of store. If you need to trigger it every time you click on the same component, you need to trigger it every time Every click changes a value, and computed should be cached.
A better way would be to write an action as vuex, and then import this action in each component, and you can use it.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template