其他分享
首页 > 其他分享> > Property ‘xxx‘ does not exist on type ‘xxx‘报错解决

Property ‘xxx‘ does not exist on type ‘xxx‘报错解决

作者:互联网

用ts写一个组件的时候,遇到了Property ‘increment’ does not exist on type 'Add’的红点儿报错,但神奇的是竟然还能正常运行。
![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210602162037899.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTkzNTE5OA==,size_16,color_FFFFFF,t_70
在参考一些正确的代码后,有两个解决方案。

  1. export default class Add extends Vue {} 里面定义对应的Prop:

    import { Component, Vue, Prop } from “vue-property-decorator”;
    import { mapState } from “vuex”;

    @Component({
    computed: {
    …mapState({
    sum: (state) => (state as Record<string, number>).sum,
    }),
    },
    methods: {
    //…mapActions([“Add”, “Subtract”]),
    increment() {
    this.KaTeX parse error: Expected 'EOF', got '}' at position 28: …ch("Add"); }̲, decrement…store.dispatch(“Subtract”);
    },
    },
    })
    export default class Add extends Vue {
    @Prop({ type: Number, default: false })
    sum!: number;

    @Prop({ type: Function, default: false })
    increment!: void;

    @Prop({ type: Function, default: false })
    decrement!: void;
    }

  2. 删掉@Component()内的内容,直接在export default class Add extends Vue {} 里面写对应逻辑:

    import { Component, Vue, Prop } from “vue-property-decorator”;

    @Component({
    })
    export default class Add extends Vue {
    get sum(): number {
    return this.$store.state.sum;
    }

    increment(): void {
    this.$store.dispatch(“Add”);
    }

    decrement(): void {
    this.$store.dispatch(“Subtract”);
    }
    }

这里使用get获取sum,类似一个计算属性。对于方法只需要直接声明即可。计算属性和方法都需要声明类型。

标签:Vue,default,sum,xxx,Component,Prop,Add,does,报错
来源: https://blog.csdn.net/m0_54883970/article/details/123212990