vue cli引用jquery(cli3.0版本前)

博客介绍了在Vue CLI中引用jQuery、Swiper和Bootstrap的方法。对于jQuery,需注意vue-cli3.0配置文件变化,要安装依赖、在特定文件添加代码并在组件页面引用;引用Swiper要安装依赖并在组件内操作;引用Bootstrap则安装依赖后在main.js文件引入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue cli引用jquery(cli3.0版本前)

vue cli引用jquery

注:vue-cli3.0 没有了 webpack.config.js 配置文件,取而代之的是集合在 vue.config.js文件 内进行配置

1.安装jquery依赖

npm install jquery -S

2.在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 = {
  plugins: [                                                     //添加这一块代码
    new webpack.ProvidePlugin({                                  //添加这一块代码
      jQuery: "jquery",                                          //添加这一块代码
      $: "jquery"                                                //添加这一块代码
    })                                                           //添加这一块代码
  ],                                                             //添加这一块代码
  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
  },

3.在组件页面内引用

<template>
  <div id="apps">点击</div>
</template>

<script>
import $ from "jquery";                   //引用jquery
export default {
  name: "Index",
  components: {
    Header,
    Footer
  },
  data() {
    return {};
  },
  mounted() {                                                //jQuery事件写在mounted()周期函数内才有效
    $("#apps").on("click", function() {
      console.log("引入成功");
    });
  },
  methods: {}
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

vue cli引用swiper

1.安装swiper 依赖

npm install swiper -S

2.在组件内

<template>
  <div id="Header">
    <div class="swiper-container">
      <div class="swiper-wrapper">
        <div class="swiper-slide" style="height:550px;margin-top:90px;">
          <img src="../../../static/lib/image/banner3.png" alt style="width:100%;height:100%;">
        </div>
        <div class="swiper-slide" style="height:550px;margin-top:90px;">
          <img src="../../../static/lib/image/banner4.png" alt style="width:100%;height:100%;">
        </div>
      </div>
      <!-- Add Arrows -->
      <div class="swiper-button-next left carousel-control"></div>              //按钮
      <div class="swiper-button-prev"></div>                                          //按钮
    </div>
  </div>
</template>

<script>
import Swiper from "swiper";                                         //引入swiper.js
import 'swiper/dist/css/swiper.css'                                 //引入swiper.css
export default {
  name: "Header",
  data() {
    return {};
  },
  mounted() {                                                                  // 声明必须写在mounted()周期函数内
    var swiper = new Swiper(".swiper-container", {
      loop: true,
      autoplay: true,
      navigation: {
        nextEl: ".swiper-button-next",
        prevEl: ".swiper-button-prev"
      }
    });
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>             //添加swiper样式
.swiper-container {
  width: 100%;
  height: 100%;
}

.swiper-slide {
  height: 400px;
  margin: 40px auto;
  text-align: center;
  font-size: 18px;
  /* Center slide text vertically */
  display: -webkit-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
}

.swiper-pagination-bullet {
  width: 20px;
  height: 20px;
  text-align: center;
  line-height: 20px;
  font-size: 12px;
  color: #000;
  opacity: 1;
  background: rgba(0, 0, 0, 0.2);
}

.swiper-pagination-bullet-active {
  color: #fff;
  background: #ffb72b;
}
</style>

vue cli引用bootstrap

1.安装bootstrap 依赖

npm install bootstrap3 -S

2.然后在main.js文件中引入bootstrap

import Vue from 'vue'
import App from './App'
import router from './router'

import $ from 'jquery'
import "bootstrap3/dist/css/bootstrap.css"
import "bootstrap3/dist/js/bootstrap.js"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值