CNode

babel ES6 中promise this指向问题

问答
AAoqin发布于9 年前最后回复9 年前2 回复4772 浏览0 收藏

想请教下,用babel 处理 Class中 有promise 这种的时候,用lam,和bind方法都无法解决this指向问题。 在上层定义 self=this 可以. class test { construct(){ this.user={ name:'test' }; } getInfo(){ promise.then(data=>{ promise2 }).then( this.test() ) ) } test (){ console.log(this.user) } }

一般是怎么处理这种方式的啊,纠结了。

查看回复

回复 (2)

I
i5ting#1·9 年前

自己绑定this上下文吧

    _exec() {
        let self = this;

        debug('Pagelet ' + this.domid + ' fetch');

        // 1) this.before
        // 2)fetch,用于获取网络数据,可选
        // 3) parse,用于处理fetch获取的数据
        // 4)render,用与编译模板为html
        // 5)this.end 通知浏览器,写入完成

        return self.before()
            .then(self.fetch.bind(self))
            .then(self.parse.bind(self))
            .then(self.render.bind(self))
            .then(self.end.bind(self))
            .catch(self.catchFn)
    }
A
Aoqin#2·9 年前
引用 i5ting自己绑定this上下文吧

@i5ting 谢谢,我试试

参与回复
登录后即可参与回复。登录