Comments (7)
I'm also having this issue where all my routes fetch data in the beforeRouteEnter
hook as prescribed in the router docs , however this hook is not fired by hot-reload and all data are removed from the component, forcing me to do a full manual reload of the page.
Will the router hooks be supported by hot-reload or are we left with cmd+r
for the unforeseeable future?
from vue-hot-reload-api.
Be cool to get attention on this issue. Guess i have to forgo HMR for now.
from vue-hot-reload-api.
It has been a while and it seems this still is an issue. Any updates on this?
from vue-hot-reload-api.
I have been running into this issue this week while looking at moving our product to using beforeRouteEnter
guards to preload data. Seems this becomes a non-starter from a development experience though if every time we change the file I have to do a full reload and lose my HMR because the fetched data vanishes?
Any updates or guidance would be appreciated.
from vue-hot-reload-api.
me too
from vue-hot-reload-api.
yeah having the same issue, my workaround atm is while developing set the data in the mounted hook.
from vue-hot-reload-api.
Workaround for Vue2
It will be useful in 95%+ cases when editing a component.
In other cases, you will need to reload the page.
// unique for component
let hmrName = 'some-string';
export default {
created() {
if (window[hmrName]) {
Object.assign(this._data, window[hmrName]._data);
}
},
mounted() {
window[hmrName] = this;
},
destroyed() {
delete window[hmrName];
},
}
As a mixin with parameters
// src/mixins/hmr.js
export function vHmrStateMixin(name) {
// NOTE: use only in DEV
if (!process.env.DEV) {
return {};
}
let store = window._devHmr = window._devHmr || Object.create(null);
return {
created() {
if (store[name]) {
Object.assign(this._data, store[name]._data);
}
},
mounted() {
store[name] = this;
},
destroyed() {
delete store[name];
},
};
}
// src/views/Home.vue
export { vHmrStateMixin } from '@/mixins/hmr';
export default {
mixins: [vHmrStateMixin('home')],
beforeRouteEnter(to, from, next) { ... },
}
Explanation
HMR executes hooks:
NEW.beforeCreate
NEW.created
NEW.beforeMount
OLD.beforeDestroy
OLD.destroyed
NEW.mounted
The order of events is important.
We restore the data (_data
) as early as possible, in the created
hook, when the data has already been initialized. The beforeCreated
hook doesn't work for us.
Saving in the mounted
hook, and cleaning up in the destroyed
hook. These are the only places where we can do it.
When the component is destroyed (for example, when changing the page), the destroyed
hook will be called, which will remove the reference to the last instance of the component from window
.
There will be NO memory leaks =)
We use window
because we need global storage, but <script>
inside * .vue
files has local scope.
from vue-hot-reload-api.
Related Issues (20)
- I have a problem whith vue-hot-reload-api HOT 3
- Handle custom option merge strategies
- v2.1.1 cause throw error:[vuex] Do not mutate vuex store state outside mutation handlers HOT 6
- Error when using with SSR HOT 2
- please add tags for releases HOT 1
- TypeError: Cannot read property 'extend' of undefined HOT 14
- Support for Vue 2.5+ with Typescript? HOT 1
- 热部署时,同类型vue组件顺序修改与不同类型vue组件顺序修改,对于$children产生效果不同
- Initial data lost in component when updating a mixin
- Component extends another component lost its extended methods after hot reload HOT 5
- [Bug] Cannot create property 'xxx' on boolean 'true' with Vue 2.6.7 HOT 1
- Hot reload not working with global functional components
- Hot reload and memory leak (functional components) HOT 3
- Add comment to one line of example
- rerender throws an exception when component options are missing a render function
- Hot reloading of extended components doesn't work when base component changes HOT 3
- How to keep custom properties when reloading components
- No reactivity with data from websocket when using npm run serve
- It should actually do a full reload when it's required, not just reporting a need
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-hot-reload-api.