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

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

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

一 .引入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>

组件中可以直接使用的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);
}

2.方法二:main.js 中

import '../node_modules/swiper/dist/css/swiper.min.css';
import 'swiper';

执行上面的_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>

二 引入的jquery的方法

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

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

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

需要使用的组件中引入

import $ from 'jquery'
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值