vue中使用swiper插件遇到的问题

//1. 安装swiper
npm i swiper
//2.引用
import Swiper from "swiper";
//下面用不了就用这个 import "swiper/dist/css/swiper.css"; 可以去node_modules--->swiper文件夹里找一下swiper.css的路径
import "swiper/css/swiper.css";
//3.创建swiper实例
  **-----1.由于swiper组件必须在界面加载完毕后在才能使用,所以实例应该放在mounted函数里
  **-----2.由于getItems函数是异步请求数据,所以要处理swiper实例在数据请求成功后创建,
           使用 this.$nextTick([callback]),nextTick即页面加载完之后执行回调函数,方法有3个:
  **-----2.1直接利用asyncawait
		async mounted() {
		  await this.$store.dispatch("getItems");
		   this.$nextTick(() => {
		      var mySwiper = new Swiper(".swiper-container", {
		        loop: true, // 循环模式选项
		        // 如果需要分页器
		        pagination: {
		          el: ".swiper-pagination"
		        }
		      });
		  });
		}
            
**-----2.2在getItems函数中传一个参数cb,函数最后添加一句代码typeof cb==='function' &&cb()
          在dispatch getItems的时候传递回调函数
	  async getItems({ commit },cb) {
	  	const result = await reqItems()
	  	if (result.code === 0) {
	    	const items = result.data
	    	commit(RECEIVE_ITEMS, items)
	    	typeof cb==='function' &&cb()
	  	}
	  },
	 mounted() {
	  this.$store.dispatch("getItems", () => {
	    this.$nextTick(() => {
	      var mySwiper = new Swiper(".swiper-container", {
	        loop: true, // 循环模式选项
	        // 如果需要分页器
	        pagination: {
	          el: ".swiper-pagination"
	        }
	      });
	    });
	  });
	}
**-----2.3在watch中监视执行  
watch: {
     items() {
       this.nextTick(() => {
         var mySwiper = new Swiper(".swiper-container", {
           loop: true, // 循环模式选项
           // 如果需要分页器
           pagination: {
             el: ".swiper-pagination"
           }
         });
       });
    }
  }
//推荐使用2中的3个方法的第一个2.1,简单
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值