情景:普通網(wǎng)頁編輯的富文本,保存到服務(wù)器,小程序通過接口獲取了富文本,現(xiàn)在需要把普通的DOM元素放在小程序里展示
下載:https://github.com/icindy/wxParse
把下載的組件放到根目錄下的component文件夾里
<!--pages/detail/detail.wxml-->
<import src="../../components/wxParse/wxParse.wxml"/>
<view>
<template is="wxParse" data="{{wxParseData: article.nodes}}"/>
</view>
/* pages/detail/detail.wxss */
@import '../../components/wxParse/wxParse.wxss'
// pages/detail/detail.js
import WxParse from '../../components/wxParse/wxParse.js'
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
detailObx: {}
},
getDetai() {
wx.request({
url: 'https://xxx.com/xx/index.php/user/index/getArticleInfo',
data: {
id: 1
},
method: 'POST',
header: {
'content-type': 'application/json' // 默認(rèn)值
},
success: (res) => {
console.log(res.data)
const { code, message, data } = res.data;
if (code === '000000') {
this.setData({
detailObx: data || {}
}, () => {
WxParse.wxParse('article', 'html', this.data.detailObx.content || '', this, 5);
if (this.data.detailObx.subscribe==2) {
// this.wxFuc()
}
console.log(this.data.detailObx)
});
} else {
// this.$toast(message);
wx.showToast({
title: message,
duration: 2000
})
}
}
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
console.log('---- on load -------', options)
this.getDetai();
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
*/
onReady: function () {
console.log('---- on ready -------')
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow: function () {
console.log('---- on show -------')
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面卸載
*/
onUnload: function () {
},
/**
* 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作
*/
onPullDownRefresh: function () {
},
/**
* 頁面上拉觸底事件的處理函數(shù)
*/
onReachBottom: function () {
},
/**
* 用戶點擊右上角分享
*/
onShareAppMessage: function () {
}
})