Vue js 通用编码标准

2024-08-09 0 366

Vue js 通用编码标准

以下是 vue.JS 的其他好的和坏的做法:

通用编码标准

  1. 避免魔法数字和字符串
    • 对重复使用或具有特殊含义的值使用常量。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

   // good

   const max_items = 10;

   function additem(item) {

     if (items.length

<ol>

<li>

<strong>高效使用 v-for:</strong>

<ul>

<li>使用 v-for 时,始终提供唯一的键来优化渲染。

</li>

</ul>

</li>

</ol><pre class="brush:php;toolbar:false">   <!-- good -->

   <div v-for="item in items" :key="item.id">

     {{ item.name }}

   </div>

   <!-- bad -->

   <div v-for="item in items">

     {{ item.name }}

   </div>

  1. 避免内联样式:
    • 更喜欢使用 CSS 类而不是内联样式,以获得更好的可维护性。

1

2

3

4

5

6

7

8

<!-- good -->

<div class="item">{{ item.name }}</div>

<style scoped>

.item {

  color: red;

}

</style><!-- bad --><div :style="{ color: 'red' }">{{ item.name }}</div>

组件实践

  1. 组件可重用性:
    • 设计可通过 proPS 重用和配置的组件。

1

2

3

4

5

// good

<basebutton :label="buttonlabel" :disabled="isdisabled"></basebutton>

// bad

<button :disabled="isdisabled">{{ buttonlabel }}</button>

  1. 道具验证
    • 始终使用类型和必需的属性来验证 props。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// good

props: {

  title: {

    type: string,

    required: true

  },

  age: {

    type: number,

    default: 0

  }

}

// bad

props: {

  title: string,

  age: number

}

  1. 避免长方法:
    • 将长方法分解为更小、更易于管理的方法。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

// good

methods: {

  fetchdata() {

    this.fetchuserdata();

    this.fetchpostsdata();

  },

  async fetchuserdata() { ... },

  async fetchpostsdata() { ... }

}

// bad

methods: {

  async fetchdata() {

    const userresponse = await fetch('API/user');

    this.user = await userresponse.JSON();

    const postsresponse = await fetch('api/posts');

    this.posts = await postsresponse.json();

  }

}

  1. 避免具有副作用的计算属性:
    • 计算属性应该用于纯计算而不是副作用。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

// good

computed: {

  fullname() {

    return `${this.firstname} ${this.lastname}`;

  }

}

// bad

computed: {

  fetchdata() {

    // side effect: fetch data inside a computed property

    this.fetchuserdata();

    return this.user;

  }

}

模板实践

  1. 使用 v-show 与 v-if:
    • 使用 v-show 来切换可见性,而无需从 dom 添加/删除元素,并在有条件渲染元素时使用 v-if。

1

2

3

4

5

6

7

8

<!-- good: use v-show for toggling visibility -->

<div v-show="isvisible">content</div>

<!-- good: use v-if for conditional rendering -->

<div v-if="isloaded">content</div>

<!-- bad: use v-if for simple visibility toggling -->

<div v-if="isvisible">content</div>

  1. 避免使用大模板:
    • 保持模板干净、小;如果它们变得太大,请将它们分解成更小的组件。

1

2

3

4

5

6

7

8

   <!-- good: small, focused template -->

   <template><div>

       <baseHEADer></baseheader><basecontent></basecontent><basefooter></basefooter>

</div>

   </template><!-- bad: large, monolithic template --><template><div>

       <header>...</header><main>...</main><footer>...</footer>

</div>

   </template>

状态管理实践

  1. 使用vuex进行状态管理:
    • 使用 vuex 管理多个组件的复杂状态。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

// good

// store.js

export default new vuex.store({

  state: { user: {} },

  mutations: {

    setuser(state, user) {

      state.user = user;

    }

  },

  actions: {

    async fetchuser({ commit }) {

      const user = await fetchuserdata();

      commit('setuser', user);

    }

  }

});

  1. 避免组件中的直接状态突变:
    • 使用突变来修改 vuex 状态,而不是直接突变组件中的状态。

1

2

3

4

5

6

7

8

9

10

11

12

13

// good

methods: {

  updateuser() {

    this.$store.commit('setuser', newuser);

  }

}

// bad

methods: {

  updateuser() {

    this.$store.state.user = newuser; // direct mutation

  }

}

错误处理和调试

  1. 全局错误处理:
    • 使用 vue 的全局错误处理程序来捕获和处理错误。

1

2

3

vue.config.errorhandler = function (err, vm, info) {

  console.error('vue error:', err);

};

  1. 提供用户反馈
    • 发生错误时向用户提供清晰的反馈

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

// Good

methods: {

  async fetchData() {

    try {

      const data = await fetchData();

      this.data = data;

    } catch (error) {

      this.errORMessage = 'Failed to load data. Please try again.';

    }

  }

}

// Bad

methods: {

  async fetchData() {

    try {

      this.data = await fetchData();

    } catch (error) {

      console.error('Error fetching data:', error);

    }

  }

}

通过遵循这些额外的实践,您可以进一步提高 vue.js 应用程序的质量、可维护性和效率。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

免责声明
1. 本站所有资源来源于用户上传和网络等,如有侵权请邮件联系本站整改team@lcwl.fun!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系本站工作人员处理!
6. 本站资源售价或VIP只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 因人力时间成本问题,部分源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别!
9.本站所有源码资源都是经过本站工作人员人工亲测可搭建的,保证每个源码都可以正常搭建,但不保证源码内功能都完全可用,源码属于可复制的产品,无任何理由退款!

网站搭建学习网 CSS Vue js 通用编码标准 https://www.xuezuoweb.com/10363.html

常见问题
  • 本站所有的源码都是经过平台人工部署搭建测试过可用的
查看详情
  • 购买源码资源时购买了带主机的套餐是指可以享受源码和所选套餐型号的主机两个产品,在本站套餐里开通主机可享优惠,最高免费使用主机
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Fa快捷助手
手机编程软件开发

在手机上用手点一点就能轻松做软件

去做软件
链未云主机
免备案香港云主机

开通主机就送域名的免备案香港云主机

去使用
链未云服务器
免备案香港云服务器

支持售后、超低价、稳定的免备案香港云服务器

去使用