Vue+Vux學習案例(三)—構建開源中國微信版(新增網路通訊)
阿新 • • 發佈:2019-01-31
首先新建通訊api.js
/** * Created by mwuyz on 2016/10/28. */ import "whatwg-fetch" import promise from "es6-promise" /* *獲取資訊列表 */ export let getList = async (page, tag) => { let response = await fetch(`http://192.168.1.107:8081/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) }) }
因為開源中國的api並不只支援跨域,所以本外另外在本地起了一層轉發服務,這層服務只做轉發,支援跨域請求,解決了這個,就可以正常訪問開源中國的資訊了
這裡目前只做了一個獲取資訊,後面再新增其他功能。這次修改了主頁home.vue
<template> <div> <app-header></app-header> <tab :line-width="2" > <tab-item :selected="tag === item" v-for="item in taglist" @click="tag = item">{{item}}</tab-item> </tab> <news-list></news-list> <app-footer></app-footer> </div> </template> <style> @import '~vux/dist/vux.css'; body{ line-height: 1.2; } </style> <script> import Tab from 'vux/dist/components/tab' import TabItem from 'vux/dist/components/tab-item' import Scroller from 'vux/dist/components/scroller' import Cell from 'vux/dist/components/cell' import Group from 'vux/dist/components/group' import AppHeader from './Header' import AppFooter from './Footer' import NewsList from './NewsList' import ScrollerDemo from './scrollerdemo' export default { data () { return { tag: '資訊', taglist: ['資訊', '部落格', '問答', '活動'], } }, components: { Tab, TabItem, AppHeader, AppFooter, Scroller, Cell, Group, NewsList, ScrollerDemo }, ready () { }, methods:{ } } </script>
增加獲取資訊頁NewsList.vue
<template> <div> <scroller v-show="ishow" lock-x scrollbar-y height="360px"> <div > <cell v-for="x in Objlist" :title="x.title" link="demo/wechat" :inline-desc='x.body'> <img class="ic_img" slot="icon" src="../assets/image/ic_label_today.png"> <div> <span></span> </div> </cell> </div> </scroller> </div> </template> <style> .ic_img{ position:absolute; top:10px; left: 5px; width:15px; height:15px; } .weui_cell_bd>p{ font-size:15px; } .vux-label-desc{ padding-right:15px; } .weui_cell_bd.weui_cell_primary{ padding-left:5px; } </style> <script> import Scroller from 'vux/dist/components/scroller' import Cell from 'vux/dist/components/cell' import Group from 'vux/dist/components/group' import { getList } from '../utils/api' export default{ data(){ return{ ishow:true, Objlist:[], title:'111', body:'1111', comment_count:2, pub_date:'2016-11-01 07:21:39', pageIndex:1, catalog:0, } }, components:{ Scroller, Cell, Group }, ready () { this.getList() // this.suaDate() }, methods:{ async getList() { let data =await getList(this.pageIndex, this.catalog) console.log(data) if(data.obj_list.length>0){ this.ishow=true this.title=data.obj_list[0].title this.body=data.obj_list[0].body for(var i=0;i<data.obj_list.length;i++){ var bngDate = new Date(data.obj_list[i].pub_date); var endDate = new Date(); var minutes = (endDate.getTime()-bngDate.getTime())/60/60/1000; var minute=parseInt(minutes); if(minute>=48){ data.obj_list[i].pub_date='2天前' }else if(minute>=24){ data.obj_list[i].pub_date='昨天' }else{ data.obj_list[i].pub_date=minute+'小時以前' } this.Objlist.push(data.obj_list[i]); } } this.locked = false this.loading = false }, suaDate(){ } } } </script>
到這裡就可以初見成果了,後面功能下期介紹(未完待續) 原始碼下載點這裡。