vue-router4使用
目录结构
app>src>router>index.js
>main.js
>App.vue
router/index.js
书写配置文件(只有两个组件的路由)注意,关于数据获取,最好在每个路由中进行分别获取!
import { createRouter, createWebHashHistory } from 'vue-router';
import Detail from '../components/detail/detail';
import MainList from '../components/main-list/main-list';
const router = createRouter({
history: createWebHashHistory(),
routes: [
{
path: '/',
redirect: '/home'
},
{
path: '/home',
name: 'home',
component: MainList,
},
{
path: '/detail',
name: 'detail',
component: Detail,
}
]
});
export default router;
在main.js中引用
import { createApp } from 'vue';
import App from './App.vue';
import router from './router/index';
import fastclick from 'fastclick';
import VConsole from 'vconsole';
import 'common/less/index.less';
fastclick.attach(document.body);
let vConsole = new VConsole();
createApp(App).use(router).use(vConsole).mount('#app');
vue-router4安装
npm install vue-router@4
或者
<script src="https://unpkg.com/vue-router@4"></script>
在App.vue中使用
<router-view v-slot="{ Component }">
<transition name="aside-right" appear>
<component :is="Component" />
</transition>
</router-view>
// 跳转到路由
this.$router.push({
name: 'detail',
query: {
target: item.target
}
})
使用params传递参数:
// 静态引用会在首屏加载时候载入全部组件,所以这里路由应该改为动态加载
import { createRouter, createWebHashHistory } from 'vue-router'
import ViewHome from '@/views/home/home';
import ViewEdit from '@/views/edit/edit';
// 路由配置
const routes = [
// 配置当访问根路径/的时候,跳转到/recommend的路径 渲染Recommend组件
{
path: '/',
redirect: '/home'
},
{
path: '/home',
name: 'home',
component: ViewHome,
children: []
},
{
path: '/edit/:id',
name: 'edit',
component: ViewEdit,
// 指定子路由 每个对象都是子路由配置
children: []
}
];
const router = createRouter({
history: createWebHashHistory(),
routes
});
export default routers
使用name+params传递:
openEdit(id, index) {
this.$router.push({
name: 'edit',
params: {
id: id
}
});
},
使用path:
openEdit(id, index) {
this.$router.push({
path: `/edit/${id}`
});
},
v-slot="{ Component } 中的Component就是路由中定义的指向的组件!即本例中的MainList和Detail!
关于使用时的问题,以及给路由的组件添加过度动画,请参见 vue-router4过度动画无效解决方案
如果要在router-view中使用路由元信息,比如实现每个路由有不同的过度
const router = createRouter({
history: createWebHashHistory(),
routes: [
{
path: '/',
redirect: '/home'
},
{
path: '/home',
name: 'mainlist',
component: MainList,
// 路由元信息定义每个路由进入退出的不同动画
meta: { transition: 'fadeIN' }
},
{
path: '/detail',
name: 'detail',
component: Detail,
meta: { transition: 'aside-right' }
}
],
<!-- 这里Component就是指的router中注册的组件, route就是每个路由的数组routeS -->
<router-view v-slot="{ Component, route }">
// 动态读取绑定路由元信息,v-slot = {route}就是配置中的routes数组
<transition :name="route.meta.transition" appear>
<!-- 加入keep-alive保证进入退出都有动画 -->
<!-- exclude排除缓存,让其不缓存数据 -->
<keep-alive exclude="detail">
<component :is="Component"
@closeDetail="监听组件派发的特定事件"
:all="给路由组件传递的数据"
/>
</keep-alive>
</transition>
</router-view>
注意上面的exclude="detail"中的detail指的是组件的名称!,并非是路由中的name!比如:
<router-view v-slot="{ Component, route }">
<transition :name="route.meta.transition"
appear
>
<keep-alive exclude="video-detail">
<component :is="Component"></component>
</keep-alive>
</transition>
</router-view>
其中的video-detail指的是video-detail组件中的name!
export default {
name: "video-detail",
data() {。。。
},
并不是router/index.js 配置文件中的路由名称!
routes: [
{
path: '/',
redirect: '/home'
},
{
path: '/home',
name: 'home',
component: Home,
},
{
path: '/detail/:id',
name: 'detail', // 排除的名称不能以路由配置文件中的为准!!
component: VideoDetail,
meta: { transition: 'aside-right' }
}
],
exexclude="video-detail"之后,vue就不会缓存该组件,每次进入的时候都会执行created()钩子。