Taro使用wxParse富文本组件

官方使用例子:Taro使用wxParse

项目开发主要使用的ts, 但是ts直接在标签中import可能会报错, 于是按照官方给的示范例子, 单独封装成一个富文本组件。

  1. 下载wxParse, 并把它放在components文件夹中
    在这里插入图片描述
  2. 新建富文本组件
    wxParseComponent.js文件
import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'

import Parse from '@/components/wxParse/wxParse.js'

import './wxParse.scss'


// let data = null;
export default class ParseComponent extends Component {
  componentDidMount() {
    if (this.props.data) {
      const {data} = this.props;
      Parse.wxParse('data', 'html', data, this.$scope, 5);
    }
    // data = '<div style="color: red">我是HTML代码</div>'
    // Parse.wxParse('data', 'html', data, this.$scope, 5)
  }

  

  render() {
    return (
      <View>
        <import src='../../../components/wxParse/wxParse.wxml' />
        <template is='wxParse' data='{{wxParseData:data.nodes}}' />
      </View>
    )
  }
}

scss文件其实就是引入wxParse的scss文件, 也可以在wxParseComponent.js文件直接引入
在这里插入图片描述

  1. 在需要的地方引入并使用
import ParseComponent from './wxParse/wxParseComponent.js' // 引入
            <View>
              {process.env.TARO_ENV === 'weapp' ? (
                <ParseComponent data={product.description} />
              ) : (
                <View>只在小程序里支持</View>
              )}
            </View> // 使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值