Closed rogeralsing closed 4 years ago
Well, I don't see you actually creatingf a router instance anywhere, all you do is install the plugin, but not use it, so ...
If your example code is incomplete, complete it. if that's actually all you have, please read the usage instructions for Vue Router again about how to set it up.
Setup is as follows:
const router = new VueRouter({
mode: "history",
routes: [
{
path: "/",
component: Content,
props: {user: state.user}
},
{
path: "/login",
component: Login,
props: {user: state.user}
},
{
path: "/register",
component: Register,
props: {user: state.user}
},
{
path: "/content",
component: Content,
props: {user: state.user}
},
{
path: "/profile",
component: Profile,
props: {user: state.user}
}
]
});
router.beforeEach((to, from, next) => {
...auth code...
next();
});
export default {
router, //<- the router
name: "App",
props: {
source: String
},
components: {},
data: () => ({
drawer: null,
state
}),
};
I see.
Well, root
points ot the root instance, which is the one created with new Vue()
.
You on the other hand, add the router not in root, but in the first child component, App (which is technically valid, but unusual). So in root, there's no router.
The usual way to add the router is to import it into main.js and add it to new Vue()
's options:
then it will work via root.$router as expected.
Many thanks, this solved it.
setup(props, vm) {
...
vm.root.$options.router.push({ name: "Overview" });
...
}
It works for me but the problem is that I don't have the router directly under the root.
Version: "@vue/composition-api": "^0.4.0"
// src/router/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router = new VueRouter({
...
})
export default router
import router from '@/router'
export default {
setup() {
const { currentRoute } = router
if (currentRoute.query.redirect) {
// ...
return
}
router.push({ name: "Overview" })
}
}
@F-loat this solution doesn't seem to work - currentRoute
returns /
when on any route
I'm coming up short on information how to access the $router when using composition API.
All leads point to
ctx.root.$router
, everyone I've asked says this should be the way to get hold of the router. But, it is undefined. Other plugins such as Vue Resource show up asctx.root.$http
. but router does not.My setup looks as such:
inside any
setup
, the above issue persists.Am I missing some step? is there some other way to access the router so that I can push new urls to navigate via code?