0
Well I just used this route
var route = [
{
path: '/*',
name: 'all',
component: () => import('./component/All.vue')
}
];
0
You need to import vue router. You can choose to import vue router when creating a new project or later by adding vue router to the project.
Once you do that you need to define your routes. Please follow this link โ https://scotch.io/tutorials/getting-started-with-vue-router.
Basically if you include router option while creating new project most of work is done for you by cli.
0
To dynamically add route to existing router, you could use router.addRoutes([])
as follow :
router.js :
Vue.use(Router)
// define your static routes
var route = [
{
path: '/home',
name: 'Home',
component: Home
}
];
export default new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: route
})
anywhere else
import router from '@/router'
router.addRoutes([{
path: '/awards',
name: 'Awards',
component: () => import('./views/Awards.vue')
},
{
path: '/news',
name: 'News',
component: () => import('./views/News.vue')
},
{
path: '/product',
name: 'Product',
component: () => import( './views/Product.vue')
},
{
path: '/page',
name: 'Page',
component: () => import('./views/Page.vue')
}
])
router.push('/page')
UPDATE: another example with promise
import router from '@/router'
const getRoutesFromApi = () => {
return new Promise((resolve, reject) => {
const data = [
{
path: '/awards',
name: 'Awards',
component: () => import('./views/Awards.vue')
},
{
path: '/news',
name: 'News',
component: () => import('./views/News.vue')
},
{
path: '/product',
name: 'Product',
component: () => import( './views/Product.vue')
},
{
path: '/page',
name: 'Page',
component: () => import('./views/Page.vue')
}
]
setTimeout(resolve(data), 5000)
})
}
getRoutesFromApi().then(response => router.addRoutes(response))
UPDATE 2 : more concrete example
import router from '@/router'
import axios from 'axios'
axios.get('http://your-api/whatever').then(response => {
// response contains your data as json, you have to fetch them to get vuejs route-like object
const routes = JSON.parse(response.data).map(o => {
// here you have to transform your data in vuejs route item for example
return {
path: o.menu_url
name: o.menu_item + 'View'
component: () => import('./views/' + o.menu_item + '.vue')
}
})
router.addRoutes(routes)
}
Source:stackexchange.com