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
相关文章
|
JavaScript
Vue找到package.json中没有用到依赖并删除
Vue找到package.json中没有用到依赖并删除
1488 55
springboot 集成 swagger 2.x 和 3.0 以及 Failed to start bean ‘documentationPluginsBootstrapper‘问题的解决
本文介绍了如何在Spring Boot项目中集成Swagger 2.x和3.0版本,并提供了解决Swagger在Spring Boot中启动失败问题“Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerEx”的方法,包括配置yml文件和Spring Boot版本的降级。
springboot 集成 swagger 2.x 和 3.0 以及 Failed to start bean ‘documentationPluginsBootstrapper‘问题的解决
IDEA @AutoWired注入bean 出现红色波浪线
IDEA @AutoWired注入bean 出现红色波浪线
1584 0
IDEA @AutoWired注入bean 出现红色波浪线
element ui confirm弹窗自定义样式小知识
element ui confirm弹窗自定义样式小知识
2549 0
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16790 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
5天前
|
云安全 数据采集 人工智能
|
6天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1204 2