当前位置: 首页 > news >正文

uni-app x开发商城系统,资讯详情页面数据渲染

一、概述

上一篇文章,已经实现了资讯列表跳转详情并传递id

接下来实现,资讯详情页面数据渲染

效果如下:

动画

二、资讯详情页面结构

修改 pages/news/news-detail文件,固定一行数据

<template><view><!-- 资讯页 点击跳转--><view class="news_detail"><view><text>1季度多家房企利润跌幅超50% 去库存促销战打响</text><view class="info"><text>2015-04-16</text><text>浏览:3</text></view><view class="content"><text>房企一季度销售业绩已经陆续公布,克而瑞研究中心统计数据显示,今年一季度,TOP20的房企仅6家实现业绩同比增长。</text></view></view></view></view>
</template><script>export default {data() {return {id: ''}},//页面加载拿到传递来的参数id值
        onLoad(value) {this.id = value.id;console.log("onload拿到id", value);},methods: {}}
</script><style lang="scss">.news_detail {font-size: 30rpx;padding: 0 20rpx;.title {text-align: center;width: 710rpx;display: block;margin: 20rpx 0;}.info {display: flex;justify-content: space-between;// 子元素自动水平排列flex-direction: row;}.content {margin-top: 10px;}}
</style>

编译代码,运行效果如下:

image

三、调用api接口

//发送请求 传入id获取资讯详情
async getNewsDetail() {const res = await this.$http.get('/api/getnew/' + this.id, {})this.detail = res.message[0];console.log("id:" + this.id + " 资讯数据", this.detail);
}

四、数据渲染

rich-text

官方文档:https://uniapp.dcloud.net.cn/component/rich-text.html#rich-text

image

 

注意:由于api接口返回的详情内容是一段html代码,因此需要使用rich-text组件来渲染。

 

修改 pages/news/news-detail文件,渲染api接口数据

完整代码如下:

<template><view><!-- 资讯页 点击跳转--><view class="news_detail"><view><text>{{detail.title}}</text><view class="info"><text>{{detail.add_time}}</text><text>浏览:{{detail.click}}</text></view><view class="content"><!-- rich-text 支持富文本 --><rich-text :nodes="detail.content"></rich-text></view></view></view></view>
</template><script>export default {data() {return {id: '',//详情数据回显detail: "",}},//页面加载拿到传递来的参数id值
        onLoad(value) {this.id = value.id;console.log("onload拿到id", value);//回显详情数据方法
            this.getNewsDetail();},methods: {//发送请求 传入id获取资讯详情
            async getNewsDetail() {const res = await this.$http.get('/api/getnew/' + this.id, {})this.detail = res.message[0];console.log("id:" + this.id + " 资讯数据", this.detail);}}}
</script><style lang="scss">.news_detail {font-size: 30rpx;padding: 0 20rpx;.title {text-align: center;width: 710rpx;display: block;margin: 20rpx 0;}.info {display: flex;justify-content: space-between;// 子元素自动水平排列flex-direction: row;}.content {margin-top: 10px;}}
</style>

编译运行,效果如下:

image

五、封装公用类

可以看到上面的时间格式不对,但是重新copy一份代码,格式化日志,比较麻烦。

为安全考虑,uvue 不支持 v-html 指令,因此还需要对&emsp; 这类 HTML 实体 转成空格

因此需要封装一个共用类,实现全局统一调用。

 

新建文件utils/public.uts,内容如下:

// 公共工具类
export class PublicMethod {toast(msg : string) {uni.showToast({ title: msg, icon: 'none' })}formatDate(d : Date) : string {const y = d.getFullYear()const m = String(d.getMonth() + 1).padStart(2, '0')const day = String(d.getDate()).padStart(2, '0')return `${y}-${m}-${day}`}// 截取日期
    cut_data(time_str) {const date = time_str.split('T')[0];return date;},// 转换特殊html内容为空格htmlEntities(str : string) : string {return str.replace(/&emsp;/g, '\u2003').replace(/&ensp;/g, '\u2002').replace(/&nbsp;/g, '\u00A0').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&')}// ……想加多少方法继续写
}

 

修改main.uts,挂载实例

import App from './App.uvue'import { createSSRApp } from 'vue'import uviewPlus from 'uview-plus'import { PublicMethod } from '@/utils/public.uts'// 全局唯一实例
uni.$publicMethod = new PublicMethod()/* 1. 引入 request(里面已经初始化好 http) */
import http from '@/utils/request'export function createApp() {const app = createSSRApp(App)/* 2. 挂到全局属性 */app.config.globalProperties.$http = httpapp.use(uviewPlus)return {app}
}

修改 pages/news/news-detail文件,调用公共方法publicMethod

<template><view><!-- 资讯页 点击跳转--><view class="news_detail"><view><text>{{detail.title}}</text><view class="info"><text>发表时间:{{publicMethod.cut_data(detail.add_time)}}</text><text>浏览:{{detail.click}}</text></view><view class="content"><!-- rich-text 支持富文本 --><rich-text :nodes="publicMethod.htmlEntities(detail.content)"></rich-text></view></view></view></view>
</template><script>export default {data() {return {id: '',//详情数据回显detail: "",// 把全局实例挂到当前页面 data
                publicMethod: uni.$publicMethod as PublicMethod,}},//页面加载拿到传递来的参数id值
        onLoad(value) {this.id = value.id;console.log("onload拿到id", value);//回显详情数据方法
            this.getNewsDetail();},methods: {//发送请求 传入id获取资讯详情
            async getNewsDetail() {const res = await this.$http.get('/api/getnew/' + this.id, {})this.detail = res.message[0];console.log("id:" + this.id + " 资讯数据", this.detail);}}}
</script><style lang="scss">.news_detail {font-size: 30rpx;padding: 0 20rpx;.title {text-align: center;width: 710rpx;display: block;margin: 20rpx 0;}.info {display: flex;justify-content: space-between;// 子元素自动水平排列flex-direction: row;}.content {margin-top: 10px;}}
</style>

 

编译代码,效果如下:

image

 

 再点击其他资讯,效果如下:

动画

 

http://www.gsyq.cn/news/39771.html

相关文章:

  • 2025年重庆3.7米小卡服务商权威推荐榜单:重庆3.8米小卡/重庆4.2米轻卡/重庆货车源头服务商精选
  • StockTV API与其他主流数据源(如Yahoo Finance、Alpha Vantage)相比有哪些具体优势?
  • Pycharm复制项目的一个注意事项
  • 2025 年 11 月防腐球墨铸铁管,给水球墨铸铁管,水利工程用球墨铸铁管厂家最新推荐,实力品牌深度解析采购无忧之选!
  • 2025年糖果车间地坪厂家权威推荐榜单:饼干车间地坪/面包车间地坪/屠宰车间地坪源头厂家精选
  • C# Web开发教程(十二)数据校验机制
  • 2025 年 11 月碳纤维铣刀源头厂家口碑推荐榜:高精度碳纤维铣刀,复合材料专用铣刀,CNC 加工铣刀厂家精选,助力高效切削与优质加工!
  • supervosor 进程管理
  • 2025年垃圾桶加工厂权威推荐榜单:智能回收箱/分类垃圾桶/户外垃圾桶源头厂家精选
  • java基础-12 : 单列集合(Collection) - 指南
  • 三场比赛(四)
  • 2025 年 11 月 SAP 集成平台,EDI 集成平台,ERP 集成平台公司最新推荐,聚焦资质、案例、售后的五家机构深度解读!
  • Luogu P11361 [NOIP2024] 编辑字符串 题解
  • 2025年变电站接地线定做厂家权威推荐榜单:便携型接地线/单簧卡口接地线/电厂专用接地线源头厂家精选
  • 2025年钢管总成加工厂权威推荐榜单:液压钢管总成/硬管总成品牌/免焊接钢管总成源头厂家精选
  • 关于Dify工作流的项目实现与思考
  • 2025年河北搬家渠道权威推荐榜单:河北单位搬迁/河北搬运小时工/河北大型设备搬运服务精选
  • 2025年顶尖候车亭/公交站台//电子站牌/公交站牌/公交候车厅厂家: 江苏兰太城市科技领跑
  • 华为高斯Gauss数据库版本与兼容协议--详解(附带Gorm连接示例代码) - 教程
  • 2025年酒店剃须刀生产厂家权威推荐榜单:一次性剃须刀套装/女士刮毛刀/刮胡刀供应商精选
  • 打开word或PDF,在同目录下自动生成debug.log文件,文件大小0字节!最后发现竟然是一个时时刻刻都要用到的软件引起的
  • 2025年不锈钢门定制工厂排名,不锈钢酒柜定制公司推荐
  • 1000UserGuide:独立开发者获取前1000个用户的终极指南
  • 2025 年仿石漆厂家最新推荐品牌排行榜 —— 涵盖真石漆、水包砂、冠晶石等品类,权威剖析前沿品牌核心竞争力
  • 都昌电子病历编辑器最新特性
  • C/C++跳动的爱心③ - 详解
  • 2025年东北地区电气自动化公司排名,东宇电气市场口碑与产品质量全解析
  • 2025年11月空气能厂家推荐榜:五家优质企业横向对比与深度解析
  • 2025年11月止痒控油洗发水推荐对比:权威评测与用户口碑排行
  • 2025年黑胶唱机制造企业权威推荐榜单:黑胶唱机选购/顶级黑胶唱机/黑胶唱机推荐源头厂家精选