兄弟组件传参
借父组件转发
栗子:A组件传值给B组件
<template>
<div>
<A @on-click="getFalg"></A>
<B :flag="Flag"></B>
</div>
</template>
<script setup lang='ts'>
import A from './components/A.vue'
import B from './components/B.vue'
import { ref } from 'vue'
let Flag = ref<boolean>(false)
const getFalg = (flag: boolean) => {
Flag.value = flag;
}
</script>
<style>
</style>
解释:A组件通过方法传值给父组件,父组件再使用props传给B组件
缺点:比较麻烦,无法直接通信,需要修改时得同时改3文件。
Bus
在vue2可以使用$emit 传递 $on监听 emit传递过来的事件。
运用了JS设计模式之发布订阅模式、观察者模式。
简易版
type BusClass<T> = {
emit: (name: T) => void
on: (name: T, callback: Function) => void
}
type BusParams = string | number | symbol
type List = {
[key: BusParams]: Array<Function>
}
class Bus<T extends BusParams> implements BusClass<T> {
list: List
constructor() {
// 创建一个队列存储方法
this.list = {}
}
emit(name: T, ...args: Array<any>) {
let eventName: Array<Function> = this.list[name]
// 触发已经存储的方法
eventName.forEach(ev => {
ev.apply(this, args)
})
}
on(name: T, callback: Function) {
let fn: Array<Function> = this.list[name] || [];
// 将传入的方法存储至队列中
fn.push(callback)
this.list[name] = fn
}
}
export default new Bus<number>()
使用
挂载到Vue config 全局,
Bus.on('change', (val: String) => {
this.value = val
})
// 触发
Bus.emit('change', 'newMsg')
mitt
Vue3 移除了 $on/$off,事件总线改用 mitt。
安装npm install mitt -S
通过emit派发, on 方法添加事件,off 方法移除,all.clear 清空所有
1. 挂载到globalProperties全局属性
import { createApp } from 'vue'
import App from './App.vue'
import mitt from 'mitt'
const Mit = mitt()
//TypeScript注册
// 由于必须要拓展ComponentCustomProperties类型才能获得类型提示
declare module "vue" {
export interface ComponentCustomProperties {
$Bus: typeof Mit
}
}
const app = createApp(App)
//Vue3挂载全局API
app.config.globalProperties.$Bus = Mit
app.mount('#app')
A组件中派发
import { getCurrentInstance } from 'vue'
const instance = getCurrentInstance();
// Vue3官网中强烈建议不要把它当作在组合式 API 中获取 this 的替代方案来使用。
const emit1 = () => {
instance?.proxy?.$Bus.emit('on-num', 100)
}
const emit2 = () => {
instance?.proxy?.$Bus.emit('*****', 500)
}
B组件监听
import { getCurrentInstance } from 'vue'
// 监听on-num
const instance = getCurrentInstance()
instance?.proxy?.$Bus.on('on-num', (num) => {
console.log(num,'===========>B')
})
// 监听所有事件
instance?.proxy?.$Bus.on('*',(type,num)=>{
console.log(type,num,'===========>B')
})
// 移除监听
instance?.proxy?.$Bus.off('on-num',Fn)
// 清空所有监听
instance?.proxy?.$Bus.all.clear()
2. 使用Provide、Inject
import { createApp } from 'vue'
import App from './App.vue'
import mitt from 'mitt'
const Mit = mitt()
const app = createApp(App)
// 需要类型标注的话,得使用InjectionKey
app.provide('$bus', Mit)
app.mount('#app')
// 使用:// import { inject } from "vue"// const $bus = inject('$bus')// console.log($bus)
3. 新建一个ts文件,用到的时候再引入(推荐)
import mitt from 'mitt';
type Events = {
foo: string;
bar?: number;
};
const emitter = mitt<Events>(); // inferred as Emitter<Events>
export default emitter
使用
emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'
emitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)
ps:解决mitt多次触发的方法:页面关闭时销毁事务线程
onUnmounted(() => {
emitter.all.delete('foo')
})
