小程序新手一枚,在练习的过程中发现接口中内容存在html标签,
搜索了网上解决方wxParse,参考教程我的代码如下:
Json是这样的结构:
{
"meta": {
...
},
"pages": [
{...}
],
"posts": [
{
"title": "...",
"content": "<p>这里是带有html标签的文本。</p>\n"
},
{
"title": "...",
"content": "<p>这里是带有html标签的文本。</p>\n"
}
]
}
我的app.js:
App({
onLaunch: function (options) {
var that = this;
wx.request({
url: 'https://www.sfatpaper.com/content.json',
success(res) {
that.globalData.posts = res.data.posts
}
})
},
globalData: {
posts: ''//传递到子页面
}
})
我的logs.wxml:
<navigator url="/pages/details/details?id={{item.date}}" class="posts" wx:for="{{posts}}" wx:for-index="idx" wx:for-item="item" wx:key="idx">
<view class="title">{{item.title}}</view>
<text>{{item.text}}</text>
</navigator>
----------------通过一楼的回答已对问题作调整,目前问题代码如下--------------------
我的details.js:
// pages/posts/posts.js
var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
Page({
data: {
title: '',
contents: ''
},
onLoad: function (options) {
var that = this;
that.setData({
posts: app.globalData.posts
})
//筛选数据
that.data.posts.map(function (item, idx) {
if (item.date == options.id) {
//套用WxParse开始(此时通过map剩下的应该是唯一的一条内容数据)
console.log('item.content:'+item.content) //正常的数据
var parseText = WxParse.wxParse('article', 'html', item.content, that, 5)
console.log('parseText:' + parseText) //parseText:undefined
//套用WxParse结束
that.setData({
title: item.title,
content: item.content
})
}
})
}
})
点击列表将url中的参数带给详情页,然后详情页通过参数对posts数据进行过滤,最后得出那条唯一的内容,然后进行wxParse,但是不知道什么仍然会报给我 parseText:undefined
在前台渲染上,页面只渲染了文章的第一个段落。
posts是个数组,你直接posts.content读不到数据,要posts[0].content
你都采纳了还直接改问题,别人在外面看到都是已解决,就比较少点进来了,所以你另起一篇问题比较好:
如题,返回值为空是因为WxParse.wxParse()本身就没返回值;你之前不是有用正确的引入方式吗,现在怎么改成这样了。
html页面应该如下引用: