美文网首页vue程序vue教程
(1)安装部署vuecli3.0

(1)安装部署vuecli3.0

作者: 我拥抱着我的未来 | 来源:发表于2019-01-14 11:21 被阅读473次

    本节知识点

    (1) 通过官方文档查看vuecli3.0安装
    (2) 通过自定义来安装vuecli3.0

    (一) 通过官方文档来安装vuecli3.0

    在这之前请电脑上安装好node
    中文文档请移步到这里:点击我跳转到官方文档

    (二)通过自定义方式来安装

    (0) 使用之前要是网速不给力。请使用淘宝镜像这样在安装使用cnpm来安装,为了方便起见我下面的安装步骤没有使用cnpm而用的npm
    npm install -g cnpm --registry=https://registry.npm.taobao.org
    
    (1) 使用vuecli3.0
    npm i @vue/cli -g
    or
    yarn global add @vue/cli
    

    安装完以后可以查看版本 注意-V需要大写

    PS D:\工作\代码\VueAll\vue3.0Test> vue -V
    3.3.0
    

    要是想查看其他命令可以使用

    PS D:\工作\代码\VueAll\vue3.0Test> vue -h
    Usage: vue <command> [options]
    
    Options:
      -V, --version                              output the version number
      -h, --help                                 output usage information
    
    Commands:
      create [options] <app-name>                create a new project powered by vue-cli-service  // 创建一个由vue-cli-service 支持的新项目
      add [options] <plugin> [pluginOptions]     install a plugin and invoke its generator in an already created project   // 在已有的项目中添加插件
      invoke [options] <plugin> [pluginOptions]  invoke the generator of a plugin in an already created project // 在开发者模式下零配置运行一个js或vue文件
      inspect [options] [paths...]               inspect the webpack config in a project with vue-cli-service   // 在vue-cli-service的项目中检查webpack配置 
      serve [options] [entry]                    serve a .js or .vue file in development mode with zero config  // 可以理解为开发环境下启动命令
      build [options] [entry]                    build a .js or .vue file in production mode with zero config   // 打包 (在生产模式下零配置构建一个js或者vue文件)
      ui [options]                               start and open the vue-cli ui  // vue3.0提供的一个UI 
      init [options] <template> <app-name>       generate a project from a remote template (legacy API, requires @vue/cli-init)  // // 旧api 需要@vue/cli-init // 就是原来的vue-cli init <template> <app-name>
      config [options] [value]                   inspect and modify the config // 项目配置文件
      upgrade [semverLevel]                      upgrade vue cli service / plugins (default semverLevel: minor) // 更新插件或者配置文件
      info                                       print debugging information about your environment  // 打印出来vue配置文件
    
      Run vue <command> --help for detailed usage of given command.
    
    (2) 创建一个项目
    vue create 项目名称(必须小写)
    

    这个时候你会看到

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

    键盘上下选择(默认)还是手动(Manually) 要是选择default那就一路回车就行。我在这里选择手动

    Vue CLI v3.3.0
    ? Please pick a preset:
      default (babel, eslint)
    > Manually select features
    
    (3)这个时候你会看见一些配置供你选择
    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
    

    需求不一样,配置不一样 我在这里选择了这几个

    Vue CLI v3.3.0
    ? Please pick a preset: Manually select features
    ? Check the features needed for your project:
     (*) Babel
     ( ) TypeScript
     ( ) Progressive Web App (PWA) Support
     (*) Router
     (*) Vuex
    >(*) CSS Pre-processors
     (*) Linter / Formatter
     ( ) Unit Testing
     ( ) E2E Testing
    
    (4) 模块安装询问

    (i)我因为安装了router所以会弹出来

    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)
    

    选择Y

    以下我就不列举了。最后到这里要说明下

    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 // 保存就检测 (我一般选择它)
     ( ) Lint and fix on commit // fix和commit检查
    

    我在这里选择了router ,Less ,ESlint_Prettier作为代码检查

    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.? (Use arrow keys)
    > In dedicated config files  // 独立文件夹
      In package.json  // 放入package.json里 (我一般选择它)
    

    这里他会问你把babel .eslint这些文件放在独立的文件里还是package.json里

    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 //是否记录下,以便下次继续使用这套配置
    

    这里我选择N 不记录

    (5)确定后等待下载依赖模块
    Vue CLI v3.3.0
    ✨  Creating project in D:\工作\代码\VueAll\vue3.0Test\demo1.
    🗃  Initializing git repository...
    ⚙  Installing CLI plugins. This might take a while...
    
    
    > yorkie@2.0.0 install D:\工作\代码\VueAll\vue3.0Test\demo1\node_modules\yorkie
    > node bin/install.js
    
    setting up Git hooks
    done
    
    added 1114 packages from 746 contributors in 14.999s
    🚀  Invoking generators...
    📦  Installing additional dependencies...
    
    added 48 packages from 93 contributors, updated 2 packages and moved 7 packages in 6.754s
    ⚓  Running completion hooks...
    
    📄  Generating README.md...
    
    🎉  Successfully created project demo1.
    👉  Get started with the following commands:
    
     $ cd demo1
     $ npm run serve
    
    PS D:\工作\代码\VueAll\vue3.0Test>
    

    这样表示安装成功

    (6)安装后移动到目录 并启动
      cd demo1  // 进入项目目录 这里demo1就是你一上来create 那个名称
      npm run serve  // 这里和2不一样了。2是run dev 而 3 是run serve
    
     DONE  Compiled successfully in 2232ms                                                                                                                                                                                                                                                                        10:27:38
      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.
    
    9.png

    安装成功

    (7) 查看下目录结构
    1.png
    (8) 解决跨域问题

    在2里面要解决跨域问题必然会修改proxyTable
    在3里面则需要修改端口,在根目录下创建vue.config.js
    vue.config.js相关配置请参考点击我跳转到vuecli3.0相关配置

    // 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: {
        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>"
          }
        }
      }
    };
    
    
    (9)vuecli3 增加的内容
    (1) GUI 图形化界面

    这样可以增加插件,添加配置 方便管理

    vue ui 
    
    (2) 个人认为最重要的就是

    这个命令就是实现单个VUE文件的展示。以前要是写.vue文件必须依赖脚手架来查看最后的效果
    现在敲这个命令 他会让你安装一个包。安装完以后。cd到这个文件的目录下。然后敲这个命令就能实现单个文件的查看

    vue serve  xxx.vue(单个vue名称)
    
    (3) .env实现全局变量

    在项目的根目录下新建一个文件.env文件然后里面写 名字URL可以随便起但是前面的VUE_APP不能更改

    VUE_APP_URL(你自己起的名字) = http://www.baidu.com
    

    然后项目中要使用这个变量的话只有在data里面获取这个变量,这样就获取到了

    data(){
      return{
       url:process.env.VUE_APP_URL
    }
    }
    

    相关文章

      网友评论

        本文标题:(1)安装部署vuecli3.0

        本文链接:https://www.haomeiwen.com/subject/lmzrdqtx.html