Richtext
组件类型:UxRichtextComponentPublicInstance
可渲染文字样式、图片、超链接,支持部分HTML标签
平台兼容性
UniApp X
Android | iOS | web | 鸿蒙 Next | 小程序 |
---|---|---|---|---|
√ | √ | √ | x | √ |
UniApp Vue Nvue
Android | iOS | web | 鸿蒙 Next | 小程序 |
---|---|---|---|---|
x | x | √ | x | x |
Props
属性名 | 类型 | 默认值 | 说明 |
---|---|---|---|
nodes | String | 节点列表 | |
html | String | HTML标签 | |
selectable | Boolean | false | 文本是否可选,Android设置selectable属性为true时,click事件不触发 |
Events
事件名 | 说明 | 参数 |
---|---|---|
click | 被点击时触发 |
示例代码
html
<template>
<ux-page :stack="showDoc">
<ux-navbar :title="title" :bold="true">
<template v-slot:right>
<!-- #ifndef MP -->
<ux-button theme="text" icon="/static/tip.png" :icon-size="22" @click="onDoc()"></ux-button>
<!-- #endif -->
</template>
</ux-navbar>
<ux-scroll>
<ux-card direction="column" icon="flag-filled" title="富文本" :bold="true">
<ux-text text="可渲染文字样式、图片、超链接,支持部分HTML标签"></ux-text>
</ux-card>
<ux-card direction="column" icon="arrowright" title="协议模版" :bold="true">
<ux-richtext :nodes="protocol" @click="click"></ux-richtext>
</ux-card>
<ux-card direction="column" icon="arrowright" title="html标签" :bold="true">
<ux-richtext :html="html"></ux-richtext>
</ux-card>
<ux-card direction="column" icon="arrowright" title="节点" :bold="true">
<ux-richtext :nodes="nodes" @click="click"></ux-richtext>
</ux-card>
<ux-placeholder :height="200">
<ux-row justify="center" align="center" style="height: 100%;">
<ux-text prefix-icon="soapbubble-filled" text="真的没有了~"></ux-text>
</ux-row>
</ux-placeholder>
</ux-scroll>
</ux-page>
</template>
<template>
<ux-page :stack="showDoc">
<ux-navbar :title="title" :bold="true">
<template v-slot:right>
<!-- #ifndef MP -->
<ux-button theme="text" icon="/static/tip.png" :icon-size="22" @click="onDoc()"></ux-button>
<!-- #endif -->
</template>
</ux-navbar>
<ux-scroll>
<ux-card direction="column" icon="flag-filled" title="富文本" :bold="true">
<ux-text text="可渲染文字样式、图片、超链接,支持部分HTML标签"></ux-text>
</ux-card>
<ux-card direction="column" icon="arrowright" title="协议模版" :bold="true">
<ux-richtext :nodes="protocol" @click="click"></ux-richtext>
</ux-card>
<ux-card direction="column" icon="arrowright" title="html标签" :bold="true">
<ux-richtext :html="html"></ux-richtext>
</ux-card>
<ux-card direction="column" icon="arrowright" title="节点" :bold="true">
<ux-richtext :nodes="nodes" @click="click"></ux-richtext>
</ux-card>
<ux-placeholder :height="200">
<ux-row justify="center" align="center" style="height: 100%;">
<ux-text prefix-icon="soapbubble-filled" text="真的没有了~"></ux-text>
</ux-row>
</ux-placeholder>
</ux-scroll>
</ux-page>
</template>
ts
<script setup>
import * as plus from '@/uni_modules/ux-plus'
const showDoc = ref(false)
const title = ref('')
const html = ref('<h1>富文本</h1><br/><h2>可渲染文字样式、图片、超链接,支持部分HTML标签</h2>')
const nodes = ref<UTSJSONObject[]>([{
name: 'p',
attrs: {
style: 'text-align: center; text-decoration: line-through;'
},
children: [
{
type: 'text',
text: 'UxFrame'
}
]
},
{
name: 'p',
attrs: {
style: 'background-color: green; color: red;'
},
children: [
{
type: 'text',
text: '富文本演示'
}
]
},
{
name: 'img',
attrs: {
src: 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png',
width: '100',
height: '100'
}
}] as UTSJSONObject[])
const protocol = ref<UTSJSONObject[]>([{
name: 'p',
attrs: {
style: 'font-size: 13px;'
},
children: [
{
type: 'text',
text: '欢迎您使用我们的服务!在开始使用之前,请仔细阅读以下协议:'
},
{
name: 'a',
attrs: {
href: 'https://ask.dcloud.net.cn/article/35623',
style: 'color: #A90000;'
},
children: [
{
type: 'text',
text: '《用户使用协议》'
}
]
},
{
type: 'text',
text: '、'
},
{
name: 'a',
attrs: {
href: 'https://ask.dcloud.net.cn/article/35623',
style: 'color: #A90000;'
},
children: [
{
type: 'text',
text: '《隐私政策》'
}
]
}
]
}] as UTSJSONObject[])
function click(e : UniRichTextItemClickEvent) {
console.log(e);
// 点击图片
if(e.detail.src != null) {
uni.previewImage({
urls: [e.detail.src!]
})
}
// 点击超链接
if(e.detail.href != null) {
uni.navigateTo({
url: `/pages/webview/webview?title=隐私政策&url=${e.detail.href!}`
})
}
}
function onDoc() {
plus.openWeb({
title: '在线文档',
url: 'https://www.uxframe.cn/component/richtext.html',
// blur: 1,
success: () => {
showDoc.value = true
},
complete: () => {
showDoc.value = false
}
})
}
onLoad((e : OnLoadOptions) => {
title.value = e['title'] ?? ''
})
</script>
<script setup>
import * as plus from '@/uni_modules/ux-plus'
const showDoc = ref(false)
const title = ref('')
const html = ref('<h1>富文本</h1><br/><h2>可渲染文字样式、图片、超链接,支持部分HTML标签</h2>')
const nodes = ref<UTSJSONObject[]>([{
name: 'p',
attrs: {
style: 'text-align: center; text-decoration: line-through;'
},
children: [
{
type: 'text',
text: 'UxFrame'
}
]
},
{
name: 'p',
attrs: {
style: 'background-color: green; color: red;'
},
children: [
{
type: 'text',
text: '富文本演示'
}
]
},
{
name: 'img',
attrs: {
src: 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png',
width: '100',
height: '100'
}
}] as UTSJSONObject[])
const protocol = ref<UTSJSONObject[]>([{
name: 'p',
attrs: {
style: 'font-size: 13px;'
},
children: [
{
type: 'text',
text: '欢迎您使用我们的服务!在开始使用之前,请仔细阅读以下协议:'
},
{
name: 'a',
attrs: {
href: 'https://ask.dcloud.net.cn/article/35623',
style: 'color: #A90000;'
},
children: [
{
type: 'text',
text: '《用户使用协议》'
}
]
},
{
type: 'text',
text: '、'
},
{
name: 'a',
attrs: {
href: 'https://ask.dcloud.net.cn/article/35623',
style: 'color: #A90000;'
},
children: [
{
type: 'text',
text: '《隐私政策》'
}
]
}
]
}] as UTSJSONObject[])
function click(e : UniRichTextItemClickEvent) {
console.log(e);
// 点击图片
if(e.detail.src != null) {
uni.previewImage({
urls: [e.detail.src!]
})
}
// 点击超链接
if(e.detail.href != null) {
uni.navigateTo({
url: `/pages/webview/webview?title=隐私政策&url=${e.detail.href!}`
})
}
}
function onDoc() {
plus.openWeb({
title: '在线文档',
url: 'https://www.uxframe.cn/component/richtext.html',
// blur: 1,
success: () => {
showDoc.value = true
},
complete: () => {
showDoc.value = false
}
})
}
onLoad((e : OnLoadOptions) => {
title.value = e['title'] ?? ''
})
</script>
css
<style lang="scss">
</style>
<style lang="scss">
</style>