富文本 xHtml
预览
介绍
HTML 渲染组件(我同时也提供了一个原生渲染HTML的插件x-html-u,类似头条,如果你有更高需要需求,比如安全控制和定义渲染,渲染为页面图片请使用此原生插件,如果只是普通需要可以用本页面组件)。App 端基于官方 rich-text 原生渲染,Web / 小程序基于 htmlparser2 节点渲染。只支持 HTML,不解析 Markdown。 App 端使用官方 rich-text 原生渲染,默认 mode="native"。 Web / MP-WEIXIN 使用条件编译:htmlparser2 解析 HTML 为节点树,再由全局子组件 x-html-node 递归渲染,支持链接和图片点击等扩展能力。 htmlparser2 只在 Web / 小程序分支引入,App 端不会编译 x-html-node 和解析库。 支持的标签和样式以官方 rich-text 文档为准:br、p、ul、li、span、strong、i、big、small、a、u、del、h1-h6、img;Web/小程序节点渲染额外支持表格、pre、blockquote 等常见标签。 官方文档:https://doc.dcloud.net.cn/uni-app-x/component/rich-text.html
平台兼容
| Harmony | H5 | andriod | IOS | 小程序 | UTS | UNIAPP-X SDK | version |
|---|---|---|---|---|---|---|---|
| ☑ | ☑ | ☑️ | ☑️ | ☑️ | ☑️ | 4.76+ | 1.1.18 |
文件路径
ts
@/uni_modules/tmx-ui/components/x-html/x-html.uvue使用
ts
<x-html></x-html>Props 属性
| 名称 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 需要渲染的 HTML 字符串,只支持 HTML,不解析 Markdown | string | "" |
| mode | 渲染模式。仅 App 端生效:native 使用原生 rich-text,web 使用 webview;Web/小程序端固定走节点渲染 | union | 'native' |
| space | 显示连续空格的方式:ensp / emsp / nbsp | string | '' |
| userSelect | 文本是否可选 | boolean | true |
| contentStyle | App:rich-text 的全局样式,支持 font-size / font-family / line-height / color;Web/小程序:作为节点的基础文本样式,暗黑模式下未指定 color 时默认 #ffffff | string | "line-height:1.6;" |
| selectionHandleColor | 选择区域高亮手柄颜色(仅 App native) | string | "#4285F4FF" |
| selectionBackgroundColor | 选择区域高亮背景颜色(仅 App native) | string | "#33B5E566" |
Events 事件
| 名称 | 参数 | 说明 |
|---|---|---|
| itemClick | undefined | 点击 a 或 img 时触发。 |
Slots 插槽
| 名称 | 说明 | 数据 |
|---|
Ref 方法
| 名称 | 参数 | 返回值 | 说明 |
|---|
示例文件路径
json
/pages/zhanshi/html示例源码
uvue
vue
<template>
<!-- #ifdef MP-WEIXIN || MP-ALIPAY -->
<page-meta :page-style="`background-color:${xThemeConfigBgColor}`">
<navigation-bar :background-color="xThemeConfigNavBgColor"
:front-color="xThemeConfigNavFontColor"></navigation-bar>
</page-meta>
<!-- #endif -->
<x-sheet>
<x-text font-size="18" class=" text-weight-b mb-8">富文本 HTML xHtml</x-text>
<x-text color="#999999">
App 端基于官方 rich-text,Web / 小程序走节点渲染;只支持 HTML,不解析 Markdown。
App 端使用官方 rich-text 原生渲染;Web/小程序通过条件编译使用 htmlparser2 解析节点,再由 x-html-node 递归渲染,支持链接和图片点击。
App 端支持的标签以官方 rich-text 为准,Web/小程序额外支持表格、pre、blockquote、ol 等。
</x-text>
</x-sheet>
<x-sheet>
<x-html :value="html" @itemClick="onItemClick"></x-html>
<x-button class="mt-20" @click="appendHtml" :block="true">追加一段 HTML</x-button>
</x-sheet>
<!-- #ifdef WEB || MP-WEIXIN || MP-ALIPAY -->
<x-sheet>
<x-text font-size="16" class="text-weight-b mb-8">Web / 小程序扩展标签</x-text>
<x-text color="#999999" class="mb-12">
App 端以官方 rich-text 支持范围为准;Web/小程序节点渲染额外支持表格、引用、代码块和有序列表。
</x-text>
<x-html :value="richHtml" @itemClick="onItemClick"></x-html>
</x-sheet>
<!-- #endif -->
</template>
<script setup lang="ts">
import { ref } from "vue"
const html = ref<string>(`<h1>富文本 HTML</h1>
<p style="line-height:1.8;">
这是 <strong>加粗</strong>、<i>斜体</i>、<u>下划线</u>、<del>删除线</del> 和
<span style="color:#ff6600;">彩色文字</span> 的示例。
<br/>第二行还可以放 <big>大号文字</big> 和 <small>小号文字</small>。
</p>
<h2>列表</h2>
<ul>
<li>第一项</li>
<li>第二项</li>
<li>第三项</li>
</ul>
<h2>链接和图片</h2>
<p>
<a href="https://xui.tmui.design">点击打开 TMUI 官网</a>
</p>
<p>
<img src="https://xui.tmui.design/assets/logo-a483vlZl.png" style="width:120px;height:auto;" />
</p>
顶层行内混排(没有 p 包裹):<strong>加粗</strong>、<a href="https://xui.tmui.design">链接</a>和普通文字应该保持在同一行。
`)
// #ifdef WEB || MP-WEIXIN || MP-ALIPAY
const richHtml = ref<string>(`<h2>表格</h2>
<table>
<caption>组件对照</caption>
<thead>
<tr><th>组件</th><th>说明</th></tr>
</thead>
<tbody>
<tr><td>x-html</td><td>HTML 渲染入口</td></tr>
<tr><td>x-html-node</td><td>内部递归节点渲染器,不单独使用</td></tr>
</tbody>
</table>
<h2>引用和代码</h2>
<blockquote>Web / 小程序节点渲染支持 blockquote。</blockquote>
<pre><code>const html = "<strong>hello</strong>";
console.log(html);</code></pre>
<h2>有序列表</h2>
<ol start="3">
<li>第三项</li>
<li>第四项</li>
</ol>
`)
// #endif
function onItemClick(detail : Record<string, any>) : void {
const href = detail['href'] as string
const src = detail['src'] as string
let text = ''
if (href != null && href != '') {
text = href
} else if (src != null && src != '') {
text = src
}
if (text == '') return
uni.showToast({
title: text,
icon: 'none'
})
}
function appendHtml() : void {
html.value = html.value + `<p style="line-height:1.8;">
追加的一段 HTML:<strong>动态更新</strong>、<span style="color:#007aff;">蓝色文字</span>。
</p>`
}
</script>
<style>
</style>