打造vuecli3+element后台管理系统(一)初始化项目

2,276 阅读3分钟

升级vue-cli2到vue-cli3

# 卸载vue-cli旧版本
npm uninstall vue-cli -g
# 安装新版本
npm install -g @vue/cli
# 查看版本
npm vue -V

创建项目

vue create newproject

接下来就是小孩子做选择题了

  • 选择自主配置

    1

  • 选择你要引入的初始化依赖包。咱项目里会用到这几个,按空格选中,再按一下就取消的哈~(这里我用cmd创建项目,没有用git-bash,git-bash在这里不好使,不支持选中:expressionless:。你也可以用vscode的终端来创建。)

    2

  • 路由要不要使用history模式。no。

    3

  • 预编译css语法选择哪一个,因为咱用的是elementUI,所以选择sass

    4

  • 项目代码风格规范,个人习惯问题,我习惯使用ESlint + Prettier

    5

  • 保存时自动检测代码是否合乎规范or在提交代码的时候一并格式化代码

    6

  • 配置文件的保存方式,我选择保存在package.json里

    7

  • 是不是要保存你上面的所有设置,下次创建项目默认按照这些选择来生成项目。no。

    8

目录结构

.
│  .gitignore <!-- git忽略文件配置文件 -->
│  babel.config.js <!-- babel配置文件 -->
│  package-lock.json <!-- 用以记录当前状态下实际安装的各个npm package的具体来源和版本号 -->
│  package.json <!-- 定义了这个项目所需要的各种模块,以及项目的配置信息(比如名称、版本、许可证等元数据) -->
│  README.md <!-- 项目介绍文件 -->
│  
├─node_modules <!-- 项目依赖包 -->
├─public <!-- 放不会变动的文件(相当于vue-cli2.x中的static) -->
│      favicon.ico <!-- 项目图标 -->
│      index.html <!-- 设置项目的一些meta头信息,提供用于挂载 vue 节点。 -->
│      
└─src <!-- 存放项目源码及需要引用的资源文件。 -->
    │  App.vue <!-- 根组件 -->
    │  main.js <!-- 入口文件 -->
    │  router.js <!-- 路由文件 -->
    │  store.js <!-- vuex仓库文件 -->
    │  
    ├─assets <!-- 静态资源文件,放可能会变动的文件 -->
    │      logo.png
    │      
    ├─components <!-- 公共组件 -->
    │      HelloWorld.vue
    │      
    └─views <!-- 页面 -->
            About.vue
            Home.vue
            

简单将vue-admin-template整合进项目

经过以上步骤,恭喜你,你的vuecli3项目已经初步搭建完成~~ 撒花 ~~ vue-admin-template里头的很多东西对我来说,是多余的,所以我就选择性的将我用的到的地方整合进项目里,我就不做详细解释了噢,捡整合中有点小困难的地方讲一讲。放上项目git地址,有需要的同学可以clone一下看看整合效果。

新建自己的ESlint配置文件

这个代码风格规范的问题,可以根据自己的习惯或者是公司协作开发的代码规范去修改。删除package.json下的eslintConfig配置信息,在根目录下新建.eslintrc.js文件,自定义代码规范。贴一下我习惯的规范配置:

