1、先安裝好vue-cli,如果還沒有安裝好的可以參考:《windows下vue-cli及webpack 構(gòu)建網(wǎng)站(一)環(huán)境安裝》
2、安裝好之后Vue的歡迎界面,我們要做個(gè)例子導(dǎo)入bootstrap的樣式,就先去復(fù)制一份bootstrap的導(dǎo)航樣式過來替換掉Vue的歡迎頁面吧,打開Src文件夾下面的components組件目錄,然后打開 Hello.vue文件,把
  <h1>{{ msg }}</h1>  <h2>Essential Links</h2>  <ul>   <li><a  rel="external nofollow" target="_blank">Core Docs</a></li>   <li><a  rel="external nofollow" target="_blank">Forum</a></li>   <li><a  rel="external nofollow" target="_blank">Gitter Chat</a></li>   <li><a  rel="external nofollow" target="_blank">Twitter</a></li>   <br>   <li><a  rel="external nofollow" target="_blank">Docs for This Template</a></li>  </ul>  <h2>Ecosystem</h2>  <ul>   <li><a  rel="external nofollow" target="_blank">vue-router</a></li>   <li><a  rel="external nofollow" target="_blank">vuex</a></li>   <li><a  rel="external nofollow" target="_blank">vue-loader</a></li>   <li><a  rel="external nofollow" target="_blank">awesome-vue</a></li>  </ul> </div></template>內(nèi)容改成
<template> <nav class="navbar navbar-default" role="navigation">  <div class="container-fluid">   <!-- Brand and toggle get grouped for better mobile display -->   <div class="navbar-header">    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">     <span class="sr-only">Toggle navigation</span>     <span class="icon-bar"></span>     <span class="icon-bar"></span>     <span class="icon-bar"></span>    </button>    <a class="navbar-brand" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ msg }}</a>   </div>   <!-- Collect the nav links, forms, and other content for toggling -->   <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">    <ul class="nav navbar-nav">     <li class="active"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Link</a></li>     <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Link</a></li>     <li class="dropdown">      <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>      <ul class="dropdown-menu" role="menu">       <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Action</a></li>       <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Another action</a></li>       <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Something else here</a></li>       <li class="divider"></li>       <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Separated link</a></li>       <li class="divider"></li>       <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >One more separated link</a></li>      </ul>     </li>    </ul>    <form class="navbar-form navbar-left" role="search">     <div class="form-group">      <input type="text" class="form-control" placeholder="Search">     </div>     <button type="submit" class="btn btn-default">Submit</button>    </form>    <ul class="nav navbar-nav navbar-right">     <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Link</a></li>     <li class="dropdown">      <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>      <ul class="dropdown-menu" role="menu">       <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Action</a></li>       <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Another action</a></li>       <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Something else here</a></li>       <li class="divider"></li>       <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Separated link</a></li>      </ul>     </li>    </ul>   </div><!-- /.navbar-collapse -->  </div><!-- /.container-fluid --> </nav></template>這時(shí)候運(yùn)行vue 顯示的頁面變成:

現(xiàn)在我們加入的bootstrap導(dǎo)航代碼已經(jīng)可以顯示出來了,但是有點(diǎn)丑,是因?yàn)槲覀冞€沒把bootstrap的CSS文件加載過來,接下來我們就是要導(dǎo)入CSS文件和JS文件了。
3、下載Bootstrap文件包,然后把css、js、fonts三個(gè)文件夾復(fù)制到vue項(xiàng)目的src/assets目錄下。

4、讓vue支持jQuery需要先安裝jquery插件,通過cmd命令進(jìn)入項(xiàng)目文件夾,然后運(yùn)行 cnpm install jquery --save-dev 安裝插件

接著分別運(yùn)行
cnpm install style-loader --save-dev cnpm install css-loader --save-dev cnpm install file-loader --save-dev
安裝支持css的插件。

5、修改build文件夾下面的webpack.base.conf.js文件,讓其支持外部的css和js,首先打開文件后在頭部加入:
var webpack = require('webpack')然后在
module.exports = { entry: {  app: './src/main.js' },后面加
plugins: [ new webpack.ProvidePlugin({  $: "jquery",  jQuery: "jquery",  "windows.jQuery": "jquery" })],把
alias: { 'vue$': 'vue/dist/vue.common.js', 'src': path.resolve(__dirname, '../src'), 'assets': path.resolve(__dirname, '../src/assets'), 'components': path.resolve(__dirname, '../src/components')}改成
alias: { 'vue$': 'vue/dist/vue.common.js', 'src': path.resolve(__dirname, '../src'), 'assets': path.resolve(__dirname, '../src/assets'), 'components': path.resolve(__dirname, '../src/components'), jquery: "jquery/src/jquery"}保存文件
6、接著修改 src文件夾下面的main.js文件,打開文件之后在頂部加入
import './assets/css/bootstrap.min.css'import './assets/js/bootstrap.min'
接著保存之后重啟一下服務(wù) npm run dev
如果提示錯(cuò)誤

說明bootstrap.js文件格式不匹配不能通過webpack的格式檢查,這樣我們就需要在webpack的配置文件里面把js文件設(shè)置成不檢查格式,打開build文件夾下的webpack.base.conf.js文件,找到 preLoaders 下面的
{ test: //.js$/, loader: 'eslint', include: projectRoot, exclude: /node_modules/}代碼,修改成
{ test: //.js$/, loader: 'eslint', include: projectRoot, exclude: [/node_modules/,/js/]}接著保存之后運(yùn)行npm  run dev
就可以看到導(dǎo)航的效果出來了

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答