前端代码规范 -- vue篇

组件命名规范

PascalCase (单词首字母大写命名)是最通用的声明约定

kebab-case (短横线分隔命名) 是最通用的使用约定

Prop定义

prop定义应该尽量详细,至少需要指定其类型。

反例:

props: ['status']

好例子:

props: {
  status: String,
  default: '',
}

Prop 名大小写

在声明 prop 的时候,其命名应该始终使用 camelCase,而在模板和 JSX 中应该始终使用 kebab-case。

反例:

props: {
  'greeting-text': String
}
<welcome-message greetingText="hi"></welcome-message>

好例子:

props: {
  greetingText: String
}
<welcome-message greeting-text="hi"></welcome-message>

使用 ES6 风格编码

  1. 定义变量使用 let ,定义常量使用 const
  2. 模板字符串
 // bad
  const a = 'foobar'
  const b = 'foo' + a + 'bar'

  // good
  const a = 'foobar'
  const b = `foo${a}bar`
  1. 解构赋值
 // 对象解构赋值
  // bad
  function getFullName(user) {
    const firstName = user.firstName
    const lastName = user.lastName
  }

  // good
  function getFullName(user) {
    const { firstName, lastName } = user 
  }

  1. 扩展运算符
  const items = [1, 2, 3, 4, 5]

  // bad
  const itemsCopy = items

  // good
  const itemsCopy = [...items]

组件/实例的选项顺序(将选择和认知成本最小化)

  <script>
    export default {
      components : {
      },
      
      props: {
	  },
	  
      data () {
        return {
        }
      },
      
      computed: {
      },
      
      watch: {
      },
      
      mounted() {
      },
      
      methods: {
      }
   }
  </script>

目录文件(store)

src
|-- store 
|    |-- index.js
|    |-- page1
|    |-- |-- index1.js
|    |-- page2
|    |-- |-- index2.js
index1.js, index1.js文件

const state = {}

const getters = {}

const mutations = {}

const actions = {}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
index.js文件

import page1 from './page1'
import page2 from './page2'

export default new Vuex.Store({
  state: {},
  getters: {},
  mutations: {},
  actions: {},
  modules: {
  	page1,
  	page2,
  },
})
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值