Skip to content

VUE3 的安装

备注

在 VUE 里面安装方式有两种

1.第一种的安装方式的打包工具是 webpack

2.第二种安装方式的打包工具是 vite(vite 不支持 vue2)

Webpack 安装方式

先安装脚手架

bash

npm i @vue/cli -g
or
yarn global add @vue/cli

创建项目

bash

vue create 项目名称(必须小写)

正式安装步骤

bash
Vue CLI v3.3.0
? Please pick a preset: (Use arrow keys)
> default (babel, eslint)
  Manually select features

它的意思是选择默认还是手动选择

空格表示选择,回车表示下一步

bash

Vue CLI v3.3.0
? Please pick a preset: Manually select features
? Check the features needed for your project: (Press <space> to select, <a> to toggle all, <i> to invert selection)
>(*) Babel
 ( ) TypeScript
 ( ) Progressive Web App (PWA) Support
 (*) Router
 (*) Vuex
 (*) CSS Pre-processors
 (*) Linter / Formatter
 ( ) Unit Testing
 ( ) E2E Testing

让你选择是 vue2 还是 vue3

bash
Choose a version of Vue.js that you want to start the project with (Use arrow keys)
> 3.x
  2.x

模块安装询问 因为安装了 router 所以会弹出 Y

js

Vue CLI v3.3.0
? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, Router, Vuex, CSS Pre-processors, Linter
? Use history mode for router? (Requires proper server setup for index fallback in production) (Y/n)

后面还有几步我就不列举了 发个汇总版本的

bash

Vue CLI v3.3.0
? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, Router, Vuex, CSS Pre-processors, Linter
? Use history mode for router? (Requires proper server setup for index fallback in production) Yes
? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Less
? Pick a linter / formatter config: Prettier
? Pick additional lint features: (Press <space> to select, <a> to toggle all, <i> to invert selection)Lint on save
? Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? In package.json
? Save this as a preset for future projects? (y/N) N //是否记录下,以便下次继续使用这套配置

安装最后一步

js

cd demo1  // 进入项目目录 这里demo1就是你一上来create 那个名称
npm run serve  // 这里和2不一样了。2是run dev 而 3 是run serve

DONE  Compiled successfully in 2232ms                                                                                                                                                                        App running at:
  - Local:   http://localhost:8080/
  - Network: http://192.168.0.8:8080/

  Note that the development build is not optimized.
  To create a production build, run npm run build.

根目录下新建 vue.config.js 里面的配置

bash

// vue.config.js 配置说明
//官方vue.config.js 参考文档 https://cli.vuejs.org/zh/config/#css-loaderoptions
// 这里只列一部分,具体配置参考文档
module.exports = {
  //别名设置
  configureWebpack: {
    resolve: {
      alias: {
        assets: "@/assets",
        components: "@/components",
        views: "@/views",
      },
    },
  },
  // 部署生产环境和开发环境下的URL。
  // 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
  //例如 https://www.my-app.com/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.my-app.com/my-app/,则设置 baseUrl /my-app/。
  baseUrl: process.env.NODE_ENV === "production" ? "./" : "/",

  // outputDir: 在npm run build yarn build ,生成文件的目录名称(要和baseUrl的生产环境路径一致)
  outputDir: "dist",
  //用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下)
  assetsDir: "assets",
  //指定生成的 index.html 的输出路径  (打包之后,改变系统默认的index.html的文件名)
  // indexPath: "myIndex.html",
  //默认情况下,生成的静态资源在它们的文件名中包含了 hash 以便更好的控制缓存。你可以通过将这个选项设为 false 来关闭文件名哈希。(false的时候就是让原来的文件名不改变)
  filenameHashing: false,

  //   lintOnSave:{ type:Boolean default:true } 问你是否使用eslint
  lintOnSave: true,
  //如果你想要在生产构建时禁用 eslint-loader,你可以用如下配置
  // lintOnSave: process.env.NODE_ENV !== 'production',

  //是否使用包含运行时编译器的 Vue 构建版本。设置为 true 后你就可以在 Vue 组件中使用 template 选项了,但是这会让你的应用额外增加 10kb 左右。(默认false)
  // runtimeCompiler: false,

  /**
   * 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
   *  打包之后发现map文件过大,项目文件体积很大,设置为false就可以不输出map文件
   *  map文件的作用在于:项目打包后,代码都是经过压缩加密的,如果运行时报错,输出的错误信息无法准确得知是哪里的代码报错。
   *  有了map就可以像未加密的代码一样,准确的输出是哪一行哪一列有错。
   * */
  productionSourceMap: false,

  // 它支持webPack-dev-server的所有选项
  devServer: {
    /* 这里有个前提必须引入文件
     const appData = require("./data.json");
     const seller = appData.seller;
     const goods = appData.goods;
     const ratings = appData.ratings;
    */
    before(app) {
      app.get("/api/seller", function (req, res) {
        res.json({
          errno: 0,
          data: seller,
        });
      });
      app.get("/api/goods", function (req, res) {
        res.json({
          errno: 0,
          data: goods,
        });
      });
      app.get("/api/ratings", function (req, res) {
        res.json({
          errno: 0,
          data: ratings,
        });
      });
    },
    host: "localhost", //也可以直接写IP地址这样方便真机测试
    port: 8080, // 端口号
    https: false, // https:{type:Boolean}
    open: true, //配置自动启动浏览器
    // proxy: 'http://localhost:4000' // 配置跨域处理,只有一个代理

    // 配置多个代理
    proxy: {
      "/api": {
        target: "<url>", //写地址
        ws: true, // 允许跨域
        changeOrigin: true, //允许跨域
        pathRewrite: {
          "^/api": "",
        },
      },
      "/foo": {
        target: "<other_url>",
      },
    },
  },
};

修改配置在 package.json 里面

  • 增加了 rules 选项
js
"eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "@vue/prettier"
    ],
    "rules": {
      "no-console": "off"
    },
    "parserOptions": {
      "parser": "babel-eslint"
    }
  },

vite 的安装方式

命令安装

bash

npm init vue@latest

下一步开始选择

bash

 Project name: <your-project-name>
 Add TypeScript? No / Yes
 Add JSX Support? No / Yes
 Add Vue Router for Single Page Application development? No / Yes
 Add Pinia for state management? No / Yes
 Add Vitest for Unit testing? No / Yes
 Add Cypress for both Unit and End-to-End testing? No / Yes
 Add ESLint for code quality? No / Yes
 Add Prettier for code formatting? No / Yes

安装依赖包并启动

如果不确定是否开启某个功能 ,你可以直接按下回车键选择 No,在项目被创建后,通过一下步骤启动

bash
> cd <your-project-name>
> npm install
> npm run dev