小程序引用html遇到的问题

先说引用html的方法:

1.下载wxParse
https://github.com/icindy/wxParse

2.添加到项目

3.引用

在需要使用转换的js中引入

var WxParse = require('../../wxParse/wxParse.js');//html转换为wxml
 
对应的wxss中引入
@import "/wxParse/wxParse.wxss";/* html转换为wxml */
对应的wxml中引入
<import src="../../wxParse/wxParse.wxml" />
 
<template is="wxParse" data="{{wxParseData:goodsDetail.nodes}}"/>
4.调用
js中使用
//获取商品详情
getGoodsDetail: function (){
var url = app.globalData.reqUrl + 'shop_goods/view';
var params = {
id: this.data.goodsId
, user_id: app.globalData.userId
,page: 1
,city: ""
};
app.request.requestGetApi(url, params, this, this.successFun_goodsDetail, this.failFun);
},

successFun_imgDetail: function(res,selfObj){ console.log(res); /** * WxParse.wxParse(bindName , type, data, target,imagePadding) * 1.bindName绑定的数据名(必填) * 2.type可以为html或者md(必填) * 3.data为传入的具体数据(必填) * 4.target为Page对象,一般为this(必填) * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选) */ // var that = this; WxParse.wxParse('shopDetail', 'md', res, selfObj, 0); },

遇到的问题

1.返回的html图片是相对地址

要改成绝对地址,需要修改html2json.js文件

if (node.tag === 'img') {
              node.imgIndex = results.images.length;
              console.log(app.globalData.baseUrl);
              var imgUrl = "项目中使用的图片地址路径" + node.attr.src;
                if (imgUrl[0] == '') {
                  imgUrl.splice(0, 1);
                }
                imgUrl = wxDiscode.urlToHttpUrl(imgUrl, __placeImgeUrlHttps);
                node.attr.src = imgUrl;
                node.from = bindName;
                results.images.push(node);
                results.imageUrls.push(imgUrl);

            }
原文地址:https://www.cnblogs.com/duanzhenzhen/p/11895837.html