利用 vue 命令,创建一个项目,并启动webpack开发服务器
0、准备
全局安装@vue/cli模块包
#全局安装@vue/cli模块包
yarn global add @vue/cli
# OR
npm install -g @vue/cli
#查看`Vue`命令版本
vue -V
查看`Vue`命令版本 vue -V
1、创建项目 vue create vuecli-project
# vue和create是命令, vuecli-project是自己的文件夹名
vue create vuecli-project
- 创建项目 vue create vuecli-project
- 选择模板 Default ([Vue 2] babel, eslint) 可以上下箭头选择, 回车确定, 弄错了ctrl+c从第1步来
- 选择包管理器 Use Yarn or Use NPM
C:\Users\QYWF\Desktop>vue create vuecli-project Vue CLI v4.5.13 ? Please pick a preset: Default ([Vue 2] babel, eslint) Vue CLI v4.5.13 ✨ Creating project in C:\Users\QYWF\Desktop\vuecli-project. 🗃 Initializing git repository... ⚙️ Installing CLI plugins. This might take a while... yarn install v1.22.5 info No lockfile found. [1/4] Resolving packages... [2/4] Fetching packages... info fsevents@2.3.2: The platform "win32" is incompatible with this module. info "fsevents@2.3.2" is an optional dependency and failed compatibility check. Excluding it from installation. info fsevents@1.2.13: The platform "win32" is incompatible with this module. info "fsevents@1.2.13" is an optional dependency and failed compatibility check. Excluding it from installation. success Saved lockfile. Done in 159.65s. 🚀 Invoking generators... 📦 Installing additional dependencies... yarn install v1.22.5 [1/4] Resolving packages... [2/4] Fetching packages... info fsevents@2.3.2: The platform "win32" is incompatible with this module. info "fsevents@2.3.2" is an optional dependency and failed compatibility check. Excluding it from installation. info fsevents@1.2.13: The platform "win32" is incompatible with this module. info "fsevents@1.2.13" is an optional dependency and failed compatibility check. Excluding it from installation. [3/4] Linking dependencies... [4/4] Building fresh packages... success Saved lockfile. Done in 24.20s. ⚓ Running completion hooks... 📄 Generating README.md... 🎉 Successfully created project vuecli-project. 👉 Get started with the following commands: $ cd vuecli-project $ yarn serve C:\Users\QYWF\Desktop>
4.、等待下载脚手架项目需要的依赖包。
5、终端切换脚手架项目下, 启动内置的==webpack热更新开发服务器==
cd vuecil-project yarn serve 或 npm run serve 启动本地热更新开发服务器
C:\Users\QYWF\Desktop>cd vuecli-project C:\Users\QYWF\Desktop\vuecli-project>yarn serve
6、出现绿色,就代表成功启动了
7、在浏览器地址栏输入地址 localhost:8080 或192.168.151.91:8080/
2、拓展
脚手架项目目录
vuecil-project # 项目目录
├── node_modules # 项目依赖的第三方包
├── public # 静态文件目录
├── favicon.ico# 浏览器小图标
└── index.html # 单页面的html文件(网页浏览的是它)
├── src # 业务文件夹
├── assets # 静态资源
└── logo.png # vue的logo图片
├── components # 组件目录
└── HelloWorld.vue # 欢迎页面vue代码文件
├── App.vue # 整个应用的根组件
└── main.js # 入口js文件
├── .gitignore # git提交忽略配置
├── babel.config.js # babel配置
├── package.json # 依赖包列表
├── README.md # 项目说明
└── yarn.lock # 项目包版本锁定和缓存地址
- main.js - 项目打包入口 - Vue初始化
- App.vue - Vue页面入口
- index.html - 浏览器运行的文件
- App.vue => main.js => index.html
3、配置文件vue.config.js (src并列处,项目根目录下 )
/* 覆盖webpack的配置 */
module.exports = {
devServer: { // 自定义服务配置
open: true, // 自动打开浏览器
port: 3000
}
}
修改了接口,从localhost:3000 打开
当yarn serve 开启服务器命令时会自动打开浏览器 页面
ctrl + c 停止当前命令
yarn server 重新开启服务器