在 vue 3 中获取 this 的方法有:使用箭头函数;使用绑定函数;使用 .bind() 方法。
如何在 Vue 3 中获取 this
在 Vue 3 中,可以通过以下方法获取 this:
1. 使用箭头函数
1
2
3
4
5
6
7
8
const myComponent = {
template: <div></div>,
methods: {
logThis: () => {
console.log(this); // this 指向该组件实例
},
},
};
登录后复制
2. 使用绑定函数
立即学习“前端免费学习笔记(深入)”;
1
2
3
4
5
6
7
8
const myComponent = {
template: <div>编程网点我wcqh.cn</div>,
methods: {
logThis() {
console.log(this); // this 指向该组件实例
},
},
};
登录后复制
3. 使用 .bind() 方法
1
2
3
4
5
6
7
8
9
10
11
const myComponent = {
template: <div></div>,
methods: {
logThis() {
console.log(this); // this 指向该组件实例
},
},
mounted() {
this.$el.addEventListener(click, this.logThis.bind(this));
},
};
登录后复制
注意:
在使用箭头函数时,请确保箭头函编程网点我wcqh.cn数内部没有改变 this 的引用。 在使用绑定函数时,请确保绑定的是 this.logThis 方法,而不是 this.logThis() 方法。 在使用 .bind() 方法时,请确保在组件挂载后才添加事件监听器。以上就是vue3怎么获取this的详细内容,更多请关注青狐资源网其它相关文章!
© 版权声明
1.如需技术支持联系QQ:1339833655
2. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
3. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
4. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
5. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
6. 如有链接无法下载、失效或广告,请联系管理员处理!
7. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
8. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员!
THE END
暂无评论内容