import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Detail from '../views/Detail.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/detail',
name: 'Detail',
component: Detail
},
{
path: '/about',
name: 'About',
component: () => import('../views/About.vue')
}
]
const router = new VueRouter({
routes
})
export default router
使用
进行路由跳转
使用
进行显示
使用redirect进行路由的重定向
{
path: '/',
name: 'Home',
component: Home,
redirect:'/detail'
},
{
path: '/detail',
name: 'Detail',
component: Detail,
},
{
path: '/detail',
name: 'Detail',
component: Detail,
children:[
{
path: 'info',
//以"/"开头的嵌套路径会被当作根路径,所以子路由上不用加"/";
name: 'Info',
component: Info,
},
]
},
$router.push
实现携带参数跳转this.$route.params.id
获取参数getDetail(id) {
this.$router.push({
path: `/detail/${id}`,
})
路由配置
{
path: '/detail/:id',
name: 'Detail',
component: Detail
}
this.$router.push({
name: 'Detail',
params: {id: id}
})
this.$router.push({
path: '/Detail',
query: {
id: id
}
})
router.beforeEach((to, from, next) => {
//to 前往的路由, from 来的路由, next 下一步钩子函数,没有问题,执行next()
console.log(to);
if (to.path !== '/login') {
if (window.isLogin) {
next()
} else {
next('/login?redirect='+ to.path)
}
} else {
next()
}
})