methods中的onHiddenLogin事件中的this,为什么我打印出来只有一个onHiddenLogin方法?不应该是当前组件的实例吗?
Component自定义组件中methods的this指向问题import { getLoginCode, login } from "../../api/index"; import { TOKEN } from "../../constant/index"; const app = getApp(); Component({ data: { hidden: true, height: "0%", padding: "0rpx", logo: "", appName: "", isAgree: false, }, properties: {}, observers: { hidden: function (hidden) { if (!hidden) { this.setData({ height: "68%", padding: "24rpx", logo: app.globalData.appConfig.logo, appName: app.globalData.appConfig.app_name, }); } }, }, methods: { onShowLogin: function () { this.setData({ hidden: false, }); }, onHiddenLogin: function () { console.log(this); this.setData({ hidden: true, height: "0%", padding: "0rpx", logo: "", appName: "", isAgree: false, }); }, onAgree: function () { this.setData({ isAgree: !this.data.isAgree }); }, onAgreement: function () { console.log("协议"); }, onLogin: async function () { if (!this.data.isAgree) return wx.showToast({ title: "请先勾选同意", icon: "none", duration: 1500, }); const loginCode = await getLoginCode(); const payload = { code: loginCode, appid: app.globalData.appConfig.appid, secret: app.globalData.appConfig.appsecret, key: app.globalData.appConfig.key, }; const { code, data } = await login(payload); if (code !== 0) return; wx.setStorageSync(TOKEN, data); this.onHidden(); this.onEmit(); }, onEmit: function () { // 执行父组件传递过来的方法 this.triggerEvent("onEmit"); }, }, options: { styleIsolation: "apply-shared", }, });
2023-11-20