module.exports = {
  root: true,
  parserOptions: {
    parser: 'babel-eslint',
    sourceType: 'module'
  },
  env: {
    browser: true,
    node: true,
    es6: true,
  },
  extends: ['plugin:vue/recommended', 'eslint:recommended'],

  // add your custom rules here
  //it is base on https://github.com/vuejs/eslint-config-vue
  rules: {
    "vue/max-attributes-per-line": [2, {
      "singleline": 10,
      "multiline": {
        "max": 1,
        "allowFirstLine": false
      }
    }],
    "vue/singleline-html-element-content-newline": "off",
    "vue/multiline-html-element-content-newline":"off",
    "vue/name-property-casing": ["error", "PascalCase"],
    "vue/no-v-html": "off",
    'accessor-pairs': 2,
    'arrow-spacing': [2, {
      'before': true,
      'after': true
    }],
    'block-spacing': [2, 'always'],
    'brace-style': [2, '1tbs', {
      'allowSingleLine': true
    }],
    'camelcase': [0, {
      'properties': 'always'
    }],
    'comma-dangle': [2, 'never'],
    'comma-spacing': [2, {
      'before': false,
      'after': true
    }],
    'comma-style': [2, 'last'],
    'constructor-super': 2,
    'curly': [2, 'multi-line'],
    'dot-location': [2, 'property'],
    'eol-last': 2,
    'eqeqeq': ["error", "always", {"null": "ignore"}],
    'generator-star-spacing': [2, {
      'before': true,
      'after': true
    }],
    'handle-callback-err': [2, '^(err|error)$'],
    'indent': [2, 2, {
      'SwitchCase': 1
    }],
    'jsx-quotes': [2, 'prefer-single'],
    'key-spacing': [2, {
      'beforeColon': false,
      'afterColon': true
    }],
    'keyword-spacing': [2, {
      'before': true,
      'after': true
    }],
    'new-cap': [2, {
      'newIsCap': true,
      'capIsNew': false
    }],
    'new-parens': 2,
    'no-array-constructor': 2,
    'no-caller': 2,
    'no-console': 'off',
    'no-class-assign': 2,
    'no-cond-assign': 2,
    'no-const-assign': 2,
    'no-control-regex': 0,
    'no-delete-var': 2,
    'no-dupe-args': 2,
    'no-dupe-class-members': 2,
    'no-dupe-keys': 2,
    'no-duplicate-case': 2,
    'no-empty-character-class': 2,
    'no-empty-pattern': 2,
    'no-eval': 2,
    'no-ex-assign': 2,
    'no-extend-native': 2,
    'no-extra-bind': 2,
    'no-extra-boolean-cast': 2,
    'no-extra-parens': [2, 'functions'],
    'no-fallthrough': 2,
    'no-floating-decimal': 2,
    'no-func-assign': 2,
    'no-implied-eval': 2,
    'no-inner-declarations': [2, 'functions'],
    'no-invalid-regexp': 2,
    'no-irregular-whitespace': 2,
    'no-iterator': 2,
    'no-label-var': 2,
    'no-labels': [2, {
      'allowLoop': false,
      'allowSwitch': false
    }],
    'no-lone-blocks': 2,
    'no-mixed-spaces-and-tabs': 2,
    'no-multi-spaces': 2,
    'no-multi-str': 2,
    'no-multiple-empty-lines': [2, {
      'max': 1
    }],
    'no-native-reassign': 2,
    'no-negated-in-lhs': 2,
    'no-new-object': 2,
    'no-new-require': 2,
    'no-new-symbol': 2,
    'no-new-wrappers': 2,
    'no-obj-calls': 2,
    'no-octal': 2,
    'no-octal-escape': 2,
    'no-path-concat': 2,
    'no-proto': 2,
    'no-redeclare': 2,
    'no-regex-spaces': 2,
    'no-return-assign': [2, 'except-parens'],
    'no-self-assign': 2,
    'no-self-compare': 2,
    'no-sequences': 2,
    'no-shadow-restricted-names': 2,
    'no-spaced-func': 2,
    'no-sparse-arrays': 2,
    'no-this-before-super': 2,
    'no-throw-literal': 2,
    'no-trailing-spaces': 2,
    'no-undef': 2,
    'no-undef-init': 2,
    'no-unexpected-multiline': 2,
    'no-unmodified-loop-condition': 2,
    'no-unneeded-ternary': [2, {
      'defaultAssignment': false
    }],
    'no-unreachable': 2,
    'no-unsafe-finally': 2,
    'no-unused-vars': [2, {
      'vars': 'all',
      'args': 'none'
    }],
    'no-useless-call': 2,
    'no-useless-computed-key': 2,
    'no-useless-constructor': 2,
    'no-useless-escape': 0,
    'no-whitespace-before-property': 2,
    'no-with': 2,
    'one-var': [2, {
      'initialized': 'never'
    }],
    'operator-linebreak': [2, 'after', {
      'overrides': {
        '?': 'before',
        ':': 'before'
      }
    }],
    'padded-blocks': [2, 'never'],
    'quotes': [2, 'single', {
      'avoidEscape': true,
      'allowTemplateLiterals': true
    }],
    'semi': [2, 'never'],
    'semi-spacing': [2, {
      'before': false,
      'after': true
    }],
    'space-before-blocks': [2, 'always'],
    'space-before-function-paren': [2, 'never'],
    'space-in-parens': [2, 'never'],
    'space-infix-ops': 2,
    'space-unary-ops': [2, {
      'words': true,
      'nonwords': false
    }],
    'spaced-comment': [2, 'always', {
      'markers': ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ',']
    }],
    'template-curly-spacing': [2, 'never'],
    'use-isnan': 2,
    'valid-typeof': 2,
    'wrap-iife': [2, 'any'],
    'yield-star-spacing': [2, 'both'],
    'yoda': [2, 'never'],
    'prefer-const': 2,
    'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
    'object-curly-spacing': [2, 'always', {
      objectsInObjects: false
    }],
    'array-bracket-spacing': [2, 'never']
  }
}

在项目中使用svg

将icons文件夹引入项目:

icons

引入svg组件

svg组件

在配置文件配置一些东西,让项目可以使用svg。

在根目录下新建vue.config.js:

const path = require('path')

function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  assetsDir: 'assets', // 静态资源文件夹
  devServer: {
    port: 9533 // 端口号
  },
  chainWebpack: config => {
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()

    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
  }
}

按照自己的需要选择性的将vue-admin-template中的东西copy到项目中

在入口文件引入各个依赖
// main.js
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import locale from 'element-ui/lib/locale/lang/zh-CN' // lang i18n
import 'element-ui/lib/theme-chalk/index.css'
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import '@/styles/index.scss' // global css
import 'element-ui/lib/theme-chalk/index.css'
import '@/icons' // icon

Vue.config.productionTip = false
Vue.use(ElementUI, {
  locale
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')