Vue中避免滥用this去读取data中数据

  • template模板中如何避免

提前处理v-for循环所用的数据,不要在v-for循环中去读取数组、对象类型的数据。在上述template模板中滥用this的例子中可以这样优化。

假设listarrobj皆是服务端返回来的数据,且arrobj没有用到任何模块渲染中,可以这样优化

优化前:

<template>
  <div class="wrap">
    <div v-for=item in list>
      <div> {
  { arr[item.index]['name'] }} </div>
      <div> {
  { obj[item.id]['age'] }} </div>
    </div>
  </div>
</template>

优化后:

<template>
  <div class="wrap">
    <div v-for=item in listData>
      <div{
  {item.name}} </div>
        <div>{
  {item.age}}</div>
    </div>
  </div>
</template>
<script>
const arr = [];
const obj = {}
export default {
  data() {
    return {
      list: [],
    }
  },
  computed: {
    listData: function ({list}) {
      list.forEach(item => {
        item.name = arr[item.index].name;
        item.age = obj[item.id].age;
      })
      return list;
    }
  },
}
</script>

相关推荐

  1. Vue避免滥用this读取data数据

    2024-01-13 18:10:01       62 阅读
  2. vue-mounted处理data数据

    2024-01-13 18:10:01       50 阅读
  3. Vuethis.$nextTick的执行时机

    2024-01-13 18:10:01       62 阅读
  4. vuethis.$nextTick的作用

    2024-01-13 18:10:01       31 阅读
  5. vue this.$set 的作用

    2024-01-13 18:10:01       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-13 18:10:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-13 18:10:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-13 18:10:01       87 阅读
  4. Python语言-面向对象

    2024-01-13 18:10:01       96 阅读

热门阅读

  1. Flutter--常用技术文档

    2024-01-13 18:10:01       43 阅读
  2. 网络基础面试题(五)

    2024-01-13 18:10:01       55 阅读
  3. Redis 内存碎片

    2024-01-13 18:10:01       58 阅读
  4. android 返回到首页案例

    2024-01-13 18:10:01       60 阅读
  5. Redis面试系列-02

    2024-01-13 18:10:01       51 阅读
  6. how to protect your stomach

    2024-01-13 18:10:01       43 阅读