簡體   English   中英

捕鼠器綁定在 nuxt.js 頁面中不起作用

[英]Mousetrap bind won't work in a nuxt.js page

嗨,我希望有人可以幫助我。 我想在輸入某個序列后立即重定向到不同的頁面。 我在控制台中看到“它可以工作”,但它不會重定向。 我收到錯誤消息

“未捕獲的類型錯誤:無法讀取未定義的屬性(讀取 '$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>

我使用來自https://craig.is/killing/mice btw 的 Mousetrap 庫。

你有什么建議嗎?

謝謝你的回答......這對我有用。 將其更改為箭頭 function 以便 'this.$router' 可以保留為 vue 實例。

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

像這樣使用它解決了 OP 的問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM