神采飞阳

 

inject注入函数

在父组件中
const handleFunction= (e) => {
相关代码

};
provide(
"handleFunction",handleFunction);

 


const fn = inject('handleFunction',Function,true);
然后调用方法fn
fn(参数);
在子组件中就可以这样写。如果注入的不是函数inject的第二和第三个参数不用写。Function代表注入的是函数,true代表有传入参数。
这在vue项目的 .d.ts文件中的代码
export declare function inject<T>(key: InjectionKey<T> | string, defaultValue: T | (() => T), treatDefaultAsFactory: true): T;
也可以看出来

 

posted on 2021-08-22 21:05  神采飞阳  阅读(525)  评论(0)    收藏  举报

导航