概要
在进行owl组件式开发中,经常会遇到组件方法中的this指向出现问题的情况,那么这个时候如何解决呢?
内容
组件视图:
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="demo.ListLineRenderer">
<a href="#" t-on-click.stop.prevent="()=>go_field_demo('demo.bom.line',line.bom_position_int_id)">
demo
</a>
</t>
</templates>
go_field_demo(){
cosnole.log(this)
}
这个时候我要是在代码中调用方法go_field_demo,并且打印它的this,发现它是undefined。
解决方法:
在构造函数中用bind进行绑定this就可以了!
setup() {
super.setup(...arguments);
this.go_field_demo= this.go_field_demo.bind(this);
}
小结
多写多敲多思考,毕竟,知己知彼才能看懂源码。
Tip:本人才学尚浅,如有纰漏,还请不吝赐教!