uni-app文章详情-富文本展示 优雅展示代码块

简介:

在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、微信小程序的效

11

2、百度小程序的效果:
12

非常完美的解决了问题。

目录
相关文章
|
1月前
|
移动开发 负载均衡 小程序
代驾app开发丨代驾系统开发玩法详情丨代驾系统开发网页版/H5/小程序及源码部署
**司机/代驾员端**:司机可以通过APP接收订单,查看订单详情、路线和导航,提供现场服务并进行确认。
|
2月前
|
数据采集 XML API
京东app商品详情数据接口
京东app商品详情数据接口
30 1
|
2月前
|
数据采集 存储 数据挖掘
淘宝app端商品详情数据采集python
淘宝app端商品详情数据采集python
29 2
|
7月前
|
存储 前端开发 测试技术
同城交友开发稳定版丨同城交友app系统开发详细程序/案例设计/方案逻辑/规则详情/源码设计
需求分析:明确系统的功能、特点和目标受众,并根据市场需求进行需求调研,从中提取核心功能和关键需求。
|
9月前
|
XML JSON 缓存
Java实现商品ID获取淘宝APP商品详情方法
Java实现商品ID获取淘宝APP商品详情方法
82 0
|
10月前
|
人工智能 小程序 大数据
盲盒抽奖游戏APP及小程序系统开发(详情开发)丨盲盒抽奖游戏APP及小程序开发源码平台
  企业以互联网为依托,通过运用大数据、人工智能等先进技术手段,对商品的生产、流通与销售过程进行升级改造,进而重塑业态结构与生态圈,并对线上服务、线下体验以及现代物流进行深度融合的零售新模式。
|
10月前
|
存储 JSON 安全
旅游APP开发详情丨旅游系统开发案例设计/功能方案/逻辑项目/源码平台
 数据共享和分发:区块链中的链式结构允许数据在网络中被共享和分发。每个参与节点都可以获得完整的区块链副本,从而保持数据的同步和一致性。这种去中心化的特点使得区块链具有高度的可靠性和抗故障能力。
|
10月前
|
存储 区块链
即时聊天/即时通讯app系统开发详情方案/玩法规则/案例设计/源码说明
区块链的核心——分布式和存储不依赖于中心化的硬件或管理机构,在区块链中的所有节点的权限和义务都是对等的,Therefore,each result can also participate in the recording and maintenance of data.It differs from traditional data structures that rely on the"center"and can achieve point-to-point data transmission and real-time data recording,with higher effici
|
10月前
|
XML JSON 缓存
数据获取-快捷获取淘宝app商品详情原数据
数据获取-快捷获取淘宝app商品详情原数据
|
10月前
|
数据采集 移动开发 NoSQL
商品详情app原数据
商品详情app原数据
664 0

热门文章

最新文章