vue 路由元信息和过渡动效

路由元信息


过渡动效

简单例子:
使用transition包住视图渲染标签

<transition name="fade">
      <router-view/>
    </transition>

使用官方提供的css

.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}
.fade-enter{
  opacity: 0;
}
.fade-leave-to{
  display: none;
}


代码:

<template>
  <div id="app">
    <div id="nav">
      <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link>
    </div>
    <transition :name="tName">
      <router-view/>
    </transition>
  </div>
</template>
<script>
  export default {
    data(){
      return{
        tName: ''
      }
    },
    watch:{
      '$route'(to,from){
        //判断点击哪个组件 赋不同的值
          if(to.name==='Home'){
            this.tName='fade'
          }
          else if(to.name==='About'){
            this.tName='fade2'
          }
        }
    }
  }
</script>
<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#nav {
  padding: 30px;
}

#nav a {
  font-weight: bold;
  color: #2c3e50;
}

#nav a.router-link-exact-active {
  color: #42b983;
}
.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}
.fade-enter{
  opacity: 0;
}
.fade-leave-to{
  display: none;
}
.fade2-enter-active, .fade2-leave-active {
  transition: opacity 2s;
}
.fade2-enter{
  opacity: 0;
}
.fade2-leave-to{
  display: none;
}
</style>
posted @ 2021-07-28 21:05  一个经常掉线的人  阅读(10)  评论(0编辑  收藏  举报