Vue前端开发规范整理(推荐)(6)

正例:

// store/modules/todos.js
export default {
 state: {
 list: []
 },
 mutations: {
 REMOVE_TODO (state, todoId) {
 state.list = state.list.filter(todo => todo.id !== todoId)
 }
 },
 actions: {
 removeTodo ({ commit, state }, todo) {
 commit('REMOVE_TODO', todo.id)
 }
 }
}
<!-- TodoItem.vue -->
<template>
 <span>
 {{ todo.text }}
 <button @click="removeTodo(todo)">
 X
 </button>
 </span>
</template>

<script>
import { mapActions } from 'vuex'

export default {
 props: {
 todo: {
 type: Object,
 required: true
 }
 },
 methods: mapActions(['removeTodo'])
}
</script>

反例:

// main.js
new Vue({
 data: {
 todos: []
 },
 created: function () {
 this.$on('remove-todo', this.removeTodo)
 },
 methods: {
 removeTodo: function (todo) {
 var todoIdToRemove = todo.id
 this.todos = this.todos.filter(function (todo) {
 return todo.id !== todoIdToRemove
 })
 }
 }
})

附录

1. 推荐使用vs code进行前端编码,规定Tab大小为2个空格

vs code配置

{
 "editor.tabSize": 2,
 "workbench.startupEditor": "newUntitledFile",
 "workbench.iconTheme": "vscode-icons",
 // 以下为stylus配置
 "stylusSupremacy.insertColons": false, // 是否插入冒号
 "stylusSupremacy.insertSemicolons": false, // 是否插入分好
 "stylusSupremacy.insertBraces": false, // 是否插入大括号
 "stylusSupremacy.insertNewLineAroundImports": false, // import之后是否换行
 "stylusSupremacy.insertNewLineAroundBlocks": false, // 两个选择器中是否换行
 "vetur.format.defaultFormatter.html": "js-beautify-html",
 "eslint.autoFixOnSave": true,
 "eslint.validate": [
 "javascript",
 {
 "language": "html",
 "autoFix": true
 },
 {
 "language": "vue",
 "autoFix": true
 },
 "javascriptreact",
 "html",
 "vue"
 ],
 "eslint.options": { "plugins": ["html"] },
 "prettier.singleQuote": true,
 "prettier.semi": false,
 "javascript.format.insertSpaceBeforeFunctionParenthesis": false,
 "vetur.format.js.InsertSpaceBeforeFunctionParenthesis": false,
 "vetur.format.defaultFormatter.js": "prettier",
 // "prettier.eslintIntegration": true
}

vs code 插件

  • Auto Close Tag
  • Path Intellisense
  • Prettier
  • Vetur
  • vscode-icons

总结

以上所述是小编给大家介绍的Vue前端开发规范整理,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对黑区网络网站的支持!

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/324.html