关于前端:前端开发Vue项目中解决Emitted-value-instead-of-an-instance-of-Error问题

分享一个在刚接触前端开发的开发者常遇到的一个经典谬误,那就是在Vue我的项目运行中遇到Emitted value instead of an instance of Error的问题,附带解决该问题的办法以及原理。

重现报错提醒:

(Emitted value instead of an instance of Error) <van-cell v-for=" item  in this.todoList">: component lists rendered with v-for should have explicit keys. See https://vuejs.org/guide/list.html#key for more info.


因为上述正告造成Vue我的项目不能启动,正告的大略意思就是在组件外面应用v-for 然而没有设置key,会造成非唯一性问题。

针对上述问题的解决办法:
在正告的组件外面v-for前面加一个属性key,为元素绑定了一个key,v-for=”(item, index) in this.todoList” :key=”index” 的操作,即:

 <van-cell
          v-for="(item, index) in this.todoList" :key="index"
           :to="{ name: 'Approval/Detail', params: { id: item.businessId } }"
        >
          <van-icon name="bell" size="30" />
          <div class="info-right">
            <p class="user-info">{{ item.startBy }}</p>
            <p class="place">{{ item.processInstanceName }}</p>
          </div>
        </van-cell>


上述这样操作就防止了Emitted value instead of an instance of Error问题。

应用原理:
当应用v-for进行列表渲染时,虚构dom和理论dom不一样,不能做唯一性,为元素绑定一个key,能够确保唯一性操作,这也是Vue官网举荐的做法。

以上就是本章全部内容,欢送关注三掌柜的微信公众号“程序猿by三掌柜”,三掌柜的新浪微博“三掌柜666”,欢送关注!

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理