Vue.js 如何在VueJS中使用window.onpopstate内的变量
我有一个Vue组件,如下所示: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 });
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;
}