Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigating between pages with API call on beforeRouteEnter issue #308

Open
pedromelo222 opened this issue Jan 27, 2021 · 0 comments
Open

Comments

@pedromelo222
Copy link

pedromelo222 commented Jan 27, 2021

When navigation from page A to B with a different layout and page B needs to fetch data inside beforeRouteEnter, the child component of page A will be recreated within the layout of page B until next () is called and then destroyed again.

i think it's the same problem #192
the problem happens because the layout is changed within router.beforeEach () while beforeRouterEnter has not finished fetching data,

I fixed making the layout change only when the data is fetched (beforeResolve).

components/App.vue

methods: {    
    /**
     * Apply the application layout
     */
    applyLayout(){
      this.layout = this.nextLayout;
      this.nextLayout = null;
    },
     /**
     * Set the next page layout.
     *
     * @param {String} layout
     */
    setLayout(layout) {
      if (!layout || !layouts[layout]) {
        layout = this.defaultLayout;
      }
      this.nextLayout = layouts[layout];
    }
  }

router/index.js

/**
 * Create a new router instance.
 *
 * @return {Router}
 */
function createRouter () {
  const router = new Router({
    scrollBehavior,
    mode: 'history',
    routes
  })

  router.beforeEach(beforeEach)
  router.afterEach(afterEach)
  router.beforeResolve(beforeResolve)

  return router
}
async function beforeResolve (to, from, next) {
  router.app.applyLayout()
  next();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant