I just learned Vue not long ago. I made a simple note according to the online demo, using localstorage for local storage. Then I want to call the localstorage method when refreshing the page or leaving the page. Can this be achieved? If so, how? Thank you for your answer, I appreciate it!
Generally when developing using Vue, you don’t need to touch
window.onload
such DOM-related APIs. Instead, use the component life cycle hook encapsulated by Vue:Use life cycle functions, choose which one to use according to your needs, refer to https://cn.vuejs.org/v2/api/#Options-Life Cycle Hook
Use localstorage for local storage, and then I want to call the localstorage method when refreshing the page or leaving the page
1. Use localstorage for page refresh, that is, after Vue is instantiated, the following are available for you to use:
These can operate local storage when the page is refreshed.
Note: In fact, local storage does not need to be written in the vue instance. It essentially has nothing to do with vue. You just execute a piece of js when the page is refreshed
main.js
2. Page closing has nothing to do with the life cycle of vue, and there is no such thing as destruction. Therefore, there is no way for you to operate localStorage when closing the page. This is different from what the people above said.