Ts 提示对象的类型为 “unknown”

provide/inject 只适用于最外层

方案1 使用默认值 (有小问题)

如果是 ref 不要加 .value

provide('abc', {

name: editId,

});

const abc = inject('abc', { name: 0 });

console.log('abc', abc);

方案2 使用 InjectionKey 推荐

type.ts

import { InjectionKey, Ref } from 'vue';

export interface Params {

name: string;

}

export const paramsKey: InjectionKey> = Symbol('');

export type setParams = (params: Params) => void;

export const setParamsKey: InjectionKey = Symbol('');

import { paramsKey, setParamsKey, Params } from './type';

const user = ref({ name: 'zs' });

provide(paramsKey, user);

function setUser(params: Params) {

user.value.name = params.name;

}

provide(setParamsKey, setUser);

{{ user }}

精彩内容

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: