1、前言
- 之前一直使用的是vue-cli2来搭建项目,最近要搭建一个项目,发现使用
vue init webpack demo
无法搭建项目,去看了官网发现更新到了3.0的版本。 - 现在就介绍一下vue-cli3项目搭建的过程以及需要注意的点。
- Vue-cli3官网
2、install
node
环境
node >= 8.9 推荐:8.11.0 +
node -v // 查看node版本
npm -v // 查看npm版本
- 这是官网的原话,意思是如果你已经安装了
vue-cli (1.x or 2.x)
的版本,你需要先卸载后再安装
The package name changed from vue-cli to @vue/cli.
If you have the previous vue-cli (1.x or 2.x) package installed globally,
you need to uninstall it first with npm uninstall vue-cli -g or yarn global remove vue-cli.
- 安装
npm uninstall vue-cli -g // 卸载旧版本
# OR
yarn global remove vue-cli
npm install -g @vue/cli // 安装最新的版本
# OR
yarn global add @vue/cli
3、创建新项目
vue create hello-world // mac
winpty vue.cmd create hello-world // windows环境
- 图中绿色的框中第一次是没有的,是保存的项目配置。这里选择
Manually select features
,选择更多配置。 - 按
上下键
来切换,按空格
键来确认或者取消,图中只是作为演示。红色部分为css预处理器
,选择之后如让你选择默认的css预处理器。 - 是否使用
router
的history
模式,选择y
,路由文件中会使用mode: 'history'
- 由于上面配置选择了使用
css预处理器
,这里提示我们选择一种默认的CSS pre-processor
- 由于上面配置选择了使用
linter / formatter
,这里选择一种ESLint
,选择ESLint + Standard config
标准模式。 - 选择什么时候执行eslint校验
Lint on save
- 选择以什么形式配置以上所选的功能
In dedicated config files
- 是否保存设置为一个预设模板,选择
y
,会让你输入个模板名Save preset as:
, 下次可选择此模板快速创建项目。 - 目录结构如下
4、vue-cli2
功能全覆盖
Vue CLI 3 uses the same vue binary, so it overwrites Vue CLI 2 (vue-cli).
If you still need the legacy vue init functionality, you can install a global bridge:
npm install -g @vue/cli-init
vue init webpack my-project
这样你就可以继续使用vue-cli2来创建项目了。
5、问题描述
- 问题1:
Windows
和mac
环境的差异
- 首先来看官网的一段提示, 大概的意思就是如果您在
Windows
上使用Git Bash
和minTTY
,交互提示将不起作用。 - 您必须使用
winpty vue.cmd create hello-world
来创建项目。 - 如果您仍然希望使用
vue create hello-world
语法,您可以通过在~/.bashrc
中添加别名alias vue='winpty vue.cmd'
- 重新启动Git Bash终端会话,以导入更新后的
bashrc
文件。
WARNING
If you are on Windows using Git Bash with minTTY, the interactive prompts will not work.
You must launch the command as winpty vue.cmd create hello-world.
If you however want to still use the vue create hello-world syntax, you can alias the command by adding the following line to your ~/.bashrc file. alias vue='winpty vue.cmd'
You will need to restart your Git Bash terminal session to pull in the updated bashrc file.
- 问题2:
node
版本的差异
- 如果
node
不是推荐的版本可能存在一些问题,如下所示: 由于我在配置的时候选择了sass/scss
作为默认的css
预处理器,在通过npm
安装sass/scss
的时候失败了,这时候有有两种处理 - 当
sass/scss
不是必须的时候,你可以选择不安装或者安装less
等代替。 - 你也可以在项目创建好后再配置
sass/scss
。 - 或者变更node版本为推荐的版本。