详解windows下vue-cli及webpack 构建网站(二)导入bootstrap样式


Posted in Javascript onJune 17, 2017

1、先安装好vue-cli,如果还没有安装好的可以参考:《windows下vue-cli及webpack 构建网站(一)环境安装》

2、安装好之后Vue的欢迎界面,我们要做个例子导入bootstrap的样式,就先去复制一份bootstrap的导航样式过来替换掉Vue的欢迎页面吧,打开Src文件夹下面的components组件目录,然后打开 Hello.vue文件,把
   

<h1>{{ msg }}</h1>
  <h2>Essential Links</h2>
  <ul>
   <li><a href="https://vuejs.org" rel="external nofollow" target="_blank">Core Docs</a></li>
   <li><a href="https://forum.vuejs.org" rel="external nofollow" target="_blank">Forum</a></li>
   <li><a href="https://gitter.im/vuejs/vue" rel="external nofollow" target="_blank">Gitter Chat</a></li>
   <li><a href="https://twitter.com/vuejs" rel="external nofollow" target="_blank">Twitter</a></li>
   <br>
   <li><a href="http://vuejs-templates.github.io/webpack/" rel="external nofollow" target="_blank">Docs for This Template</a></li>
  </ul>
  <h2>Ecosystem</h2>
  <ul>
   <li><a href="http://router.vuejs.org/" rel="external nofollow" target="_blank">vue-router</a></li>
   <li><a href="http://vuex.vuejs.org/" rel="external nofollow" target="_blank">vuex</a></li>
   <li><a href="http://vue-loader.vuejs.org/" rel="external nofollow" target="_blank">vue-loader</a></li>
   <li><a href="https://github.com/vuejs/awesome-vue" rel="external nofollow" target="_blank">awesome-vue</a></li>
  </ul>
 </div>
</template>

内容改成

<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>

这时候运行vue 显示的页面变成:

详解windows下vue-cli及webpack 构建网站(二)导入bootstrap样式

现在我们加入的bootstrap导航代码已经可以显示出来了,但是有点丑,是因为我们还没把bootstrap的CSS文件加载过来,接下来我们就是要导入CSS文件和js文件了。

3、下载Bootstrap文件包,然后把css、js、fonts三个文件夹复制到vue项目的src\assets目录下。

详解windows下vue-cli及webpack 构建网站(二)导入bootstrap样式

4、让vue支持jQuery需要先安装jquery插件,通过cmd命令进入项目文件夹,然后运行 cnpm install jquery --save-dev 安装插件

详解windows下vue-cli及webpack 构建网站(二)导入bootstrap样式

接着分别运行

cnpm install style-loader --save-dev 
cnpm install css-loader --save-dev 
cnpm install file-loader --save-dev

  安装支持css的插件。

详解windows下vue-cli及webpack 构建网站(二)导入bootstrap样式

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'

接着保存之后重启一下服务 npm run dev

如果提示错误

详解windows下vue-cli及webpack 构建网站(二)导入bootstrap样式

说明bootstrap.js文件格式不匹配不能通过webpack的格式检查,这样我们就需要在webpack的配置文件里面把js文件设置成不检查格式,打开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/]
}

接着保存之后运行npm  run dev

就可以看到导航的效果出来了

详解windows下vue-cli及webpack 构建网站(二)导入bootstrap样式

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
showModelessDialog()使用详解
Sep 07 Javascript
javascript刷新父页面的各种方法汇总
Sep 03 Javascript
实现图片预加载的三大方法及优缺点分析
Nov 19 Javascript
基于BootStrap Metronic开发框架经验小结【三】下拉列表Select2插件的使用
May 12 Javascript
JavaScript检测原始值、引用值、属性
Jun 20 Javascript
有关JS中的0,null,undefined,[],{},'''''''',false之间的关系
Feb 14 Javascript
浅谈 Vue v-model指令的实现原理
Jun 08 Javascript
微信小程序 转发功能的实现
Aug 04 Javascript
jQuery NProgress.js加载进度插件的简单使用方法
Jan 31 jQuery
node.js通过axios实现网络请求的方法
Mar 05 Javascript
详解微信小程序实现跑马灯效果(附完整代码)
Apr 29 Javascript
jquery-ui 进度条功能示例【测试可用】
Jul 25 jQuery
js简易版购物车功能
Jun 17 #Javascript
Webpack如何引入bootstrap的方法
Jun 17 #Javascript
Webpack执行命令参数详解
Jun 17 #Javascript
JS实现留言板功能
Jun 17 #Javascript
利用vscode编写vue的简单配置详解
Jun 17 #Javascript
JavaScript用二分法查找数据的实例代码
Jun 17 #Javascript
JS实现新建文件夹功能
Jun 17 #Javascript
You might like
php一些错误处理的方法与技巧总结
2013/08/10 PHP
PHP+javascript制作带提示的验证码源码分享
2014/05/28 PHP
PHP提交表单失败后如何保留已经填写的信息
2014/06/20 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十六)
2014/06/30 PHP
PHP闭包实例解析
2014/09/08 PHP
php操作xml入门之xml基本介绍及xml标签元素
2015/01/23 PHP
JavaScript中常见陷阱小结
2010/04/27 Javascript
js multiple全选与取消全选实现代码
2012/12/04 Javascript
jquery 实现窗口的最大化不论什么情况
2013/09/03 Javascript
javascript验证上传文件的类型限制必须为某些格式
2013/11/14 Javascript
JS实用的动画弹出层效果实例
2015/05/05 Javascript
jQuery实现自动调整字体大小的方法
2015/06/15 Javascript
JavaScript生成二维码图片小结
2015/12/27 Javascript
javascript self对象使用详解
2016/10/18 Javascript
webpack踩坑之路图片的路径与打包
2017/09/05 Javascript
微信小程序 简易计算器实现代码实例
2019/09/02 Javascript
HTML元素拖拽功能实现的完整实例
2020/12/04 Javascript
python实现端口转发器的方法
2015/03/13 Python
python使用urllib2提交http post请求的方法
2015/05/26 Python
简单掌握Python中glob模块查找文件路径的用法
2016/07/05 Python
python爬虫实现教程转换成 PDF 电子书
2017/02/19 Python
python处理csv中的空值方法
2018/06/22 Python
pyqt 实现QlineEdit 输入密码显示成圆点的方法
2019/06/24 Python
Python使用Pyqt5实现简易浏览器(最新版本测试过)
2020/04/27 Python
关于tensorflow softmax函数用法解析
2020/06/30 Python
HTML5 Canvas锯齿图代码实例
2014/04/10 HTML / CSS
Roxy俄罗斯官方网站:冲浪和滑雪板的一切
2020/06/20 全球购物
金融事务专业求职信
2014/04/25 职场文书
羽毛球比赛策划方案
2014/06/13 职场文书
社区娱乐活动方案
2014/08/21 职场文书
幼儿园小班班务总结
2015/08/03 职场文书
文明和谐家庭事迹材料(2016精选版)
2016/02/29 职场文书
python爬虫之selenium库的安装及使用教程
2021/05/23 Python
mysql配置SSL证书登录的实现
2021/09/04 MySQL
Python中 range | np.arange | np.linspace三者的区别
2022/03/22 Python
python中validators库的使用方法详解
2022/09/23 Python