ianho / xing-weapp-editor

开箱即用的微信小程序图文编辑组件
MIT License
105 stars 15 forks source link

通过 this.setData() 动态设置的html,编辑器无法展示 #3

Closed metanoia1989 closed 5 years ago

metanoia1989 commented 5 years ago

页面js文件

Page({
  data: {
    html: '',
    uploadUrl: '',
    key: ''
  },
  onLoad: function (options) {
    let uploadUrl = Api.getImageUpload();
    this.setData({
      uploadUrl: uploadUrl,
      html: wx.getStorageSync(options.key),
      key: options.key
    });
    console.log(this.data.html)
  },

wxml文件

<xing-editor
  bindfinish="finish"
  output-type="html"
  image-upload-url="{{uploadUrl}}"
  image-upload-name="article"
  image-upload-key-chain="result.url"
  html="{{html}}"/>

打印出来的this.data.html是有值的,但是页面没有展示html内容。 需要修改组件的js文件,把attached() 中的部分代码放到 ready() 中 xing-editor.js

  ready: function() {
    if (this.properties.nodes && this.properties.nodes.length > 0) {
      const textBufferPool = [];
      this.properties.nodes.forEach((node, index) => {
        if (node.name === 'p') {
          textBufferPool[index] = node.children[0].text;
        }
      })
      this.setData({
        textBufferPool,
        nodeList: this.properties.nodes,
      })
    } else if (this.properties.html) {
      const nodeList = this.HTMLtoNodeList();
      const textBufferPool = [];
      nodeList.forEach((node, index) => {
        if (node.name === 'p') {
          textBufferPool[index] = node.children[0].text;
        }
      })
      this.setData({
        textBufferPool,
        nodeList,
      })
    }
  },

微信文档上关于 attached()ready() 的解释:

attached 组件生命周期函数,在组件实例进入页面节点树时执行 |   ready 组件生命周期函数,在组件布局完成后执行,此时可以获取节点信息(使用 SelectorQuery )

菜鸟不是很懂啦。

ianho commented 5 years ago

fixed