PHP前端开发

Vue js 通用编码标准

百变鹏仔 3天前 #JavaScript
文章标签 标准

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

通用编码标准

  1. 避免魔法数字和字符串:
   // 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. 避免内联样式:
   <!-- good -->   <div class="item">{{ item.name }}</div>   <style scoped>   .item {     color: red;   }   </style><!-- bad --><div :style="{ color: 'red' }">{{ item.name }}</div>

组件实践

  1. 组件可重用性:
   // good   <basebutton :label="buttonlabel" :disabled="isdisabled"></basebutton>   // bad   <button :disabled="isdisabled">{{ buttonlabel }}</button>
  1. 道具验证:
   // good   props: {     title: {       type: string,       required: true     },     age: {       type: number,       default: 0     }   }   // bad   props: {     title: string,     age: number   }
  1. 避免长方法:
   // 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. 避免具有副作用的计算属性:
   // 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:
   <!-- 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. 避免使用大模板:
   <!-- 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进行状态管理:
   // 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. 避免组件中的直接状态突变:
   // good   methods: {     updateuser() {       this.$store.commit('setuser', newuser);     }   }   // bad   methods: {     updateuser() {       this.$store.state.user = newuser; // direct mutation     }   }

错误处理和调试

  1. 全局错误处理:
   vue.config.errorhandler = function (err, vm, info) {     console.error('vue error:', err);   };
  1. 提供用户反馈:
   // 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 应用程序的质量、可维护性和效率。