is vuejs2.x composition api fully compatibile with typescript?

一世执手 提交于 2021-01-28 06:15:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!