xua*_*uan 15 typescript typescript-generics vuejs3
我创建了一个钩子函数并想在其他地方重用它:
\nimport { ref, Ref, reactive } from 'vue'\n\nconst useSelectAarray = <T>(items: T[]) => {\n const selectItem: Ref<T|null> = ref(null)\n const options = reactive([...items])\n const select = (key: string, value: any) => {\n const trigger = options.find((item: { [index: string]: any }) => item[key] === value)\n\n if (typeof trigger !== 'undefined') {\n selectItem.value = trigger\n }\n }\n\n return { \n selectItem,\n select,\n items: options\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n但是在selectItem.value = trigger,我收到类型错误Type 'UnwrapRefSimple<T>' is not assignable to type 'T'\xe3\x80\x82
我怎么解决这个问题?
\nn00*_*099 24
const selectItem = ref<T>(null) as Ref<T?>
Run Code Online (Sandbox Code Playgroud)
请参阅: https: //github.com/vuejs/vue-next/issues/2136
| 归档时间: |
|
| 查看次数: |
10306 次 |
| 最近记录: |