请提供一个在页面内获取该页面实例的方法

发布于 7 年前作者 sunxiulan17806 次浏览最后编辑 7 年前来自 ask

能否像getApp一样提供一个获取当前Page实例的方法?

当多个function跟页面事件多重嵌套的时候,this的指向会发生错误,所以无法调用Page的setData方法…

求解。

3 回复
jie96
jie961 楼6 年前

谢谢1楼,这波我真笨

但还是希望有一个getPageByName()方法

xiuyingmo
xiuyingmo2 楼6 年前

自己在js里定义个全局变量,然后onLoad时候吧this赋值给这个全局变量就可以很方便的随意调用了。例如

var top_this={}

Page({

    onLoad: function () {

        top_this=this;

    },

    a:function()

    {

            wx.getSystemInfo({

                 success: function(res) {

                        top_this.setData({b:res.windowWidth});

                  }

           })

 

    }

})

当然,可能有更好的方法能获取Page,这个楼主找到了也告诉我一下

jie16
jie163 楼5 年前

function a()

{

    var this_a=this;

    function b()

    {

         var this_b=this;

        function b()

        {

            //这里可以取到this_a,this_b

            //无论套多少层多复杂。

            //so,你的基本功不扎实

        }

    }

}