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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| <template> <div> <div class="news-img"> <a :href="news.url"> <img :src="news.thumbnail_pic_s" /> </a> </div> <div class="type-news"> <ul> <li v-for="item in typeList" :key="item.id" :class="{ typeinit: isAlive == item.id }" @click="change(item.id)" > {{ item.name }} </li> </ul> </div> <NewsList :data="newsList"></NewsList> </div> </template>
<script> import axios from 'axios' import {reactive, toRefs} from "vue"; import NewsList from "@/components/NewsList"; export default { name: "TypeNews", setup() { const state = reactive({ typeList: [ { id: "guonei", name: "国内" }, { id: "guoji", name: "国际" }, { id: "yule", name: "娱乐" }, { id: "tiyu", name: "体育" }, { id: "junshi", name: "军事" }, { id: "keji", name: "科技" }, { id: "caijing", name: "财经" }, { id: "youxi", name: "游戏" }, { id: "qiche", name: "汽车" }, { id: "jiankang", name: "健康" }, ], isAlive: "guonei", newsList: [], news: {}, });
getNews("guonei");
function getNews(type) { axios.get("/juheNews/toutiao/index", { params: { type: type, key: '你的key', page_size: 5, }, }).then((resp=>{ state.newsList = resp.data.result.data; state.news = state.newsList[Math.floor(Math.random() * state.newsList.length)]; })).catch((error=>{ console.log(error) })) }
function change(id) { state.isAlive = id; getNews(id); }
return { ...toRefs(state), change, }; }, components: { NewsList }
} </script>
<style scoped> .news-img img { width: 100%; height: 200px; display: block; } .type-news { width: 100%; margin-top: 8px; } .type-news ul { width: 100%;
display: flex; justify-content: center; flex-wrap: wrap; } .type-news ul li { box-sizing: border-box; width: 48px; height: 22px; border: solid 1px #e03d3e; border-radius: 11px; margin: 5px 10px;
font-size: 14px; color: #e03d3e;
display: flex; justify-content: center; align-items: center; }
.typeinit { background-color: #e03d3e; color: #fff !important; } </style>
|