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

34 篇文章 1 订阅

用ts写一个组件的时候,遇到了Property ‘increment’ does not exist on type 'Add’的红点儿报错,但神奇的是竟然还能正常运行。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210602162037899.png?x-oss-process=image/watermark,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,类似一个计算属性。对于方法只需要直接声明即可。计算属性和方法都需要声明类型。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值