首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以在组合API和Vue类组件中使用vue 3?

是否可以在组合API和Vue类组件中使用vue 3?
EN

Stack Overflow用户
提问于 2021-11-21 17:52:29
回答 2查看 758关注 0票数 1

在过去的8个月中,我们使用vue 3和类组件构建了一个项目,但由于它似乎不再被维护,我们希望逐步切换到composition,更确切地说是使用安装脚本语法。

我们目前正在使用vue3.0.0和vue-class-components 8.0.0。

我们的目标是,因为我们必须不断地向项目添加新特性,开始使用复合API创建新组件,同时保持那些已经用vue类组件编写的组件。而且,在我们继续的过程中,我们将尝试用复合API重写它们。

我尝试使用vue类组件创建一个简单的HelloWorld组件:

代码语言:javascript
复制
<template>
 <div>
   <TestComponent :test="'test string'" />
 </div>
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component';
import TestComponent from './TestComponent.vue';

@Options({
 components: { TestComponent }
})
export default class HelloWorld extends Vue {
}
</script>

并添加一个测试组件:

代码语言:javascript
复制
<template>
  <h1>Test composant {{ test }}</h1>
</template>

<script lang="ts">
export default {
  name: 'TestComponent',
  props: { test: {type: String, required: true }},
  setup(props: { test: string }, context: unknown) { console.log(context); return { test: props.test } }
}
</script>

但是,我的代码中出现了一个错误:当在TestComponent中声明HelloWorld时,编译器告诉我,他期待在TestComponent中声明的参数'test‘:

代码语言:javascript
复制
Argument of type '{ name: string; props: { test: StringConstructor; required: boolean; }; setup(props: { test: string; }, context: unknown): { test: string; }; }' is not assignable to parameter of type 'Component<any, any, any, ComputedOptions, MethodOptions>'.
  Type '{ name: string; props: { test: StringConstructor; required: boolean; }; setup(props: { test: string; }, context: unknown): { test: string; }; }' is not assignable to type 'ComponentOptions<any, any, any, ComputedOptions, MethodOptions, any, any, any>'.
    Type '{ name: string; props: { test: StringConstructor; required: boolean; }; setup(props: { test: string; }, context: unknown): { test: string; }; }' is not assignable to type 'ComponentOptionsBase<any, any, any, ComputedOptions, MethodOptions, any, any, any, string, {}>'.
      Types of property 'setup' are incompatible.
        Type '(props: { test: string; }, context: unknown) => { test: string; }' is not assignable to type '(this: void, props: Readonly<LooseRequired<any>>, ctx: SetupContext<any>) => any'.
          Types of parameters 'props' and 'props' are incompatible.
            Property 'test' is missing in type 'Readonly<LooseRequired<any>>' but required in type '{ test: string; }'.ts(2345)
TestComponent.vue.ts(5, 18): 'test' is declared here.

更新:我尝试在TestComponent中全局注册main.ts,但是错误仍然是相同的。

有办法让这两个人一起工作吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-22 02:03:12

一个问题是您的props声明是不正确的。它应该是:

代码语言:javascript
复制
// ❌ invalid
// props: {
//   test: String,
//   required: true
// },

props: {
  test: {
    type: String,
    required: true
  }
},

此外,您不需要键入setup()的参数,因为它们通常是从defineComponent()实用程序中推断出来的,启用类型推理在Vue组件中是这样的:

代码语言:javascript
复制
// export default {
//   setup(props: { test: string }, context: unknown) { /*...*/ }
// }

import { defineComponent } from 'vue'
export default defineComponent({
  setup(props, context) { /*...*/ }
})
票数 1
EN

Stack Overflow用户

发布于 2021-11-22 01:54:33

请注意,:testv-bind:test的语法糖,这意味着如果您没有用于:test的反应性变量绑定,则应该使用test。见下文:

代码语言:javascript
复制
// note there is no colon before test, which means you are just passing a constant string.
<TestComponent test="test string" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70057164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档