-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vue跨组件通信的几种方法 #20
Comments
This was referenced Jul 1, 2016
正好学习的时候看到,言简意赅,非常感谢~ |
mark |
🐶 🐶 🐶 🐶 🐶 |
mark |
我用你的vuex,报错,我也不知道为什么 |
@15831929073 文章是1.0版本的 |
66666 |
mark |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
在开发组件的时候,一定会遇到组件的通信,比如点击一个图标出现弹窗和蒙层,这三个分别是不同的组件。管理他们之间的状态就成了问题。
props双向绑定
官方文档在这,通过
sync
双向绑定,属性变化会同步到所有组件,这也是最简单的实现方式,缺点是属性会比较多。实现方式如下App.vue
文件component/dialog/index.vue
文件component/dialog-icon/index.vue
文件component/mask/index.vue
文件自定义事件
官方文档在这,子组件
$dispatch()
派发事件传递给父组件,父组件$broadcast()
广播事件传递给子组件,这种方式虽然减少了props的使用,但是需要额外定义几个事件,状态多了就会变得很复杂,实现方法如下App.vue
文件component/dialog-icon/index.vue
文件component/dialog/index.vue
文件component/mask/index.vue
文件Vuex
官方文档在这里,状态统一放store管理,修改状态通过mutations,组件通过action调用mutations,虽然有点绕,但是所有东西放一起后期会更好维护,实现方法如下
App.vue
文件component/dialog/index.vue
文件component/dialog-icon/index.vue
文件component/mask/index.vue
文件vuex/store.js
文件vuex/mutations.js
文件vuex/mutations-types.js
文件vuex/action.js
文件The text was updated successfully, but these errors were encountered: