自定义了一个组件,在开发工具里面能正常运行,真机调试为啥获取不到组件中定义的数据?
发布于 4 年前 作者 xiulan44 9651 次浏览 来自 官方Issues
  1. 代码片段:https://developers.weixin.qq.com/s/496dXZmG7pf1
  2. 问题描述:自定义组件,然后在页面上引用,页面引用的时候设置了参数,在组件中使用这个参数;开发者工具里面参数获取正常,真机调试获取不到;
  3. 代码:

组件部分:

/** JS*/
// component/textToVoice.js
var Api = require('../../api/residents')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    voiceText: {
      type: String,
      value: '欢迎使用',
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },
  /**
   * 组件的方法列表
   */
  methods: {
    textToVoice: function () {
      /*********************************************************************/
      console.log(this.properties.voiceText)//此处真机无数据,开发者工具有数据*/
      /*********************************************************************/
      let that = this
      let filePath = wx.env.USER_DATA_PATH+"/"+that.properties.voiceText+".mp3"
      wx.getFileInfo({
        filePath:filePath,
        success: function (res) {
          console.log(res)
          play(filePath)
        },
        fail:function () {
          wx.downloadFile({
            url: Api.getVoiceByText+"?text="+that.data.voiceText,
            filePath: filePath,
            success:function (result) {
              play(result.filePath)
            },
            fail:function (err) {
              console.log("打印下错误",err)
            }
          })
        }
      })

    },
  }
})
var play = function (filePath) {
  console.log("准备播放声音",filePath)
  let InnerAudioContext = wx.createInnerAudioContext()
  InnerAudioContext.src = filePath
  InnerAudioContext.play()
}

===== wxml====

{{voiceText}}

组件使用

json

"usingComponents": {
  "text-voice": "./component/textToVoice/textToVoice"
}

wxml

    居民
  
    社区工作人员
  

3.效果

2 回复
   wx.downloadFile({
            url: Api.getVoiceByText+"?text="+that.data.voiceText,
            filePath: filePath,
            success:function (result) {
              play(result.filePath)
            },
            fail:function (err) {
              console.log("打印下错误",err)
            }
          })

这里是网络地址?有看打印吗?

回到顶部