问题
Recently i migrated our vue.js2.x project to typescript, and based on evan-you's comment in this issue:
https://github.com/vuejs/rfcs/pull/17#issuecomment-494242121
The Class API proposal is being dropped. so i'm using regular option based version of vue.js which it faced me with alot of type problems.
I want to know is vuejs2.x composition api fully compatibile with typescript? and should i use it to solve all the problem? what is the best practice to do here in this situation?
回答1:
I'm unsure what "fully compatible with TypeScript" means. But you can definitely use TypeScript with the Vue composition API, and TypeScript helps to improve the code and the developer experience.
Here is an example using the composition plugin with 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
};
}
});
All the above code is well typed. The composition API (here: createComponent
, reactive
, computed
) is provided with correct types. Notice that with the composition API, we don't need to use this
anymore.
回答2:
Yes, Vue.js is compatible with TypeScript as the Vue CLI provides built-in TypeScript tooling support. It just needs to be installed via npm
or yarn
, as stated here, hereby it won't need any additional tooling to use TS with Vue.
There are also some explanations about recommended configuration and basic usage included in the given links.
来源:https://stackoverflow.com/questions/59873235/is-vuejs2-x-composition-api-fully-compatibile-with-typescript