vuejs2.x 组合 api 与打字稿完全兼容吗? [英] is vuejs2.x composition api fully compatibile with typescript?

查看:26
本文介绍了vuejs2.x 组合 api 与打字稿完全兼容吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我将我们的 vue.js2.x 项目迁移到了 typescript,并基于 evan-you 在本期的评论:

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

Class API 提案正在被删除.所以我使用的是基于常规选项的 vue.js 版本,它给我带来了很多类型问题.

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.

我想知道 vuejs2.x 组合 api 是否与 typescript 完全兼容?我应该用它来解决所有问题吗?在这种情况下,最好的做法是什么?

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?

推荐答案

我不确定与 TypeScript 完全兼容"是什么意思.但是您绝对可以将 TypeScript 与 Vue 组合 API 一起使用,TypeScript 有助于改进代码和开发者体验.

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.

这是一个使用 composition 插件和 Vue 2 的示例:

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
    };
  }
});

以上所有代码的类型都很好.组合 API(此处:createComponentreactivecomputed)提供了正确的类型.请注意,使用组合 API,我们不再需要使用 this.

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.

这篇关于vuejs2.x 组合 api 与打字稿完全兼容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