bus的使用方法
1、在js文件中定义一个中央事件总线BUS,并暴露出来。
import Vue from 'vue'
const Bus = new Vue()
export default Bus
2、在文件A(传递数据)和文件B(接收数据)中引入bus。
import Bus from '@/js/bus';
3、在文件A中放入数据,在文件B中取出数据。
beforeDestroy() {
//如果添加成功,则跳转到成功页面,并将添加的用户ID传过去
//此处传刚注册用户返回的用户ID
Bus.$emit('staffInfo', this.terminalInfo);
console.log('A beforedestroy 销毁前状态================》');
},
created() {
Bus.$on('staffInfo', (msg) => {
console.log('successTerminal页面接收-', msg);
if(msg.role == 'terminal') {
this.info.terminalRole = '哑终端';
this.info.terminalType = msg.terminalType;
this.info.mac = msg.newMac;
this.info.office = msg.selectOne + msg.selectTwo + msg.selectThree + msg.selectFour;
this.info.workingPlace = msg.workingPlace;
this.info.introducer = msg.introducer;
this.info.valideTime = '永久';
this.info.registerTime = msg.registerTime;
}
})
console.log('%c%s',
'background: yellow;',
' B created 创建完成状态===============》');
},
4、注意事项:文件A中 e m i t 应 该 写 在 b e f o r e D e s t r o y 钩 子 函 数 中 。 文 件 B 中 的 emit应该写在beforeDestroy钩子函数中。文件B中的 emit应该写在beforeDestroy钩子函数中。文件B中的on应该写在creat