vuejs / router

🚦 The official router for Vue.js
https://router.vuejs.org/
MIT License
3.91k stars 1.19k forks source link

Migrating vue-router from v3 to v4 results in unexpected changes to Router signature from createRouter #1974

Closed arggrande closed 1 year ago

arggrande commented 1 year ago

Reproduction

n/a

Steps to reproduce the bug

Howdy folks, this is a bit of a strange one, but bear with me and I'll try and explain whats going on because I'm stumped and really need some help.

Steps

  1. We have an existing project with vue-router v3, which is using a utility function to export an instance of vue-router (where vue-router v3 instantiates the router via a Class)
  2. Upgrade to vue-router v4, where we also changed the Class invocation to use the function createRouter instead (as per the migration docs)
  3. Existing routing code that relied on currentRoute stops working, this includes things like;
    • accessing the params prop
    • accessing the query props

Expected behavior

Existing properties from currentRoute still be available and able to be accessed/read.

Actual behavior

Any code using currentRoute fails as the expected props arent on the Router object being created by the createRouter function in our utility function.

So for instance, if I have the following code:

const queryMessage = router.currentRoute.query.message;

The application will fail with a runtime error saying query is undefined (or whichever property we're trying to access on currentRoute that wouldve worked in vue-router v3.

Additional information

Some interesting things I've noticed while trying to figure this issue out;

This is where things get really hazy for me, because for all intents and purposes on the surface the signatures should be the same, with a currentRoute prop on the type that the createRouter function is returning.

Here is our utility function, migrated to vue-router v4... if something there could be playing havoc with the resolutions:

import { createRouter as createRouterV4, createWebHistory } from 'vue-router'

// ... other stuff

export const createRouter = (base, opts = {}) => createRouterV4({
  history: createWebHistory(base),
  routes: [],
  ...opts,
});

Environment info;

Vue version:

Vue-router version:

Vue CLI version:

Webpack version (embedded in the Vue CLI):

Any help or guidance on this would be greatly appreciated <3