JS全栈之巅前端开发Amazing Arch

Vue前端开发规范

2019-07-30  本文已影响8人  waynian

引用自:
vue官方
腾讯全端AlloyTeam 团队

1 环境

1.1 代码工具

推荐VSCode、webstorm、sublime、Atom
统一开发,尽量使用VSCode,轻量、插件多,免费

1.2 工具插件(VSCode)

Vue开发插件:eslintHTML CSS SupportHTML SnippetsLive ServerVeturVue VSCode Snippets

1.3 VSCode设置

保存时自动按照eslint规则格式化代码

{
     "eslint.validate": [{
      "language": "vue",
      "autoFix": true
    },
    {
      "language": "html",
      "autoFix": true
    },
    {
      "language": "javascript",
      "autoFix": true
    }
  ],
  "eslint.autoFixOnSave": true,
}

2 命名

2.1 项目命名

全部采用小写方式, 以下划线分隔。 例:my_project_name

2.2 目录(文件夹)命名

参照项目命名规则;
有复数结构时,要采用复数命名法。
例:scriptsstylesimagesdata_models

image

2.2 文件命名

2.2.1 components

组件命名使用大驼峰(KebabCase)TodoItem.vue

2.2.2 views(pages)

页面命名使用连接符(kebab-case)user-info.vue
如果views下的文件件只有一个文件,命名使用index.vue

│── views
│   └── user_info       
│         ├── index.vue

引用例子:
// 引用到  文件夹 + '/'即可
import("../views/user_info/")

2.2.3 JS文件命名

名使用分隔符线resize-event.js

如果为单个单词,使用小写md5.js

2.2.3 CSS, SCSS文件命名

jdc.scss
jdc_list.scss
jdc_detail.scss
/* not good */
@import "_dialog.scss";

/* good */
@import "dialog";

2.2.4 HTML文件命名

使用下划线

jdc.html
jdc_list.html
jdc_detail.html

2.3 组件

export default {
  name: 'TodoItem',
  // ...
}

Vue.component('todo-item', {
  // ...
})
components/
|- BaseButton.vue
|- BaseTable.vue
|- BaseIcon.vue


components/
|- AppButton.vue
|- AppTable.vue
|- AppIcon.vue

components/
|- VButton.vue
|- VTable.vue
|- VIcon.vue
components/
|- TheHeading.vue
|- TheSidebar.vue
components/
|- TodoList.vue
|- TodoListItem.vue
|- TodoListItemButton.vue

components/
|- SearchSidebar.vue
|- SearchSidebarNavigation.vue
components/
|- SearchButtonClear.vue
|- SearchButtonRun.vue
|- SearchInputQuery.vue
|- SearchInputExcludeGlob.vue
|- SettingsCheckboxTerms.vue
|- SettingsCheckboxLaunchOnStartup.vue
<!-- 在单文件组件和字符串模板中 -->
<MyComponent/>

<!-- 在 DOM 模板中 -->
<my-component></my-component>

或者
<!-- 在所有地方 -->
<my-component></my-component>
components/
|- StudentDashboardSettings.vue
|- UserProfileOptions.vue
props: {
  greetingText: String
}

<WelcomeMessage greeting-text="hi"/>
上一篇 下一篇

猜你喜欢

热点阅读