vue2.0 + vux (五)api接口封裝 及 首頁 輪播圖製做

1.安裝 jquery 和 whatwg-fetch (優雅的異步請求API)javascript

npm install jquery --save
npm install whatwg-fetch --save

 

2.api接口封裝java

api/index.jsjquery

/**
 * 引入 異步請求API
 */
import "whatwg-fetch"

const host_addr='http://192.168.1.110:8081/'

/**
 * 獲取資訊列表
 */
export let getList = async (page, tag) => {
  let response = await fetch(host_addr + `news_list?pageIndex=${page}&pageSize=20&catalog=${tag}`, {
    method: 'GET',
    mode: 'cors'
  }).catch((error) => {
    console.log(error)
  })
  return await response.json().catch((error) => {
    console.log(error)
  })
}

/**
 * 獲取資訊詳情
 */
export let getNewsDetail = async (id) => {
  let response = await fetch(host_addr + `news_detail?id=${id}`, {
    method: 'GET',
    mode: 'cors'
  }).catch((error) => {
    console.log(error)
  })
  return await response.json().catch((error) => {
    console.log(error)
  })
}

/**
 * 獲取博客列表
 */
export let getBlogList = async (page, tag) => {
  let response = await fetch(host_addr + `blog_list?pageIndex=${page}&pageSize=20&catalog=${tag}`, {
    method: 'GET',
    mode: 'cors'
  }).catch((error) => {
    console.log(error)
  })
  return await response.json().catch((error) => {
    console.log(error)
  })
}

/**
 * 獲取問答列表
 */
export let getQuestionList = async (page, tag) => {
  let response = await fetch(host_addr + `question_list?pageIndex=${page}&pageSize=20&catalog=${tag}`, {
    method: 'GET',
    mode: 'cors'
  }).catch((error) => {
    console.log(error)
  })
  return await response.json().catch((error) => {
    console.log(error)
  })
}

/**
 * 獲取活動列表
 */
export let getEventList = async (page, tag) => {
  let response = await fetch(host_addr + `event_list?pageIndex=${page}&pageSize=20&catalog=${tag}`, {
    method: 'GET',
    mode: 'cors'
  }).catch((error) => {
    console.log(error)
  })
  return await response.json().catch((error) => {
    console.log(error)
  })
}

 

3.輪播圖npm

<!-- 資訊 -->
<template>
  <div>
    <!-- 輪播圖 -->
    <swiper :list="imgs" auto style="width:100%;height:120px;margin:0 auto;" dots-class="custom-bottom" dots-position="center"></swiper>
  </div>
</template>

<script>
  // 引入 vux 內部組件
  import { Swiper } from 'vux'
  // 引入 api接口文檔
  import { getList } from '../../api'

  // 輪播圖列表
  const imgList = [
    'http://file06.16sucai.com/2016/0222/17714c3b51079911760e5ef7fdb553f6.jpg',
    'http://pic.58pic.com/58pic/15/67/98/93C58PICjeM_1024.jpg',
    'http://file06.16sucai.com/2016/0315/1df566087c24a94cd9534bc9bc1871ff.jpg'
  ];

  // 輪播圖圖片地址列表
  const urlList = imgList.map((one, index) => ({
    url: 'javascript:',  //這裏填寫圖片點擊的連接
    img: one
  }));

  export default {
    name: 'NewsList',
    components:{
      Swiper
    },
    data(){
      return {
        imgs:urlList,
      }
    }
  }
</script>

 

4.效果圖json

相關文章
相關標籤/搜索