ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

vivo蓝河操作系统实践教程-》精简 data 上数据

2023-11-14 10:45:02  阅读:113  来源: 互联网

标签:


一般来讲 data 上的数据都是有响应式追踪地,首先一定要保证 data 的数据在视图中使用到了,其次要尽可能的简化 data 上面的数据。

不渲染的数据不定义在 data 上

推荐级别:强烈

不需要数据绑定或页面间传递的数据,不要定义在 data 里

反例

<template>
  <text>{{a}}</text>
</template>

<script>
  export default {
    data: {
      a: 'hello',
      b: 'world',
    },
    onInit() {
      this.a = this.a + this.b
    },
  }
</script>

正例

<template>
  <text>{{a}}</text>
</template>

<script>
  const b = 'world'
  export default {
    data: {
      a: 'hello',
    },
    onInit() {
      this.a = this.a + b
    },
  }
</script>

简化 data 上数据

推荐级别:建议

动态绑定的数据结构应去除冗余信息,尽可能的简单合理

反例

<template>
  <list>
    <list-item for="{{list}}">
      <text>{{ $item.name }}</text>
      <image src="{{ $item.img }}"></image>
    </list-item>
  </list>
</template>

<script>
  export default {
    data: {
      list: [
        { name: 'a', value: '', img: 'image_1.png' },
        { name: 'b', value: '', img: 'image_2.png' },
        { name: 'c', value: '', img: 'image_3.png' },
        { name: 'd', value: '', img: 'image_4.png' },
      ],
    },
  }
</script>

正例

<template>
  <list>
    <list-item for="{{list}}">
      <text>{{ $item }}</text>
      <image src="{{`image_${$idx}.png`}}"></image>
    </list-item>
  </list>
</template>

<script>
  export default {
    data: {
      list: ['a', 'b', 'c', 'd'],
    },
  }
</script>

不使用动态属性修改 data 上数据

推荐级别:强烈

js 的对象属性可以使用 . 和中括号两种方式去访问,尽量不要用中括号放变量去修改对象的值。这样子做会使您的数据无法被框架优化。

反例

<template>
  <div>
    <text>{{person.name}}</text>
    <text>{{person.location}}</text>
  </div>
</template>

<script>
  export default {
    data: {
      person: {
        name: 'Vance',
        location: 'shenzhen',
      },
    },
    onInit() {
      const location = 'location'
      this.person[location] = 'beijing'
    },
  }
</script>

正例

<template>
  <div>
    <text>{{person.name}}</text>
    <text>{{person.location}}</text>
  </div>
</template>

<script>
  export default {
    data: {
      person: {
        name: 'Vance',
        location: 'shenzhen',
      },
    },
    onInit() {
      this.person.location = 'beijing'
    },
  }
</script>

自定义组件传参类型要简单

推荐级别:鼓励

自定义组件尽量用简单数据类型定义 props 进行传参,如 String、Number、Boolean 类型。

反例

<template>
  <my-button buttonInfo="{{buttonInfo}}"></my-button>
</template>

<script>
  export default {
    data: {
      buttonInfo: {
        name: '默认值',
        color: '#ff0000;',
      },
    },
  }
</script>

正例

<template>
  <my-button name="默认值" color="#ff0000;"></my-button>
</template>

渲染数据层级不宜过深

推荐级别:鼓励

动态绑定的数据层数不宜过深。

反例

<template>
  <div>
    <text>{{obj.a.name}}</text>
    <text>{{obj.b.name}}</text>
  </div>
</template>

<script>
  export default {
    data: {
      obj: {
        a: {
          name: 'name a',
        },
        b: {
          name: 'name b',
        },
      },
    },
  }
</script>

正例

<template>
  <div>
    <text>{{obj.a}}</text>
    <text>{{obj.b}}</text>
  </div>
</template>

<script>
  export default {
    data: {
      obj: {
        a: 'name a',
        b: 'name b',
      },
    },
  }
</script>

标签:
来源:

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有