在父組件中
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;
也可以看出來