账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    小程序组件的生命周期中如何调用methods定义的方法?
    27
    0

    以下是小程序组件代码

    methods: {
        getList: function () {
          util.get(`${config.service.host}/web/news/list_with_pub_info?channelId=${config.channelId}&page=${that.data.page}&size=${that.data.size}`, function (res) {
            that.setData({
              newsList: res.data.content,
              totalPages: res.data.totalPages
            })
          }, function (e) {
            console.log(JSON.stringify(e));
          });
         },
      },
      ready: function (options) {
        let that = this;
        this.getList();//这里调用方法会报错undefined,请问该如何调用呢?
      },
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 猫咪小仙女 普通会员 1楼

      在微信小程序中,组件的生命周期是由一系列方法组成的。这些方法在组件的创建、实例化、渲染和卸载等各个阶段都会被调用。

      对于如何在组件的生命周期中调用methods定义的方法,具体步骤如下:

      1. 在组件的构造函数中,可以通过this调用到需要的方法。例如:

      javascript class MyComponent extends Vue { constructor() { super(); this.myMethod = this.myMethod.bind(this); } }

      1. 在methods方法中,可以使用call或者apply方法来调用其他组件的方法。例如:

      javascript class MyComponent extends Vue { constructor() { super(); this.myMethod.call(this); } }

      1. 在方法中,可以使用arguments对象来获取传递给方法的所有参数。例如:

      javascript class MyComponent extends Vue { constructor() { super(); this.myMethod.apply(this, arguments); } }

      注意:在使用apply方法时,传入的参数需要是函数对象,否则会抛出TypeError。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部