利用脚手架 创建项目

利用 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

  1. 创建项目 vue create vuecli-project
  2. 选择模板   Default ([Vue 2] babel, eslint)   可以上下箭头选择, 回车确定, 弄错了ctrl+c从第1步来
  3. 选择包管理器 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    # 项目包版本锁定和缓存地址

  1. main.js - 项目打包入口 - Vue初始化
  2. App.vue - Vue页面入口
  3. index.html - 浏览器运行的文件
  4. 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 重新开启服务器

### 如何使用 Vue CLI 创建 Vue 2 项目 为了创建基于 Vue 2 的新项目,需先确认已全局安装 `@vue/cli` 工具。这可以通过执行命令 `npm install -g @vue/cli` 来完成[^2]。 一旦安装完毕,验证其正确性的方法是输入 `vue --version` 命令;如果显示了版本号,则表明安装成功[^1]。 接下来进入实际项目创建工作: #### 安装特定版本的 Vue CLI (可选) 对于希望确保兼容性或有特殊需求的情况,可以指定安装某个具体版本的 Vue CLI,比如4.1.1版: ```bash npm install -g @vue/cli@4.1.1 ``` #### 初始化一个新的 Vue 2 项目 利用交互式的命令行界面来启动新的 Vue 项目构建过程: ```bash vue create my-vue2-project ``` 在此过程中会提示选择一系列配置选项,在这里应特别注意选择 Vue 版本的地方,务必挑选 Vue 2.x 作为目标框架版本。 #### 配置 Uni-app 项目(针对特定场景) 当涉及到 uni-app 类型的应用开发时,除了上述基本步骤外,还需额外关注一些细节设置。例如,在项目根目录下的 `vue.config.js` 文件里加入必要的依赖转换规则: ```javascript module.exports = { transpileDependencies: ['uview-ui'] } ``` 此操作是为了让某些组件库能够正常工作于当前环境中[^3]。 另外一种方式则是直接采用预设模板快速搭建 uni-app 应用程序结构: ```bash vue create -p dcloudio/uni-preset-vue my-uniproject ``` 这种方式能更加快捷地获得一个已经适配好 uni-app 平台的基础工程架构[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值