如何在vue中引入第三方jquery,swiper等库(一)

本文详细介绍了如何在项目中引入Swiper轮播库及jQuery库,并提供了多种引入方式,包括全局引入、局部引入等。此外,还给出了具体示例代码帮助理解。

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

以前习惯性是引用jQuery,bootstrap。。。库,但是不会引用,怎么办?

npm install swiper--save-dev (或者 npm install jquery -D)
  
  • 1

一 .引入swiper(全局,局部)

  1. 方法一:全局引入,也是最暴力的,但是也是有好处坏处(同时加载,但是不能保证同时下载)
<link href="https://cdn.bootcss.com/Swiper/3.4.2/css/swiper.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/Swiper/3.4.2/js/swiper.min.js"></script>
 
  • 1
  • 2

组件中可以直接使用的swiper了

 _initSwiper() {
        const container = this.$refs.swiper;
        const config = {
          effect: 'coverflow',
          slidesPerView: 'auto',
          centeredSlides: true,
          initialSlide: this.activeIndex,
          loop: true,
          autoplay: 1000,
          speed: 1000,
          coverflow: {
            rotate: 0,
            stretch: -30,
            depth: 100,
            modifier: 0.7,
            slideShadows: false,
          },
        };
    this.mySwiper = new Swiper(container, config);
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

2.方法二:main.js 中

import '../node_modules/swiper/dist/css/swiper.min.css';
import 'swiper';
 
  • 1
  • 2

执行上面的_initSwiper()的方法 即可

3.方法三:局部的引入的,有时只想的单个组件中使用某一个的库,方法如下

<section ref="swiper" class="swiper-container">
  <div class="swiper-wrapper">
     <div class="demo swiper-slide" v-for="item in colorList" :style="`backgroundColor:${item}`"></div>
      </div>
</section>



<script>
  let swiperAsync = import('swiper') //引入的swiper.js(node_modules)的方法
  export default {
  data(){
      return {
        colorList: ['red', 'yellow', 'gray', 'pink']
      }
    },
    methods: {
      async _initSwiper() {
        let Swiper = await swiperAsync; //异步加载的
        const container = this.$refs.swiper; //ref='swiper'
        const config = {  //swiper的参数配置
          effect: 'coverflow',
          slidesPerView: 'auto',
          centeredSlides: true,
          initialSlide: this.activeIndex,
          loop: true,
          autoplay: 1000,
          speed: 1000,
          coverflow: {
            rotate: 0,
            stretch: -30,
            depth: 100,
            modifier: 0.7,
            slideShadows: false,
          },
        };
        this.mySwiper = new Swiper(container, config);
      },
    },
    mounted(){
      this._initSwiper();
    }
  }
</script>

<style lang="scss" scoped>
  /*@import '../assets/styles/swiper.min.css'; !*静态资源的文件*!*/
  @import '../../node_modules/swiper/dist/css/swiper.min.css';
</style>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

二 引入的jquery的方法

  1. 全局的方法
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
 
  • 1

组件中可以直接使用的 ‘$’的方法

  1. 局部的方法:
npm install jquery -D
 
  • 1

需要使用的组件中引入

import $ from 'jquery'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值