收藏
回答

初学小程序,测试switch,ReferenceError: XXX is not defined?

初学小程序,测试switch。新建项目,然后修改app.json,添加:

    "useExtendedLib": {
      "weui": true
    },
    "usingComponents": {
      "mp-cells": "weui-miniprogram/cells/cells",
      "mp-cell": "weui-miniprogram/cell/cell",
      "mp-form": "weui-miniprogram/form/form"
    }

修改index.wxml:

<!--index.wxml-->
<mp-cell title="测试" ext-class="">
  <switch slot="footer" bindchange="bindSwitchChange" />
</mp-cell>

修改index.js:

// index.js
Page({
  data: {
    shouldTest: true
  },

  bindSwitchChange(e) {
    this.setData({ shouldTest: e.detail.value });
    console.log(shouldTest);
  }
})

然后报错:

ReferenceError: shouldTest is not defined
    at zo.bindSwitchChange (index.js? [sm]:9)
    at Object.i.safeCallback (WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1)
    at WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
    at Cn (WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1)
    at WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
    at oe (WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1)
    at ie (WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1)
    at WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
    at WAServiceMainContext.js?t=wechat&s=1667317212484&v=2.27.0:1
    at o.emit (WAServiceMainContext.js?t=wechat&s=1667317212484&v=2.27.0:1)(env: macOS,mp,1.06.2209190; lib: 2.27.0)
errorReport @ WAServiceMainContext.js?t=wechat&s=1667317212484&v=2.27.0:1
thirdErrorReport @ WAServiceMainContext.js?t=wechat&s=1667317212484&v=2.27.0:1
(anonymous) @ WAServiceMainContext.js?t=wechat&s=1667317212484&v=2.27.0:1
(anonymous) @ WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
i.safeCallback @ WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
i.call @ WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
message @ WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
i.safeCallback @ WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
(anonymous) @ WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
Cn @ WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
(anonymous) @ WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
oe @ WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
ie @ WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
(anonymous) @ WASubContext.js?t=wechat&s=1667317212484&v=2.27.0:1
(anonymous) @ WAServiceMainContext.js?t=wechat&s=1667317212484&v=2.27.0:1
emit @ WAServiceMainContext.js?t=wechat&s=1667317212484&v=2.27.0:1
dispatch @ WAServiceMainContext.js?t=wechat&s=1667317212484&v=2.27.0:1
cb @ WAServiceMainContext.js?t=wechat&s=1667317212484&v=2.27.0:1
a @ VM146 asdebug.js:10
c @ VM146 asdebug.js:10
(anonymous) @ VM146 asdebug.js:1
f @ VM146 asdebug.js:1
g @ VM146 asdebug.js:1
(anonymous) @ VM146 asdebug.js:1
_ws.onmessage @ VM146 asdebug.js:1

请问我的问题出在哪里呢?谢谢!

回答关注问题邀请回答
收藏

4 个回答

  • 张业贵
    张业贵
    2022-11-02
    console.log(shouldTest);
    

    应该是

    console.log(this.data.shouldTest);
    
    2022-11-02
    有用 2
    回复
  • ⅴ
    2022-11-02

    报错说得是,index.js 这个文件第9行附近,bindSwitchChange函数内,有异常。

    shouldTest不能直接使用,需要this.data.shouldTest

    2022-11-02
    有用 1
    回复
  • 大名狗剩
    大名狗剩
    2022-11-02

    问题解决,感谢张业贵、StriveLuckyM和v!

    2022-11-02
    有用
    回复
  • CRMEB
    CRMEB
    2022-11-02

    我这现成的成品,免费的可以研究学习

    2022-11-02
    有用
    回复
登录 后发表内容