Tencent / wepy

小程序组件化开发框架
https://wepyjs.gitee.io/wepy-docs/
Other
22.48k stars 3.05k forks source link

wepy.wx.getSetting 无法直接调用 #2378

Open UICJohn opened 4 years ago

UICJohn commented 4 years ago

Description

项目用了promisify page A 里面有一个div 设定了 @tap 事件触发 newPost 函数, newPost函数内有wepy.wx.getSetting,但是无法调用,也没有任何报错.

Environment

Reproduce

创建wpy文件,添加div并设定@tap事件,在事件函数内直接调用wepy.wx.getSetting

Observed Results

只打印了 get in

Expected Results

console 打印 get in 并且打印 get setting 或者 error message

Relevant Code / Logs

index.js

<div class="cu-btn block bg-blue sm radius" @tap="newPost">

methods: {
     newPost(e) {
        console.log('get in')
        wepy.wx.getSetting().then((res) => {
          console.log('get setting')
        }).catch((err) => {
          console.log(err);
        })
      }
  }
SpringHgui commented 4 years ago

2.0.x版本 wepy类中没有封装 wx 对象,直接使用

wx.getSetting({
     success (res) {
          console.log(res.authSetting)
     }
})
UICJohn commented 4 years ago

想问一下如果使用了promisify的话在2.0.x中应该如何调用? 直接wx.getSetting().then()? 还有就是我在console.log(wepy.wx)是可以打印出object的, 感觉没毛病, 而且getSetting 时好时坏, 我在同一个文件中的两段代码里面调分别用了getSetting,第一段的可以使用, 第二段的就怎么都调用不了,然后改成wx.getSetting就行。

这段代码可以正确调用getSetting
chooseLocation(e) {
        wepy.wx.chooseLocation().then((res) => {
          this.location = {
            name: res.name,
            address: res.address,
            longitude: res.longitude,
            latitude: res.latitude
          }
          getLocationByGeo({longitude: res.longitude, latitude: res.latitude}).then(({ result }) => {
            this.location = {
              ...this.location,
              city: result.ad_info.city,
              district: result.ad_info.district,
            }
          }).reject((err) => {
            console.log(err);
          })
        }).catch((err) => {
          wepy.wx.getSetting().then((res) => {
            if(res.authSetting.length != 0 && !res.authSetting["scope.userLocation"]) {
                console.log('choose location')
            }
          })
        })
      },
这段代码不打印get setting,也不报错
     newPost(e) {
        console.log('get in')
        wepy.wx.getSetting().then((res) => {
          console.log('get setting')
        }).catch((err) => {
          console.log(err);
        })
      }
SpringHgui commented 4 years ago

use-promisify用法参考 wepy/use-promisify

源码 index.js

UICJohn commented 4 years ago

我之前试过这么用,如果根据这个文档直接用wepy.getSetting()的话会报错

Uncaught (in promise) TypeError: _core.default.getSetting is not a function
    at home.js:95