uni-app前端开发(二)新闻功能

本文详细介绍了uni-app中实现新闻功能的步骤,包括新闻列表的逻辑层和显示层,阅读新闻的单击事件及详情页编写,下拉刷新的配置,到底部加载更多的逻辑处理,以及刷新按钮的展示和逻辑实现。通过page.json、news.vue、read.vue和methods.js等文件的代码示例,帮助开发者理解整个流程。
摘要由CSDN通过智能技术生成

新闻功能

1.新闻列表

首先要在页面的onLoad函数中添加请求初始化分页获取新闻列表

1.1逻辑层

<script>
    // 此处将方法抽取到了/common/method.js中
	import {
    fail,checkToken} from '../../common/method.js'
	import refresh from '@/static/refresh.png'
	export default {
    
        data() {
    
			return {
    
				newsList: [],
				pageNo: 1,
				pageCount: 1,
			}
		},
        methods: {
    
			getData(save = false) {
    	// 根据save值判断是第一页还是加载更多,默认false第一页
				const _this = this;
				uni.request({
    
					url: 'http://zhilianit.cn:8080/bwl/news/etNews/list',
					method: 'GET',
					data: {
    
						pageNo: _this.pageNo,
						pageSize: 10
					},
					header: {
     // 请求头
						"X-Access-Token": uni.getStorageSync("token")
					},
					success({
    data}) {
    	// 请求成功
						if (data.success) {
     	// 写入数据,save表示是否保存当前数据
							_this.write({
    save:save,data:data.result})
						} else {
    				// 判断是否token失效,弹出错误信息
							checkToken(data.message);
						}
					},
					fail() {
    
						
					},
					complete() {
    
                        
					}
				})
			}
			write({
    save,data}){
    
				if (save) {
    
					this.newsList = this.newsList.concat(data.records);
				} else {
    
					this.newsList = data.records;
				}
				this.pageCount = data.pages;
			}
		},
		onLoad({
    data}) {
    
			this.getData();
		}
    }
</script>

1.2显示层

<template>
	<view>
		<u-cell-group>	<!-- 通过v-for循环遍历列表,vue v-for循环时要给元素key唯一的值,确定其唯一性 -->
			<u-cell-item :title="item.title" v-for="(item,index) in newsList" :key="index" @tap="nav(item.id)" :center="true"></u-cell-item>
		</u-cell-group>
	</view>
</template>

2.阅读新闻

2.1 给新闻添加单击事件

<template>
	<view>
		<u-cell-group>
			<u-cell-item :title="item.title" v-for="(item,index) in newsList" :key="index" @tap="nav(item.id)" :center="true"></u-cell-item>
		</u-cell-group>
		<u-loadmore :status="status"></u-loadmore>
		<u-image class="refresh" :src="refresh" width="80rpx" height="80rpx" @tap="fresh"></u-image>
	</view>
</template>

2.2 添加单击方法nav

<script>
	export default {
    
		methods: {
    
			nav(id) {
    	// 跳转到新闻详情页面
				uni.navigateTo({
    
					url: './read?id=' + id
				})
			}
		}
	}
</script>

2.3 编写新闻详情页

<template>
	<view class="page">
		<view class="title">{
  {info.title}}</view>
		<view class="theme">作者:{
  {info.author}}&nbsp;{
  {info.createTime}}</view>
		<view class="content">
			<u-parse :html="info.content
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值