【发布时间】:2021-06-27 09:20:34
【问题描述】:
我是 TypeScript 的新手,并尝试将它与 Vue 3 组合 API 一起使用并提供/注入。
假设在父组件A 我有这样的东西:
// Parent component A
import { provide, ref } from 'vue';
import ScoreType from "@/types/Score";
setup() {
..
const score = ref<ScoreType[]>([]);
const updateScore = (val: ScoreType) => {
score.value.push(val);
};
provide('update_score', updateScore);
..
}
...然后想在子组件B 中注入updateScore 函数,以便能够更新父组件A (this is what docs recommend) 中的值。不幸的是,我收到了一个 TS 错误Object is of type 'unknown'
// Child component B
import { inject } from 'vue';
setup() {
..
const updateScore = inject('update_score');
const checkAnswer = (val: string) => {
updateScore({ /* ScoreType object */ }); // → Object is of type 'unknown'.
}
..
}
我应该怎么做才能修复 TypeScript 错误?我找不到任何关于在 TS 中注入更新函数的示例。
【问题讨论】:
标签: typescript vuejs3 inject vue-composition-api mutating-function