page.js中的const变量如何在page.wxml使用?
发布于 6 年前 作者 ming48 1820 次浏览 来自 问答

你好,我在page.js写下如下的代码

onLoad: function(options) {
  wx.getLocation({
    type: 'wgs84',
    // debugger;
    success(res) {
      const latitude = res.latitude
      const longitude = res.longitude
      console.log(latitude)
      console.log(longitude) //correct but how to trans this to wxml
      // console.log("Love it")
      // const speed = res.speed
      // const accuracy = res.accuracy
    },
    // debugger;
    fail(res) {
      // console.log("Hate it")
    }
  })

编译调试时,可以正常在console中显示latitude和longitude。

但是当我在page.wxml中使用时,

<view>
  <navigator url="/pages/create_page/create_page" redirect hover-class='other-navigator-hover'></navigator>
  <text>{{"The longitude is" + longitude + ", and the latitude is" + latitude+"."}}</text>
  <text>"{{res.latitude}}"</text>
  <text>"{{res.longitude}}"</text>
  <text>{{latitude}}</text>
</view>

要么显示undefined,要么就是直接跳过。

麻烦请教一下该如何解决呢?

Any Suggestion Will Help.

1 回复

js:

onLoad: function() {

let that = this //新加的

wx.getLocation({

type: ‘wgs84’,

// debugger;

success(res) {

const latitude = res.latitude

const longitude = res.longitude

console.log(latitude)

console.log(longitude) //correct but how to trans this to wxml

/*新加的*/

that.setData({

accuracy: res.accuracy

})

// console.log(“Love it”)

// const speed = res.speed

// const accuracy = res.accuracy

},

// debugger;

fail(res) {

// console.log(“Hate it”)

}

})

},

wxml:

<text>{{accuracy}}</text>

回到顶部