首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >获取Vue警告TypeError:无法读取未定义的属性,但数据存在

获取Vue警告TypeError:无法读取未定义的属性,但数据存在
EN

Stack Overflow用户
提问于 2018-06-28 07:25:59
回答 1查看 4.5K关注 0票数 1

我收到以下错误:

代码语言:javascript
复制
[Vue warn]: Error in nextTick: "TypeError: Cannot read property 'itemName' of undefined"

found in

---> <AssetEditForm> at src\components\AssetEditForm.vue
       <AppSection> at src\components\AppSection.vue
         <MyAssets> at src\views\Assets.vue
           <App> at src\App.vue
             <Root>

代码语言:javascript
复制
vue.runtime.esm.js?2b0e:1737 TypeError: Cannot read property 'itemName' of undefined
at VueComponent.itemName (AssetEditForm.vue?6bb7:218)
at Watcher.get (vue.runtime.esm.js?2b0e:3138)
at Watcher.evaluate (vue.runtime.esm.js?2b0e:3245)
at VueComponent.computedGetter [as itemName] (vue.runtime.esm.js?2b0e:3503)
at VueComponent.eval (AssetEditForm.vue?6bb7:104)
at Array.eval (vue.runtime.esm.js?2b0e:1833)
at flushCallbacks (vue.runtime.esm.js?2b0e:1754)

NB

  • 我在非常相似的组件中使用相同的代码,并且没有得到这些错误
  • 所有项目在Created()中抛出此警告,即如果我删除this.itemName,则this.ownership将触发警告,依此类推
  • 即使存在这些错误,数据在Vue开发工具中仍然可用,并按预期在浏览器中显示

< code >H113只有当我编辑动态创建的项目时才会出现错误。例如,通过API拉入的项目在编辑时不会触发此警告。

AssetEditForm

代码语言:javascript
复制
props: {
    myAssets:{},
    assetId:{
        type: String
    },
},data: function () {
    return {
        isShown: this.showForm,
        myAssetId: this.assetId,
        assetName: '',
        assetOwnership: '',
        assetOwnershipPercentage: '',
        assetEstimatedValue: '',
        typePlural: this.type + '\'s',
        typeLowerCase: this.type.toLowerCase(),
        // assetIdIs: '',
    }
},
created: function () {
    this.$nextTick(function () {
        console.log('created');
        this.assetName = this.itemName;
        this.assetOwnership = this.ownership;
        this.assetOwnershipPercentage = this.ownershipPercentage;
        this.assetEstimatedValue = this.estimatedValue;
    })
},
computed: {
    getAssetId: function() {
        const myInfo = this.myAssets;
        return myInfo.find(x => x.id == this.assetId);
    },
    itemName: function() {
        return this.getAssetId.itemName
    },
    ownership: function() {
        return this.getAssetId.ownership
                },
    ownershipPercentage: function() {
        return this.getAssetId.ownershipPercentage
    },
    estimatedValue: function() {
        return this.getAssetId.estimatedValue
    },
},
EN

回答 1

Stack Overflow用户

发布于 2018-06-28 07:47:58

问题可能出在itemName的计算属性上。如果this.myAssets为空或未包含正确的项,getAssetId中的find()将返回undefined (https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/find)。在您尝试访问未定义的itemNameitemName的computed属性中使用了getAssetId

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51072725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档