动态转换组件VueJS 2

时间:2017-02-17 04:42:35

标签: components transition vuejs2

我希望能够动态地在转换A和B之间切换

父组件

<child></child>

子组件

过渡A

<transition name="fade">
    <p v-if="show">hello</p>
</transition>

.fade-enter-active, .fade-leave-active {
  transition: opacity .5s
}
.fade-enter, .fade-leave-to /* .fade-leave-active in <2.1.8 */ {
  opacity: 0
}

转换B(使用animate.css库)

 <transition
    name="custom-classes-transition"
    enter-active-class="animated tada"
    leave-active-class="animated bounceOutRight"
  >
    <p v-if="show">hello</p>
  </transition>

1 个答案:

答案 0 :(得分:2)

您可以通过正确分配enterClassleaveClass来进行动态转换。以下是工作演示:https://fiddle.jshell.net/j9h3Lmr5/1/

<强> JS

var vm = new Vue({
  el: '#vue-instance',
  data: {
    show: true,
    transitionType: "fade",
    enterClass: "fade-enter",
    leaveClass: "fade-enter-active"
  },
  methods: {
    changeTransition() {
      if (this.transitionType === "fade") {
        this.transitionType = "custom-classes-transition"
        this.enterClass = "animated slideInUp"
        this.leaveClass = "animated slideOutDown"
      } else {
        this.transitionType = "fade"
          this.enterClass = "fade-enter"
          this.leaveClass = "fade-enter-active"
      }
    }
  }
});