vue 使用swiper

今天没有事做,所以无聊玩一玩VUE
思路:
写http请求:
获取数据 :通过QQ音乐移动版,得到数据
数据传入data: 会更新视图层,但是swiper他的样式只显示一种,所以才会有最后一步
在update时候创建实例:new swiper

<pre><code>
return new Promise((resolve, reject) => {
        let requestObj;
        if (window.XMLHttpRequest) {
            requestObj = new XMLHttpRequest();
        } else {
            requestObj = new ActiveXObject;
        }
        let sendData = '';
        if (type == 'POST') {
            sendData = JSON.stringify(data);
        }
        requestObj.open(type, url, true);
        requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        requestObj.send(sendData);
        requestObj.onreadystatechange = () => {
            if (requestObj.readyState == 4) {
                if (requestObj.status == 200) {
                    let obj = requestObj.response
                    if (typeof obj !== 'object') {
                        obj = JSON.parse(obj);
                    }
                    resolve(obj)
                } else {
                    reject(requestObj)
                }
            }
        }
    })
</code></pre>

vue代码

<template>
<div v-if="recommends.length">
  <div class="swiper-container">
    <div class="swiper-wrapper" >
      <div class="swiper-slide" v-bind:key="item.id" v-for="item in recommends">
       <a :href="item.linkUrl">
                ![](item.picUrl)
              </a>
      </div>
    </div>
    <div class="swiper-pagination swiper-pagination-white"></div>
  </div>
</div>
</template>
<script>
import Swiper from 'swiper';
import 'swiper/dist/css/swiper.min.css';
import httpxml from '../../common/js/httpxml'
export default {
  data() {
    return {
      recommends: [],
    }
  },
  created() {
      this._getRecommend()
  },
  updated(){
    this.createSwiper();
  },
  methods: {
    createSwiper() {
      var swiper = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        paginationClickable: true,
        loop: true,
        speed: 600,
        autoplay: 4000,
        onTouchEnd: function() {
          swiper.startAutoplay()
        }
      });
  },
    _getRecommend() {
      httpxml('/v2/view', { a: "b" }, 'post').then((res) => {
        this.recommends = res.data.slider;
      })
    },
  }
}
</script>

推荐阅读更多精彩内容

  • 转载 :OpenDiggawesome-github-vue 是由OpenDigg整理并维护的Vue相关开源项目库...
    果汁密码阅读 22,755评论 8 125
  • 1.安装 可以简单地在页面引入Vue.js作为独立版本,Vue即被注册为全局变量,可以在页面使用了。 如果希望搭建...
    Awey阅读 10,578评论 4 129
  • Vue 实例 属性和方法 每个 Vue 实例都会代理其 data 对象里所有的属性:var data = { a:...
    云之外阅读 1,891评论 0 6
  • 普罗旺斯的今天,天空中的彩霞殷红凄美,小朵的白云点缀其间,闲散而凄凉,酷似那天。 将挚爱的薰衣草一簇一簇收藏于相机...
    莫若惜阅读 724评论 2 3