最近我将我们的 vue.js2.x 项目迁移到 typescript,并基于 evan-you 在本期的评论:
https://github.com/vuejs/rfcs/pull/17#issuecomment-494242121
类 API 提案正在被放弃。所以我使用的是基于常规选项的 vue.js 版本,它给我带来了很多类型问题。
我想知道 vuejs2.x composition api 是否与 typescript 完全兼容?我应该用它来解决所有问题吗?在这种情况下,最佳做法是什么?
最近我将我们的 vue.js2.x 项目迁移到 typescript,并基于 evan-you 在本期的评论:
https://github.com/vuejs/rfcs/pull/17#issuecomment-494242121
类 API 提案正在被放弃。所以我使用的是基于常规选项的 vue.js 版本,它给我带来了很多类型问题。
我想知道 vuejs2.x composition api 是否与 typescript 完全兼容?我应该用它来解决所有问题吗?在这种情况下,最佳做法是什么?
我不确定“与 TypeScript 完全兼容”是什么意思。但是您绝对可以将 TypeScript 与 Vue 组合 API 一起使用,TypeScript 有助于改进代码和开发人员体验。
这是一个使用Vue 2 的组合插件的示例:
import { computed, createComponent, reactive } from "@vue/composition-api";
export default createComponent({
name: "Hello",
template: `<p>{{ state.message }}</p>`,
props: {
name: {
type: String,
required: true
}
},
setup(props, context) {
const state = reactive({
message: computed(() => `Hello, ${props.name}!`)
});
return {
state
};
}
});
上面所有的代码都是很好的类型。组合 API(此处为:、、、createComponent)reactive提供computed了正确的类型。请注意,使用组合 API,我们不再需要使用this。