前言

最近有小伙伴问道如何在vue-cli项目中引入第三方插件或者库,例如如果想在项目中使用jQuery中的Ajax请求数据呢?或者我想使用Bootstrap框架呢?等等这些问题,本篇博客将带你学习如何引入第三方插件或者库(仅仅只是一部分,如果没有您想要的可以自行百度),那么一起来看看吧!

本章目标

  • 学会vue-cli项目中引入jQuery

  • 学会vue-cli项目中引入Bootstrap

vue-cli项目中引入jQuery和Bootstrap

首先我们需要引入的是jQuey这个库,毕竟作为一名前端开发人员,我们经常使用jQuey中的ajax请求数据,但是学完本篇博客你可以使用另一种方法请求数据,就是下文提到的axios,这个相对于jQuey中的ajax来说是相对好用的。

(1)添加依赖并安装依赖

项目根目录下找到package.json 添加

"bootstrap": "^3.3.6",
"jquery": "^2.1.4",

版本可以根据自己的需要修改

安装命令

cnpm install
npm install

安装完成之后我们去node_modules查看是否安装成功,安装成功之后的结果

(2)导入jQuey和Bootstrap

在main.js 导入 (注意导入是node_modules下的路径可以点进去查看具体位置)min是压缩后文件建议导入这个

import 'jquery/dist/jquery.min.js'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap/dist/js/bootstrap.min.js'

main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import store from './store/index'
// import router from './router'
// import router from './router/hello'
// import router from './router/test'
// import router from './router/common'
// import router from './router/one'
import router from './router/two'
import 'jquery/dist/jquery.min'
import 'bootstrap/dist/js/bootstrap.min'
import 'bootstrap/dist/css/bootstrap.min.css'
Vue.config.productionTip = false
Vue.use(Vuex)
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
components: {},
template: ''
})

使用这种方法引入

(3)使用内置插件ProvidePlugin自动加载模块

此时jQuery并未依赖成功,将提示错误:

需在build/webpack.base.conf.js中增加插件配置

const webpack = require('webpack')

配置中添加

plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"windows.jQuery": "jquery"
})
],

build下webpack.base.conf.js的完整结果

 'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')
const webpack =require('webpack')
function resolve (dir) {
return path.join(__dirname, '..', dir)
} module.exports = {
context: path.resolve(__dirname, '../'),
entry: {
app: './src/main.js'
},
output: {
path: config.build.assetsRoot,
filename: '[name].js',
publicPath: process.env.NODE_ENV === 'production'
? config.build.assetsPublicPath
: config.dev.assetsPublicPath
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
}
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"windows.jQuery": "jquery"
})
],
node: {
// prevent webpack from injecting useless setImmediate polyfill because Vue
// source contains it (although only uses it if it's native).
setImmediate: false,
// prevent webpack from injecting mocks to Node native modules
// that does not make sense for the client
dgram: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty'
}
}

(4)使用jQuery与Bootstrap

Bootstrap资料

Bootstrap中文网:https://www.bootcss.com/

菜鸟教程:https://www.runoob.com/bootstrap/bootstrap-tutorial.html

配置好之后我们就直接使用jQuey和Bootstrap

自己可以新建一个组件中使用jQuery相关方法和Bootstrap相关资源

IndexComponent.vue

<template>
<div>
<h1>你好</h1>
<button @click="changeMsg">改变消息</button>
<ul class="nav nav-tabs">
<li role="presentation" class="active"><a href="/">首页</a></li>
<li role="presentation"><a href="#/a">组件A</a></li>
<li role="presentation"><a href="#/b">组件B</a></li>
<li role="presentation"><a href="#/c">组件C</a></li>
</ul>
</div>
</template> <script>
export default {
name: "IndexComponent",
data(){
return{ }
},
methods:{
changeMsg(){
$('h1').text('我好')
}
} }
</script> <style scoped> </style>

结果:

