在uni-app开发中,开发一个资讯详情页面,详情里包含图片和代码块。这时候用简单的rich-text控件已经不够用了。用官方demo里的html-parser.js也无法很好的展示代码区域。这个时候就要使用官方提供的插件来解决。

官方的这个插件有很多问题。需要使用第3方修复的版本:https://ext.dcloud.net.cn/plugin?id=1279

首先:下载插件 :https://ext.dcloud.net.cn/plugin?id=1279

第二步:写代码 demo示例

 

<template>
  <div>
    <u-parse :content="content" @navigate="navigate"></u-parse>
  </div>
</template>
 
<script>import uParse from "@/components/feng-parse/parse.vue";  //注意:官方提供的示例代码有问题
 
export default {
  components: {
    uParse
  },
  data () {
    return {
      article: '<div>我是HTML代码</div>'
    }
  },
  methods: {
    preview(src, e) {
      // do something
    },
    navigate(href, e) {
      // do something
    }
  }
}
</script>
 
<style>
@import url("../../components/feng-parse/parse.css");  //注意:官方提供的示例代码有问题
</style>

我这边具体的业务代码如下:(可以忽略)

<template>
<view>
<view class="banner">
{{article_detail.title}}
</view> <view class="article-meta">
<text class="article-meta-text article-author">作者:{{article_detail.fields.author}}</text>
</view>
<view class="article-content"> <div>
<u-parse :content="article_detail.content" @preview="preview" @navigate="navigate" />
</div>
<view class="parse-con">
<u-parse :content="article_detail.content" @navigate="navigate"></u-parse>
</view> <view v-if="article_detail.fields.source" class="article-source">
文章来自:{{article_detail.fields.source}}
</view>
</view>
<view class="comment-wrap"></view>
</view>
</template> <script>
import uParse from "@/components/feng-parse/parse.vue" const FAIL_CONTENT = '<p>数据加载中</p>';
export default {
components: {
uParse
},
data() {
return { article_detail: {},
id: 0
}
},
onShareAppMessage() {
return {
title: this.article_detail.title,
path: '/pages/detail/detail?query=' + this.id
}
},
onLoad: function(event) {
// 目前在某些平台参数会被主动 decode,暂时这样处理。
try {
console.log('入参:' + event.query); this.id = event.query;
} catch (error) {
console.log('异常来了'); } this.getDetail();
},
onShow: function() {
console.log('onShow里:');
console.log('id=' + this.id); // #ifdef MP-BAIDU
var zyizurl = getApp().globalData.zyiz_domain + '/api/Zyiz/Detail/' + this.id;
uni.request({
url: zyizurl,
success: (result) => { if (result.statusCode == 200) {
console.log("详情结果2:");
console.log(result);
var article_d = result.data.data;
var keyw = article_d.tags + ',' + article_d.category_name + ',' + getApp().globalData.keywords;
if (article_d.seo_keywords) {
keyw = article_d.tags + ',' + article_d.category_name + ',' + article_d.seo_keywords;
}
var zhaiyao = getApp().globalData.description;
if (article_d.zhaiyao) {
zhaiyao = article_d.zhaiyao;
}
var img_url = 'http://www.zyiz.net/templates/main_zyiz/images/logo.png';
if (article_d.img_url) {
img_url = article_d.img_url;
}
var title = article_d.title + '-' + article_d.category_name;
swan.setPageInfo({
title: title,
keywords: keyw,
description: zhaiyao,
articleTitle: title,
releaseDate: article_d.add_time,
image: img_url,
video: ''
}); } }
}); // #endif },
methods: {
getDetail() { var zyizurl = getApp().globalData.zyiz_domain + '/api/Zyiz/Detail/' + this.id;
uni.request({
url: zyizurl,
success: (result) => { if (result.statusCode == 200) {
console.log("详情结果:");
console.log(result); this.article_detail = result.data.data; } }
}); },
navigate(e) {
console.log(e)
} } } </script> <style>
@import url("../../components/feng-parse/parse.css"); /* #ifndef APP-PLUS */
page {
min-height: 100%;
} /* #endif */
.baidu-arcontent {
width: 98%; } .baidu-arcontent img,
.baidu-arcontent image {
max-width: 95% !important;
mode: aspectFit;
} .article-source {
padding: 10upx;
color: #AAAAAA;
} .banner {
margin: 10upx;
text-align: center;
font-size: 40upx;
font-weight: bold; } .article-content image {
width: 96%; } .banner-img {
flex: 1;
} .title-area {
position: absolute;
left: 30upx;
right: 30upx;
bottom: 30upx;
z-index: 11;
} .title-text {
font-size: 32upx;
font-weight: 400;
line-height: 42upx;
lines: 2;
color: #ffffff;
overflow: hidden;
} .article-meta {
padding: 20upx 30upx;
flex-direction: row;
align-items: center;
justify-content: flex-start;
} .article-meta-text {
color: gray;
} .article-text {
font-size: 26upx;
line-height: 50upx;
margin: 0 20upx;
} .article-author {
font-size: 30upx;
} .article-time {
font-size: 30upx;
} .article-content {
font-size: 30upx;
padding: 0 30upx;
margin-bottom: 30upx;
overflow: hidden;
}
</style>
 

第三步:查看效果:

1、微信小程序的效

2、百度小程序的效果:

非常完美的解决了问题。

文章根据:http://www.zyiz.net/tech/detail-104163.html 改编。

uni-app文章详情-富文本展示 优雅展示代码块的更多相关文章

  1. 微信小程序/支付宝小程序 WxParse解析富文本(html)代码

    小程序本身并不太支持html代码,比如html的img.span.p这个时候改这么办呢?需要用到一个小插件WxParse来实现. 小程序高级交流群:336925436  微信小程序支持富文本编辑器代码 ...

  2. 常见网页编辑器(富文本,Markdown,代码编辑等)

    编辑器:网页不常用的功能,但却又是不可少的功能,如果要造个编辑器轮子,它可以把人玩死!!前端几大禁忌就有富文本, 为什么都说富文本编辑器是天坑? 下面记录一下常见的一些编辑器,该文随时更新: 富文本编 ...

  3. 支付宝小程序开发——rich-text富文本组件支持html代码

    前言: 与微信小程序不同,支付宝小程序的富文本组件并不能直接支持html代码,如: 如上,支付宝小程序的富文本组件只支持数组类型,html代码需要使用 mini-html-parser 转换,具体用法 ...

  4. 小程序 富文本 的页面展示 json 数据处理 go-echo 为小程序提供feed流服务

    go生成页面 返回给web-view {{define "DBHtmlCode"}} <!DOCTYPE html> <html lang="zh-cm ...

  5. 富文本编辑器 - wangEditor 插入代码

    效果: 项目结构: 注意事项: highlightJS 代码高亮插件,wangEditor 本身就是集成的highlightJS代码高亮插件. 在wangEditor-1.3.12.js里找到var ...

  6. UEditor富文本WEB编辑器设置代码高亮

    UEditor编译器支持代码高亮显示,设置方法如下: 1.页面head引入UEditor类包文件shCore.js.shCoreDefault.css代码 (注:引入文件路径根据需求变更即可) < ...

  7. 富文本(wangEditor框架)的使用教程

    富文本的使用教程(wangEditor框架) 一,相信很多人用过很多富文本的框架,现在我向大家推荐一个很实用的一个富文本框架,具有丰富的功能项,容易使用. 所以本博客介绍这个富文本编辑器的使用哈!觉得 ...

  8. 富文本 SpannableString Span

    经典使用场景 SpannableStringBuilder needStartSSB = new SpannableStringBuilder("需要"); SpannableSt ...

  9. java富文本编辑器KindEditor

    在页面写一个编辑框: <textarea name="content" class="form-control" id="content&quo ...

随机推荐

  1. arduino中的serial .available()和serial.read()

    Serial.available() 的意思是:返回串口缓冲区中当前剩余的字符个数.一般用这个函数来判断串口的缓冲区有无数据,当Serial.available()>0时,说明串口接收到了数据, ...

  2. 文本编辑器vim/vi——模式切换及输入模式

    vim一共有三种模式:命令模式.输入模式.末行模式 要从命令模式切换到输入模式:a,i,o a——append 属于在后面追加内容:i——insert 属于插入,在前面插入内容:o——other 属于 ...

  3. 计算机操作系统学习(一) Linux常用指令(随时更新)

    1.chmod 以下转载至https://blog.csdn.net/summer_sy/article/details/70142475 chmod u+x file.sh 就表示对当前目录下的fi ...

  4. 【STM32H7教程】第51章 STM32H7的LTDC应用之LCD汉字显示和2D图形显示

    完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第51章       STM32H7的LTDC应用之LCD汉字 ...

  5. 十八、SAP中使用IF/ELSE判断语句,以及sy-subrc的用法

    一.sy_subrc为上一条语句的执行结果,如果为0,则表示执行成功. 需要注意的是,IF ELSE语句,每一个关键字都需要带句号 二.执行效果如下 相关代码如下 *&------------ ...

  6. Python 打开文件(File Open)

    版权所有,未经许可,禁止转载 章节 Python 介绍 Python 开发环境搭建 Python 语法 Python 变量 Python 数值类型 Python 类型转换 Python 字符串(Str ...

  7. .NET via C#笔记17——委托

    一.委托的内部实现 C#中的委托是一种类型安全的回调函数,假设有这样一个委托: internal delegate void Feedback(int value); 编译器会生成一个类: inter ...

  8. (二分查找)LowerBound

    在包含size个元素的,从小到大顺序的int数组a里查找比给定整数p小的,下标最大的元素,找不到返回-1 题解: int LowerBound(int a[],int size,int p) { in ...

  9. realme发布会将于本周三在北京·幻艺术举行

    5月13日消息,realme宣布将于5月15日(本周三)在北京·幻艺术中心发布realme X. 根据官方公布的海报,realme X采用了升降全面屏方案,无挖孔.无刘海.无水滴,支持屏幕指纹识别,正 ...

  10. JS最新最细面试题

    转之:https://www.jianshu.com/p/f1f39d5b2a2e 1. javascript的typeof返回哪些数据类型. 答案:string,boolean,number,und ...