手把手教你搭建vue3.x项目

发布时间:2022-02-16 11:59:49 作者:路人甲 阅读(3207)

1、首先进入你的项目目录,执行以下命令安装vue的环境

  1. npm install -g @vue/cli

当看到结尾为以下内容表示安装完成

  1. changed 945 packages, and audited 946 packages in 34s
  2. 68 packages are looking for funding
  3. run `npm fund` for details
  4. 13 vulnerabilities (6 moderate, 7 high)
  5. Some issues need review, and may require choosing
  6. a different dependency.
  7. Run `npm audit` for details.

2、创建vue项目

2.1 搭建脚手架

使用以下命令搭建脚手架,project-name 是你的项目名称,按需修改

  1. vue create project-name

2.2 选择创建项目模式

这里选择Manually select features(自定义),按回车键进入下一步

  1. Vue CLI v4.5.15
  2. ? Please pick a preset:
  3. Default ([Vue 2] babel, eslint)
  4. Default (Vue 3) ([Vue 3] babel, eslint)
  5. > Manually select features

2.3 选择需要的功能

按需选择,按上下选择,按空格键选中,确定无误后按回车键进入下一步

  1. Vue CLI v4.5.15
  2. ? Please pick a preset: Manually select features
  3. ? Check the features needed for your project:
  4. (*) Choose Vue version
  5. ( ) Babel
  6. (*) TypeScript
  7. ( ) Progressive Web App (PWA) Support
  8. >(*) Router
  9. (*) Vuex
  10. (*) CSS Pre-processors
  11. ( ) Linter / Formatter
  12. ( ) Unit Testing
  13. ( ) E2E Testing

2.4 选择vue的版本

这里选择3.x版本,按回车键进入下一步

  1. Vue CLI v4.5.15
  2. ? Please pick a preset: Manually select features
  3. ? Check the features needed for your project: Choose Vue version, TS, Router, Vuex, CSS Pre-processors
  4. ? Choose a version of Vue.js that you want to start the project with
  5. 2.x
  6. > 3.x

2.5 确认选择

这里会列出你刚刚的选择项,确认无误后输入 y, 然后按回车键进入下一步

  1. Vue CLI v4.5.15
  2. ? Please pick a preset: Manually select features
  3. ? Check the features needed for your project: Choose Vue version, TS, Router, Vuex, CSS Pre-processors
  4. ? Choose a version of Vue.js that you want to start the project with 3.x
  5. ? Use class-style component syntax? (y/N)

2.6 是否使用babel编译JSX

输入 y, 然后按回车键进入下一步

  1. ? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? (y/N)

2.7 是否用history模式来创建路由

输入 y, 然后按回车键进入下一步

  1. ? Use history mode for router? (Requires proper server setup for index fallback in production) (Y/n)

2.8 选择一个CSS预处理器

这里选选择Less, 然后按回车键进入下一步

  1. ? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default):
  2. Sass/SCSS (with dart-sass)
  3. Sass/SCSS (with node-sass)
  4. > Less
  5. Stylus

2.9 你更喜欢把Babel、ESLint等的配置放在哪里

选择In package.json, 然后按回车键进入下一步

  1. ? Where do you prefer placing config for Babel, ESLint, etc.?
  2. In dedicated config files
  3. > In package.json

2.10 是否保存为 future 项目

输入 y, 然后按回车键进入下一步

  1. Save this as a preset for future projects? (y/N)

2.11 预设另存为

这边不填写任何值,直接按回车键,此时会自动创建项目,需要等待大约十几秒

  1. ? Save preset as:

2.12 项目创建完成

当控制台显示以下信息时表示项目创建成功

  1. added 36 packages in 4s
  2. Running completion hooks...
  3. Generating README.md...
  4. Successfully created project project-name.
  5. Get started with the following commands:
  6. $ cd project-name
  7. $ npm run serve

2.13 启动项目

执行完成后会在当前目录生成一个名为 project-name 的文件夹,这个文件夹就是创建的vue项目,此时进入目录并启动项目

  1. # 进入项目
  2. cd project-name
  3. # 启动项目
  4. npm run serve

当控制台展示以下信息表示项目启动完成

  1. DONE Compiled successfully in 2713ms 上午11:57:46
  2. App running at:
  3. - Local: http://localhost:8081/
  4. - Network: http://192.168.255.10:8081/
  5. Note that the development build is not optimized.
  6. To create a production build, run npm run build.
  7. No issues found.

此时就可以通过 链接:http://localhost:8081/来访问项目了

关键字前端