Giter VIP home page Giter VIP logo

Comments (7)

jorngeorg avatar jorngeorg commented on September 6, 2024 10

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.

boycce avatar boycce commented on September 6, 2024 2

Be cool to get attention on this issue. Guess i have to forgo HMR for now.

from vue-hot-reload-api.

jacob418 avatar jacob418 commented on September 6, 2024 1

It has been a while and it seems this still is an issue. Any updates on this?

from vue-hot-reload-api.

wadetandy avatar wadetandy commented on September 6, 2024

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.

zenghj avatar zenghj commented on September 6, 2024

me too

from vue-hot-reload-api.

SeoFood avatar SeoFood commented on September 6, 2024

yeah having the same issue, my workaround atm is while developing set the data in the mounted hook.

from vue-hot-reload-api.

igisev avatar igisev commented on September 6, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.