以前习惯性是引用jQuery,bootstrap。。。库,但是不会引用,怎么办?
npm install swiper--save-dev (或者 npm install jquery -D)
一 .引入swiper(全局,局部)
- 方法一:全局引入,也是最暴力的,但是也是有好处坏处(同时加载,但是不能保证同时下载)
<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);
}
- 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';
执行上面的_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')
export default {
data(){
return {
colorList: ['red', 'yellow', 'gray', 'pink']
}
},
methods: {
async _initSwiper() {
let Swiper = await swiperAsync;
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);
},
},
mounted(){
this._initSwiper();
}
}
</script>
<style lang="scss" scoped>
@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的方法
- 全局的方法
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
组件中可以直接使用的 ‘$’的方法
- 局部的方法:
npm install jquery -D
需要使用的组件中引入
import $ from 'jquery'