主题
vue-router4
准备
引包
shellnpm install vue-router --save
在 vue 实例中使用
jsimport router from './router' app.use(router)
模板
router.js
js
import { createRouter, createWebHistory } from 'vue-router'
import Home from './views/Home.vue'
const routerHistory = createWebHistory()
export default createRouter({
history: routerHistory,
routes: [
{
path: '/',
name: 'home',
component: Home
}
]
})
在模本中使用 vuex4,实现路由拦截
jsimport store from './store' router.beforeEach((to, from, next) => { if(to.meta.requireLogin && !store.state.user){ next('/login') }else { next() } })
使用
useRoute
——router
对象,可以拿到路由信息,如域名,哈希等jsimport { useRoute } from 'vue-router' const route = useRoute() const id = route.params.id
useRouter
——router
对象,可以使用路由jsimport { useRouter } from 'vue-router' const router = useRouter() router.push('/')
过渡动效
vue
<router-view v-slot="{ Component }">
<transition name="slide">
<component :is="Component" />
</transition>
</router-view>
css
.slide-enter-active, .slide-leave-active {
transition: all 0.3s;
}
.slide-enter-from, .slide-leave-to {
transform: translate(100%, 0)
}