[英]Mousetrap bind won't work in a nuxt.js page
hi i hope somebody can help me.嗨,我希望有人可以帮助我。 I want to redirect to a different page as soon as a certain sequence is typed.我想在输入某个序列后立即重定向到不同的页面。 I get the "It works" in my console but it won't redirect.我在控制台中看到“它可以工作”,但它不会重定向。 I get the error message我收到错误消息
"Uncaught TypeError: Cannot read properties of undefined (reading '$router')" “未捕获的类型错误:无法读取未定义的属性(读取 '$router')”
Here is my code这是我的代码
<script>
export default {
head() {
return {
script: [
{
src: "js/mousetrap.min.js",
},
],
};
},
components: {},
name: "IndexPage",
mounted() {
Mousetrap.bind("1 2", function () {
console.log("It works");
this.$router.push("/pagename");
return;
});
},
};
</script>
I use the Mousetrap library from https://craig.is/killing/mice btw.我使用来自https://craig.is/killing/mice btw 的 Mousetrap 库。
Do you have some suggestions?你有什么建议吗?
Thank you for your answers... This did the trick for me.谢谢你的回答......这对我有用。 Changed it to an arrow function so 'this.$router' can be kept as vue instance.将其更改为箭头 function 以便 'this.$router' 可以保留为 vue 实例。
mounted() {
Mousetrap.bind("1 2", () => {
this.$router.push("/pagename");
});
}
Using it like this solved OP's issue:像这样使用它解决了 OP 的问题:
mounted() {
Mousetrap.bind("1 2", () => {
this.$router.push("/pagename");
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.