首页 > IT职业 > IT图书 > vue 6种通信总结
6个分支,不懂的切换分支,可以直接在src文件替换vue-props文件夹里面文件
下载文件记得先安装依赖,npm install
然后在启动 npm run dev
组件间的父子之间的传值
组件间的子父之间的传值
非组件间的组件间的传值(简称:EventBus)
sessionStorage缓存传值
路由带参数跳转进行传值
vuex进行传值

image.png

image.png

image.png

image.png
我先举个例子来解释下组件间的关联
首先在src目录下创建一个eventBus.js文件,复制这行代码进去
import Vue from 'vue'let connect = new Vue() // 实例export default connect

image.png

image.png


image.png

image.png

image.png

image.png

src下创建一个store的文件夹,里面创建index.js文件
import Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex)export default new Vuex.Store({ state: { city: "上海"
}, actions: {
handleCity(ctx, city) { // console.log(ctx, city)
ctx.commit("handleCity", city)
}
},mutations: {
handleCity(state, city) {
state.city = city
}
}
})
methods: {
handleCityClick(city) {
this.$store.dispatch("handleCity", "北京");
},
展示页面
{{this.$store.state.city}}
作者:艾尔丶Lawliet
链接:https://www.jianshu.com/p/eaa3a3b577a3
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/818/viewspace-2813623/ ,如需转载,请注明出处,否则将追究法律责任。