Vue(踩坑)vue.esm.js?efeb:628 [Vue warn]: Error in render: "TypeError: Cannot read property 'length' of...

1、项目报错如下:

 

2、先访问别的路由,在访问这个页面的路由这样不报错,但是我直接在这个页面上刷新就报错

 

3、分析解决:

                     1)找到我的length出现的地方,说出错说明现在info里面是空的

                        

 

      2)先进入其他路由---在进入当前路由---------此时我的info已经拿到了,vuex里面存的不是空对象了, 这种方式不会报错

      3)直接在这个页面刷,当前路由对象就会创建,第一次显示的时候vuex里面是空的,所以会报错。

 

4、 

 用户可能从两个地方访问这个路由组件,下面是考虑两种方式进来的代码

 

mounted(){    //方式一:从别的路由组件切换过来走这里
        new BScroll('.shop-info')
    
        if(!this.info.pics)
            return     
        new BScroll('.pic-wrapper',{
            scrollX:true
        })
        const liWidth = 120;
        const space = 6;
        const objUl = this.$refs.picsUl;
        
        objUl.style.width = (liWidth + space)*this.info.pics.length - space + 'px'

    },

//方式二:用watch监视到当前vuex里面的info有数据了才执行后面的
    watch:{ //在当前组件刷新
        info(){
            this.$nextTick(()=>{
            
                new BScroll('.pic-wrapper',{
                    scrollX:true
                })
                const liWidth = 120;
                const space = 6;
                const objUl = this.$refs.picsUl;
                
                objUl.style.width = (liWidth + space)*this.info.pics.length - space + 'px'
            })
        }
    }

 

转载于:https://www.cnblogs.com/xxm980617/p/10878390.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误是由于在渲染时尝试访问一个未定义的属性'state'引起的。根据引用[3]的内容,这个错误可能是因为你没有正确处理初始状态为空对象的情况。在你的getters函数中,你可以添加一个空对象的默认返回值来避免这个错误。通过在return语句后面添加"|| {}",即使state.goodsInfo初始值为空对象,也会返回一个空对象作为默认值。这样,就不会再出现"Cannot read property 'state' of undefined"的错误了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [报错 | vue.runtime.esm....c320:4560 [Vue warn]: Error in render: “TypeError: Cannot read properties of](https://blog.csdn.net/muziqwyk/article/details/126191223)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [【报错vue.runtime.esm.js?c320:4605 [Vue warn]: Duplicate keys detected: ‘40825‘. This may cause ...](https://blog.csdn.net/qq_40797578/article/details/128266387)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值