reload vue 重新加载_在重新加载或在vue.js中关闭之前执行某些操作

I have a Vue.JS application with a beforeDestroymethod that I'm trying to use to store some data in local storage before the app closes or is reloaded. Just for reference:

beforeDestroy: function() {

localStorage.setItem('preference', this.preference);

...

}

However, neither closing nor reloading the app actually calls this method. How can I make sure this method is called before the app is closed?

解决方案

what is it that 'closes' or 'reloads' the app? is it the user in the browser closing the window and or refreshing the page? If that is the case, nothing is "destroyed" and that function won't run. You would have to define a onbeforeunload function globally:

window.onbeforeunload = function(){

return "Are you sure you want to close the window?";

}

then you can save items to local storage inside that function before the return statement.

hint: don't define it inside your beforeDestroy function, because again, that just wont run when you reload the page or navigate away.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值