1、动画

动画 将其封装为插槽 - 图1

  1. <template>
  2. <div class="home">
  3. <transition>
  4. <p v-show="isShow">hello world</p>
  5. </transition>
  6. <button @click="handleClick">toggle</button>
  7. </div>
  8. </template>
  1. <script>
  2. export default {
  3. name: 'home',
  4. data(){
  5. return {
  6. isShow:true
  7. }
  8. },
  9. methods:{
  10. handleClick(){
  11. this.isShow = !this.isShow
  12. }
  13. }
  14. }
  15. </script>
  1. Tipscoped避免样式干扰
  1. //注释掉的内容不需要关注
  2. Tipscoped避免样式干扰
  3. <style scoped>
  4. /* .v-leave,.v-enter-to{
  5. opacity: 1;
  6. } */
  7. .v-leave-active,.v-enter-active{
  8. transition: opacity 1s;
  9. }
  10. .v-leave-to,.v-enter{
  11. opacity: 0;
  12. }
  13. </style>

2、将动画封装成插槽

  1. //组件Fade.vue
  2. <template>
  3. <transition>
  4. <slot name="fade"></slot>
  5. </transition>
  6. </template>
  7. <script>
  8. export default {
  9. name:"Fade"
  10. }
  11. </script>
  12. <style lang="less" scoped>
  13. .v-leave-active,.v-enter-active{
  14. transition: opacity 2s;
  15. }
  16. .v-enter,.v-leave-to{
  17. opacity: 0;
  18. }
  19. </style>
  1. //使用
  2. <template>
  3. <div class="about">
  4. <fade>
  5. <h1 slot="fade" v-show="isShow">This is an about page</h1>
  6. </fade>
  7. </div>
  8. </template>
  9. <script>
  10. import Fade from '../components/Fade'
  11. export default {
  12. name: 'home',
  13. data(){
  14. return {
  15. isShow:true,
  16. isPlay:false
  17. }
  18. },
  19. components:{
  20. Fade
  21. }
  22. }
  23. </script>