Pinia+Router学习笔记(十四)

简介: 本节记录Vue-Router中路由过渡动效相关内容

在进行路由跳转的时候我们也可以设置一些动效,让整个过程看起来没那么枯燥(这里使用animate.css动画库)

import { createApp, toRaw } from 'vue'
import router from './router'
import App from './App.vue'
// 引入动画库
import 'animate.css'
const app = createApp(App)

app.use(router)

app.mount('#app')
<template>
  <div>
    <router-view #default="{ route, Component }">
      <transition
        :enter-active-class="`animate__animated animate__backInRight`"
        >
        <component :is="Component"></component>
      </transition>
    </router-view>
  </div>
</template>

<script setup lang="ts">
  import { ref, reactive } from 'vue'
</script>

<style scoped></style>

router-view有一个默认作用域插槽,接收两个变量route和Component,route包含跳转时的一些路由信息,Component则是VNode,可通过动态组件展示在页面上。上图这样的处理方式让所有的路由都使用animate__backInRight这个动画,如果想让不同路由组件使用不同动画可以在meta中定义动画名

<template>
  <div>
    <router-view #default="{ route, Component }">
<!--       定义动画类名,从route.meta中取 -->
      <transition
        :enter-active-class="`animate__animated ${route.meta.transition}`"
        >
        <component :is="Component"></component>
      </transition>
    </router-view>
  </div>
</template>

<script setup lang="ts">
  import { ref, reactive } from 'vue'
</script>

<style scoped></style>
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

// declare声明模块防止类型报错
declare module 'vue-router' {
  interface RouteMeta {
    transition: string
  }
}

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    meta: {
      // 定义动画名
      transition: 'animate__backInRight',
    },
    component: () => import('../components/login.vue'),
  },
  {
    path: '/reg',
    component: () => import('../components/reg.vue'),
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

router.beforeEach((to, from, next) => {
  next(true)
})

export default router
相关文章
|
4月前
|
前端开发
如何使用react-router v6快速搭建路由?
如何使用react-router v6快速搭建路由?
108 0
|
4月前
|
存储 前端开发 JavaScript
第三十章 React的路由基本使用
第三十章 React的路由基本使用
|
4月前
|
JavaScript 网络架构 开发者
阿珊解说Vue中`$route`和`$router`的区别
阿珊解说Vue中`$route`和`$router`的区别
|
11月前
|
移动开发 前端开发 API
React-Router-基本使用
React-Router-基本使用
57 0
|
存储 移动开发 JavaScript
vue Router从入门到精通
Vue Router 是 Vue.js 的官方路由。它与 Vue.js 核心深度集成,让用 Vue.js 构建单页应用变得轻而易举。功能包括: 嵌套路由映射 动态路由选择 模块化、基于组件的路由配置 路由参数、查询、通配符 展示由 Vue.js 的过渡系统提供的过渡效果 细致的导航控制 自动激活 CSS 类的链接 HTML5 history 模式或 hash 模式 可定制的滚动行为
61 0
|
存储 JavaScript 前端开发
Vue —— 进阶 vue-router 路由(零)(路由的概念、基本使用)
Vue —— 进阶 vue-router 路由(零)(路由的概念、基本使用)
|
运维 JavaScript API
Vue-Router总结大全,从小白到精通,含vue3.0路由用法
Vue-Router总结大全,从小白到精通,含vue3.0路由用法
281 0
Vue-Router总结大全,从小白到精通,含vue3.0路由用法
Pinia+Router学习笔记(十二)
本节记录Vue-Router的两种路由重定向方式
108 0
|
前端开发
Pinia+Router学习笔记(十五)
本节记录Vue-Router中关于路由滚动行为的相关内容
66 0
Pinia+Router学习笔记(十一)
本节记录嵌套路由&命名视图相关内容
109 0