使用vue-cli3新建一个项目并写好基本配置(推荐)

2020-06-14 06:24:25易采站长站整理

1. 使用vue-cli3新建项目: https://cli.vuejs.org/zh/guide/creating-a-project.html

注意,我这里用gitbash不好选择选项,我就用了基本的cmd(系统命令提示符):上下箭头和空格键可控制选项。

详细步骤:

(1)vue create init: 这里我选择了自定义配置

(2)使用上下箭头和空格进行选择,我这里选择了这四个,之所以没用

css pre-processors
是因为我的项目中要用
postcss-cssnext
,后面会有详细配置说明

(3)后面的

Use class-style component syntax? 是否使用class风格的组件语法?

Use Babel alongside TypeScript for auto-detected polyfills? 是否使用babel做转义?

Use history mode for router? 是否使用路由的history模式?

Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? 把babel,postcss,eslint这些配置文件放哪?

Save this as a preset for future projects? (Y/n) 是否记录一下以便下次继续使用这套配置?

2. 改项目端口(个人需求): 在根目录新建vue.config.js(vue-cli3其他配置相关也写在这里)


module.exports = {
devServer: {
port: 8100, // 端口号
}
}

3. 根据环境设置相关变量(比如请求地址、打包输出路径等),并打包不同环境代码

(1)在根目录新建appconf.json,保存不同环境的对应变量配置


{
"dev": {
"serverUrl": "http://localhost:57156/"
},
"build": {
"serverUrl": "http://build.com/",
"outputDir": "../../dist/build",
"productId": "111"
},
"alpha": {
"serverUrl": "http://build-test.com/",
"outputDir": "../../dist/alpha",
"productId": "222"
}
}

(2)src下新建common文件夹,下面新建configByEnv.js,根据环境设置对应变量值


var path = require('path')
var appconf = require('../../appconf.json')
var serverurl
var productId = ''
switch (process.env.NODE_ENV) {
case 'production':
serverurl = appconf.build.serverUrl
productId = appconf.build.productId
break