向WXML Template模板 传函数的诡异方式?&如何绑定this指针?
发布于 6 年前 作者 xiuying55 13168 次浏览 来自 问答

a.template.wxml

<ptemplate name='a'>

    <view bindptap="onTap">点我</view>

</templpate>

b.wxml

..p.

<tempplate is="a" data={{ onTap:'handleTap' }} />

b.js

page({

    ...

    handleTap : e => {

        console.log(e,this);        //重点:输出this为undefined

    }

})

问题1:只有通过 data={{ onTap:‘handleTap’ }} 这种方式 注入函数到模板而不是直接传函数引用?能解释下原理么?

问题2:传入模板的函数被回调时,没有this指针,如何绑定指针?

回到顶部