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

在使用TypeScript开发Vue组件时遇到'Property ‘increment’ does not exist on type 'Add’'的错误,但组件仍能正常运行。解决方案包括在组件中定义对应的Prop或直接在methods内编写逻辑,并确保所有属性和方法都声明了正确的类型。
摘要由CSDN通过智能技术生成

用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: Exp

根据提供的引用内容,当在TypeScript中按照JS的方式去获取对象属性时,有时会出现形如"Property 'xxx' does not exist on type 'Object'"的错误提示。这是因为在TypeScript中,对象的类型是严格检查的,如果对象的类型为'Object',则无法直接访问或修改其属性。为了解决这个问题,可以使用类型断言或将对象的类型声明为'any'。具体代码如下: 1. 使用类型断言: ```typescript var obj: Object = Object.create(null); (obj as any).value = "value"; ``` 2. 将对象的类型声明为'any': ```typescript var obj: any = Object.create(null); obj.value = "value"; ``` 通过以上两种方式,可以避免出现编译错误,可以访问和修改任何属性。所以对于问题中的"Property 'isLangEn' does not exist on type '{}'"错误,可以使用以上的解决办法来解决。 #### 引用[.reference_title] - *1* *3* [TS 错误property does not exist on type Object](https://blog.csdn.net/qq_43592064/article/details/119345222)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [property xxx does not exist on type Object报错](https://blog.csdn.net/SmallTeddy/article/details/107164761)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值