javascript - VueJS 2 + typescript : computed value does not detect property defined by data

标签 javascript typescript vue.js vuejs2

以下组件:

<template lang="html">
  <div>
    <p>{{ bar }}</p>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';

export const FooBar = Vue.extend({
  computed: {
    bar: function() {
      return this.foo;
    }
  },
  data: function() {
    return {
      foo: 'bar',
    };
  },
});

export default FooBar;
</script>
导致类型错误:
13:19 Property 'foo' does not exist on type 'CombinedVueInstance<Vue, unknown, unknown, unknown, Readonly<Record<never, any>>>'.
    11 |   computed: {
    12 |     bar: function() {
  > 13 |       return this.foo;
       |                   ^
    14 |     }
    15 |   },
    16 |   data: function() {
Version: typescript 4.1.6
使用类样式的组件语法时不会发生这些类型的错误,但我不希望使用这种语法。是否有推荐的方法在 VueJS 组件上定义类型?
完整/最小的存储库复制在这里:https://github.com/davidRoussov/vue-typescript-error

最佳答案

Typescript Support 中所述Vue 文档的部分:
在您的情况下,您应该更改 bar: function() {bar: function(): string {如果您有一个返回值但没有 : VNode 注释的 render() 方法,您可能会遇到相同的错误。

关于javascript - VueJS 2 + typescript : computed value does not detect property defined by data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69694873/

相关文章:

javascript - 在knockout.js中创建observableArray的动态数组

javascript - 默认情况下数组没有原型(prototype)吗?

javascript - 如何(或更恰本地说 : Why) does JSON. Stringify 更改 Date() 值?

javascript - 如何在动态接口(interface)中访问泛型类型属性

TypeScript:用其他东西替换命名空间

javascript - 如何向子组件传递数据?

每次页面重新启动时,Firebase.auth().currentUser 都会变为 null

javascript - Redux 展示组件与容器组件

jquery - 将图标作为变量或 HTML 绑定(bind)附加到 Controller 中

vue.js - VueJS 绑定(bind)模型到动态插入的表单元素