Vue 脚手架CLI 初始化项目

x33g5p2x  于2022-02-14 转载在 Vue.js  
字(2.6k)|赞(0)|评价(0)|浏览(620)

1 介绍

  • CLI是Command-Line Interface,翻译为命令行界面,但是俗称脚手架。
  • Vue CLI是一个官方发布vue.js项目脚手架
  • 使用vue-cli可以快速搭建vue开发环境以及对应的webpack配置。
  • 使用vue-cli可以快速搭建vue开发环境以及对应的webpack配置

2 Vue CLI使用前提 Webpack

Vue.js官方脚手架工具就使用了webpack模板

  • 对所有的资源会压缩等优化操作
  • 它在开发的过程中提供了一套完整的功能,能够使得我们开发过程变得高效

Webpack全局安装

  1. npm install webpack -g

3 Vue CLI安装

https://cli.vuejs.org/zh/guide/

3.1 安装脚手架3.x

安装Vue脚手架(全局)

  1. # 脚手架3.x(后面拉一个模板就能用2)
  2. npm install -g @vue/cli

注意:上面安装的是Vue CLI3的版本,如果需要按照Vue CLI2的方式初始化项目时不可以的

查看版本

  1. vue --version

Vue CLI3.x初始化项目

  1. vue create my-project

3.2 拉取脚手架2.x

拉取脚手架2.x官方教程

  1. npm install -g @vue/cli-init
  2. # `vue init` 的运行效果将会跟 `vue-cli@2.x` 相同
  3. vue init webpack my-project

Vue CLI2初始化项目

  1. vue init webpack my-project

4 常用命令

打包项目

  1. npm run build

运行项目

  1. npm run serve

6 其他常用文件

.editorconfig

  1. # 编辑器配置
  2. root = true
  3. [*]
  4. charset = utf-8
  5. indent_style = space
  6. indent_size = 2
  7. end_of_line = lf
  8. insert_final_newline = true
  9. trim_trailing_whitespace = true

.eslintrc

  1. // https://eslint.org/docs/user-guide/configuring
  2. // eslint是用来管理和检测js代码风格的工具,可以和编辑器搭配使用,
  3. // 如vscode的eslint插件 当有不符合配置文件内容的代码出现就会报错或者警告
  4. module.exports = {
  5. root: true,
  6. parserOptions: {
  7. parser: 'babel-eslint'
  8. },
  9. env: {
  10. browser: true,
  11. },
  12. extends: [
  13. // https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention
  14. // consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules.
  15. 'plugin:vue/essential',
  16. // https://github.com/standard/standard/blob/master/docs/RULES-en.md
  17. // 'standard'
  18. ],
  19. // required to lint *.vue files
  20. plugins: [
  21. 'vue'
  22. ],
  23. // add your custom rules here
  24. rules: {
  25. "no-unused-vars": 'off', // 关掉语法检查
  26. // allow async-await
  27. 'generator-star-spacing': 'off',
  28. // allow debugger during development
  29. 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
  30. }
  31. }

5 实战

  • "vue": "^2.6.11"( package.json中 )
  • @vue/cli 4.5.15( vue -V 查看 )

创建项目

  1. vue create my-project

修改 package.json

  1. "dependencies": {
  2. // 修改下面
  3. "vue": "^2.6.11",
  4. },
  5. "devDependencies": {
  6. // 修改下面
  7. "vue-template-compiler": "^2.6.11"
  8. }

修改main.js

  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. import router from './router'
  4. // 饿了么
  5. import ElementUI from 'element-ui';
  6. import 'element-ui/lib/theme-chalk/index.css';
  7. Vue.use(ElementUI);
  8. Vue.config.productionTip = false
  9. new Vue({
  10. router,
  11. render: h => h(App),
  12. }).$mount('#app')

router/index.js

  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. // import Test from "../views/Test";
  4. Vue.use(VueRouter)
  5. const routes = [
  6. // component: () => import('../views/Ajax.vue')
  7. {
  8. path: '/',
  9. name: 'Test',
  10. component: () => import('../views/Test.vue')
  11. }
  12. ]
  13. const router = new VueRouter({
  14. // mode: 'history',
  15. base: process.env.BASE_URL,
  16. routes
  17. })
  18. export default router

7 第三方安装

vue-router

  1. npm install --save vue-router

axios

  1. npm install --save axios

饿了么

  1. npm i element-ui -S
  2. npm install --save element-ui

echarts
Vue引用echarts图表

  1. npm install echarts --save

原文:https://www.920vip.net/article/162

相关文章

最新文章

更多