【问题标题】:Property does not exist on the type in vue-class-componentvue-class-component 中的类型不存在属性
【发布时间】:2020-01-14 00:54:52
【问题描述】:

我的 TypeScript 组件文件中出现一个错误,即 prop 不存在,但我完全按照 vue-class-component documentation 示例中的描述声明了该 prop。

Property 'propMessage' does not exist on type 'MyComponent'.Vetur(2339)

我该如何解决这个问题?

【问题讨论】:

    标签: typescript vue.js vuejs2 vue-component vue-class-components


    【解决方案1】:

    您链接的文档指出:

    以下是用 Babel 编写的示例。如果您正在寻找 TypeScript 版本,it's in the example directory

    例子:

    <script lang="ts">
        import Vue from 'vue';
        import { Component } from 'vue-class-component';
    
        const AppProps = Vue.extend({
          props: {
            propMessage: String
          }
        })
    
        @Component
        export default class YourComponent extends AppProps {
    
            get something() {
                return this.propMessage;
            }
        }
    </script>
    

    【讨论】:

    • Noooo.. 我知道我能做到。但是为什么 vue-class-component 不起作用?这是Prop 的简单情况,但在computedmapGetter 的复杂情况下也不起作用。
    • 所以我需要为每个组件声明Vue.extend?每次我想使用 propmapGetters 或使用 vue-class-component 时制作两个组件?如果是这样,如果我根本不使用vue-class-component 会更好。还有其他解决方案吗?为什么示例代码不起作用?我需要该示例按预期工作。
    • 要使示例工作,您应该完全按照您提供的文档中“用法”段落中的描述进行操作。旁注是,在他们告诉您的示例中,您可能想要查看vue-property-decorator,就像我在预先编辑的答案中建议的那样。
    • 但是computed: mapState([ 'count' ]), 我也有同样的错误:Property 'count' does not exist on type 'MyComponent'.Vetur(2339) 所以问题仍然存在。无论打字和扩展。
    • 我不确定你的代码现在用computed: mapState([ 'count' ]) 是什么样子,所以也许你可以在像jsfiddle这样的网站上做一个最小的例子?请注意,在 the typescript example 中,他们在第 65 行声明 count
    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 2021-02-08
    • 2020-12-18
    • 2021-12-17
    • 2020-12-11
    • 2023-01-03
    • 2019-07-16
    • 2019-03-07
    相关资源
    最近更新 更多