Vue.js 如何在VueJS中使用window.onpopstate内的变量

Vue.js 如何在VueJS中使用window.onpopstate内的变量,vue.js,Vue.js,我有一个Vue组件,如下所示: Test.vue private input: string = ""; private pageNumber: number = 0; mounted() { window.onpopstate = function() { this.input = "test"; this.pageNumber = 1; } this.init({ page: this.page, input: this.input });

我有一个Vue组件,如下所示:

Test.vue

private input: string = "";
private pageNumber: number = 0;

mounted() {
    window.onpopstate = function() {
      this.input = "test";
      this.pageNumber = 1;
    }
    this.init({ page: this.page, input: this.input }); // reset search state when mounted
  }

但是如果我将
this.input=“test”
放在
window.onpopstate
函数中,则类型“WindowEventHandlers”上不存在带有
属性“input”的日志。

那么,如何在
窗口中为
this.input
赋值。onpopstate
函数


谢谢大家!

您正在使用function关键字创建函数。这意味着您的
可以更改此
。窗口事件,如
Window.onpopstate
始终将
this
设置为窗口。这意味着您正在
窗口
上设置
输入
页码
。要解决这个问题,可以使用arrow函数

window.onpopstate=()=>{
this.input=“test”;
此参数为0.pageNumber=1;
}

您正在使用function关键字创建函数。这意味着您的
可以更改此
。窗口事件,如
Window.onpopstate
始终将
this
设置为窗口。这意味着您正在
窗口
上设置
输入
页码
。要解决这个问题,可以使用arrow函数

window.onpopstate=()=>{
this.input=“test”;
此参数为0.pageNumber=1;
}