vue-cli项目中引入第三方插件的更多相关文章

  1. 项目中整合第三方插件与SpringMVC数据格式化关于ip地址

    一.Bootstrap 响应式按钮 <div calss="col-sm-2"> <button class="btn btn-default btn- ...

  2. vue-cli创建的项目中引入第三方库报错 'caller', 'calle', and 'arguments' properties may not be...

    http://blog.csdn.net/sophie_u/article/details/76223978 以在vue中引入mui第三方库为例: 虽然针对vue,有单独的vue-mui库可以使用,但 ...

  3. vue-cli创建的项目中引入第三方库报错'caller', 'calle', and 'arguments' properties may not be...

    本文链接:https://blog.csdn.net/Sophie_U/article/details/76223978 问题: 在vue的main.js中引入mui.min.js时,报错. 如上,单 ...

  4. spring boot:在项目中引入第三方外部jar包集成为本地jar包(spring boot 2.3.2)

    一,为什么要集成外部jar包? 不是所有的第三方库都会上传到mvnrepository, 这时我们如果想集成它的第三方库,则需要直接在项目中集成它们的jar包, 在操作上还是很简单的, 这里用luos ...

  5. vue项目中引入第三方框架

    element-ui npm install element-ui -- save; main.js中 import Element from 'element-ui'; import 'elemen ...

  6. Vuex内容解析和vue cli项目中使用状态管理模式Vuex

    中文文档:vuex官方中文网站 一.vuex里面都有些什么内容? const store = new Vuex.Store({ state: { name: 'weish', age: }, gett ...

  7. vue.cli项目中src目录每个文件夹和文件的用法

    assets文件夹是放静态资源:components是放组件:router是定义路由相关的配置:view视图:app.vue是一个应用主组件:main.js是入口文件:

  8. vue cli 项目中设置背景图

    https://blog.csdn.net/MoLvSHan/article/details/78204972 不能直接写成相对路径,像下面这这种就会报错 backgroundImage: " ...

  9. vue项目中的相关插件

    所有安装都是cd到该项目目录中安装 -S代表将插件添加到项目中的package.json文件 1.iview 是一套基于 Vue.js 的开源 UI 组件库,主要服务于 PC 界面的中后台产品 cnp ...

随机推荐

  1. 【Linux系列】配置Centos 7的软件源(二)

    目的 本文主要介绍以下内容: 设置centos的国内软件源,默认源都是国外的下载软件超级麻烦. ssh登录 下载一个shell或者cmder ssh root@192.168.10.18 #上篇设置的 ...

  2. PostGIS 查看表属性(字段、类型、是否为空)

     SELECT                a.attnum,                a.attname AS field,                t.typname AS type ...

  3. (一)OpenStack---M版---双节点搭建---基础环境配置

    ↓↓↓↓↓↓↓↓视频已上线B站↓↓↓↓↓↓↓↓ >>>>>>传送门 配置如下 本次搭建采用2台4核4G的虚拟机,也可以用2台2核4G 主机名 配置 网络 Contr ...

  4. scrapy结合selenium抓取武汉市环保局空气质量日报

    1.前言 目标网站:武汉市环境保护局(http://hbj.wuhan.gov.cn/viewAirDarlyForestWaterInfo.jspx).scrapy对接selenium模块抓取空气质 ...

  5. HTML,CSS,Javascript,PHP在网站结构中扮演的“角色”

    ①HTML   在网站结构中使用来,展示网页内容的: ②CSS   在网站结构中使用来,美化页面,如字体颜色大小等: ③Javascript(JS)   在网站结构中使用来,让页面动起来,提高浏览体验 ...

  6. wordpress 获取指定作者或者文章的所有评论数量

    wordpress 获取指定作者或者文章的所有评论数量 <?php $args = array( 'post_author' => '' // fill in post author ID ...

  7. SpringBoot第一次案例

    一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...

  8. HDFS原理概念扫盲

    1.概述 hdfs文件系统主要设计为了存储大文件的文件系统:如果有个TB级别的文件,我们该怎么存储呢?分布式文件系统未出现的时候,一个文件只能存储在个服务器上,可想而知,单个服务器根本就存储不了这么大 ...

  9. 新浪短网址最新api接口

    1,雨林短网址 网站链接:http://yldwz.cn 雨林短网址采用新浪.腾讯官方API接口,强大的多功能API,简单易用,质量高官 网提供强技术支持,99.9% SLA服务稳定安全可靠的校验机制 ...

  10. 【前端】之AJAX基础知识

    AJAX 简介 AJAX(Asynchronous JavaScript and XML),异步的JavaScript和XML AJAX不是编程语言,只是一种在无需重新加载整个网页的情况下能够更新部分 ...