Skip to content

Dynamically add routes #888

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

Closed
nkostadinov opened this issue Nov 8, 2016 · 9 comments
Closed

Dynamically add routes #888

nkostadinov opened this issue Nov 8, 2016 · 9 comments

Comments

@nkostadinov
Copy link

Could someone give me an example how to add routes(in browser) after the object is initialized. I'm trying to use createMatcher but there is not such function.

@LinusBorg
Copy link
Member

There is no publi API to do this.

@nkostadinov
Copy link
Author

The migration guide states that this is the way to add routes to already created router - https://vuejs.org/v2/guide/migration-vue-router.html#router-on-removed. Since this function seems private is there a way to achieve this ?

@LinusBorg
Copy link
Member

Hm, that section is new to me personally. and createMatcher is definitely not an export of the VueRouter instance. You can import it, though: import { createMatcher } from 'vue-router/create-matcher', but that it suboptimal because you will essentially duplicate this code, as it is already compiled in vue-router.js.

/ping @fnlctrl

@nkostadinov
Copy link
Author

Yes, I saw it is compiled but it is not in the prototype. I think it will be good if this function is available so we can add new routes because I cannot find another way to do it.

@fnlctrl
Copy link
Member

fnlctrl commented Nov 8, 2016

@LinusBorg Haven't read that section either.. Maybe we can add createMatcher to the exports?
Though I don't really think dynamically adding routes would be a good practice, since it would make the route map dependent on app state, and make the availability of urls unpredictable. e.g. one may want to share a link /foo, and be surprised that it can't be accessed by others (since /foo is dynamically registered later in app). I think it's better to stick with the static route map + route authentication approach, instead of dynamically registering routes.

@nkostadinov
Copy link
Author

I agree that this may lead to inconsistencies but in my case I want to load entire vue module(s) with ajax call from a PHP backend and I don't know all routes that shall be available so I thought of registering them later.

@fnlctrl
Copy link
Member

fnlctrl commented Nov 9, 2016

@nkostadinov Have you tried starting the app after the ajax call?

@nkostadinov
Copy link
Author

By the time the dashboard loads I am not aware of the modules in order to add their routes. My idea was when the user clicks on a link to load the template, routes and script from ajax call and then navigate to the new module - this way the module becomes standalone and self registering. Currently I'm stack at adding routes.

@yyx990803
Copy link
Member

closed via 0e0fac9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants