<template><view class= 'news'><news-item :list='newslist'></news-item></view></template><script>import newsItem from '../../components/news-item/news-item'export default {data() {return {newslist:[]}},components:{"news-item":newsItem},methods: {async getNews (){const res = await this.$myRequest({url:'/api/getnewslist'})this.newslist = res.data.messageconsole.log(this.newslist)}},onLoad(){this.getNews()}}</script><style lang='scss'>.news{}</style>
这个是new的列表,另外我们可以讲新闻这个页面进行封装
<template><view><view class='news_item' v-for='item in list' :key='item.id'><image :src='item.img_url'></image><view class='right'><view class='tit'>{{item.title}}</view><view class='info'><text>发表时间:{{item.add_time}}</text><text>浏览:{{item.click}}</text></view></view></view></view></template><script>export default{props:['list']}</script><style lang='scss'>.news_item{display: flex;padding:10rpx 20rpx;border-bottom:1px solid $shop-color;image{min-width:200rpx;max-width: 200rpx;height:150rpx;border:1px dashed #007AFF;}.right{margin-left:15rpx;display: flex;flex-direction: column;justify-content: space-between;.tit{font-size: 33rpx;}.info{font-size: 25rpx;text:nth-child(2){margin-left:30rpx;}}}}</style>
封装的步骤
- 在componments的组件文件夹下新建组件
- 利用props来获取传过来的变量
- 使用的时候需要导入另外要在组件里面注册
