更新源代码
6种方法对应6个分支,不懂的切换分支,可以直接在src替换vue-props文件夹里面文件
下载文件记得先安装依赖,npm install
然后在启动 npm run dev
- 组件间的父子之间的传值
- 组件间的子父之间的传值
- 非组件间的组件间的传值(简称:EventBus)
- sessionStorage缓存传值
- 路由带参数跳转进行传值
- vuex进行传值
### 3.非组件间的组件间的传值(简称:EventBus) 我先举个例子来解释下组件间的关联 #####组件A比作一个路由,通过中继器(eventBus.js)让其他房子接收到信号(组件B)
首先在src目录下创建一个eventBus.js文件,复制这行代码进去
import Vue from 'vue'
let connect = new Vue() // 实例
export default connect
###组件A
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", "北京");
},
展示页面
<h1>{{this.$store.state.city}}</h1>