Sivo 悉见

vue.cli脚手架初次使用图文教程

匿名 · 更新于 2018/7/22

vue-cli作用

vue-cli作为vue的脚手架,可以帮助我们在实际开发中自动生成vue.js的模板工程。

vue-cli使用

!!前提:需要vue和webpack

  1. 安装全局vue-cli

    <pre class="hljs avrasm" style="box-sizing: border-box; overflow: auto; font-family: &quot;Source Code Pro&quot;, Consolas, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 0.93em; padding: 1em; margin-bottom: 1.5em; line-height: 1.45; word-break: break-all; background: url(&quot;../img/blueprint.png&quot;) 0% 0% / 30px, 0% 0% / 30px rgb(246, 246, 246); border: none; border-radius: 4px; max-height: 35em; position: relative; margin-top: 0px !important;">
    

    npm install vue-cli -g

  2. 初始化,生成项目模板(my_project是项目名,自己随意)

    <pre class="hljs nginx" style="box-sizing: border-box; overflow: auto; font-family: &quot;Source Code Pro&quot;, Consolas, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 0.93em; padding: 1em; margin-bottom: 1.5em; line-height: 1.45; word-break: break-all; background: url(&quot;../img/blueprint.png&quot;) 0% 0% / 30px, 0% 0% / 30px rgb(246, 246, 246); border: none; border-radius: 4px; max-height: 35em; position: relative; margin-top: 0px !important;">
    

    vue init webpack my_project

  3. 进入生成的项目文件夹

    <pre class="hljs bash" style="box-sizing: border-box; overflow: auto; font-family: &quot;Source Code Pro&quot;, Consolas, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 0.93em; padding: 1em; margin-bottom: 1.5em; line-height: 1.45; word-break: break-all; background: url(&quot;../img/blueprint.png&quot;) 0% 0% / 30px, 0% 0% / 30px rgb(246, 246, 246); border: none; border-radius: 4px; max-height: 35em; position: relative; margin-top: 0px !important;">
    

    cd my_project

  4. 初始化,安装依赖

    <pre class="hljs cmake" style="box-sizing: border-box; overflow: auto; font-family: &quot;Source Code Pro&quot;, Consolas, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 0.93em; padding: 1em; margin-bottom: 1.5em; line-height: 1.45; word-break: break-all; background: url(&quot;../img/blueprint.png&quot;) 0% 0% / 30px, 0% 0% / 30px rgb(246, 246, 246); border: none; border-radius: 4px; max-height: 35em; position: relative; margin-top: 0px !important;">
    

    npm install

安装完成,目录树:

图片描述

run:

npm run dev

浏览器会自动打开到http://localhost:8080/#/ ,会看到欢迎页:

图片描述

上面是在本地运行,服务器上运行:

npm run build

生成静态文件:

打开dist文件

打开dist文件夹下新生成的index.html文件,会发现页面空白,打开控制台会发现页面中引用的css和js文件都找不到:
图片描述

说明引用路径错了,需要手动修改:

进入config/index.js

原配置中的引用路径是’/’(根目录):
图片描述

修改为’./’(当前目录):
图片描述

run:

npm run build

Done:
图片描述