鼠标陷阱绑定在 nuxt.js 页面中不起作用



hi我希望有人能帮我。我想在键入某个序列后立即重定向到另一个页面。我得到了";它工作";在我的控制台中,但它不会重定向。我收到错误信息

"未捕获的类型错误:无法读取未定义的属性(正在读取"$router"(";

这是我的代码

<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>

我使用Mousetrap库https://craig.is/killing/mice顺便说一下

你有什么建议吗?

感谢您的回答!!!这对我很有用。把它改成了箭头函数$router可以作为vue实例保存。

mounted() {
Mousetrap.bind("1 2", () => {
this.$router.push("/pagename");
});
}

这样使用它解决了OP的问题:

mounted() {
Mousetrap.bind("1 2", () => {
this.$router.push("/pagename");
});
}

最新更新