vue router:如何触发嵌套组件的转换?

时间:2016-10-09 02:07:58

标签: vue.js vue-router

以下是该方案:

  • 有一个与vue-router
  • 重复使用的Page组件
  • 此页面组件包含动态组件Element1或Element2,具体取决于当前页面

当父路由更改时,如何在此动态嵌套组件上触发离开转换?

它应该与下面的代码一起使用,但是当您更改页面时,page beforeRouteLeaveelement leave永远不会打印到控制台。

(参见代码中的3x ------ BUG: never fired -----部分)



const Index = {
  template: `<div id="app">
    <router-link :to="{ path: '1' }">page 1</router-link> | 
    <router-link :to="{ path: '2' }">page 2</router-link>
    <router-view :key="'a' + $route.params.id"></router-view>
  </div>`
};

const Element1 = {
  template: `<transition @enter="transitionEnter" @leave="transitionLeave">
    <div class="element" v-show="show">Element 1</div>
  </transition>`,
  props: {
    show: Boolean
  },
  methods: {
    transitionEnter(el, done) {
      console.log('element 1 enter')
      done()
    },
    transitionLeave(el, done) {
      console.log('------ BUG: never fired -----')
      console.log('element 1 leave')
      done()
    }
  }
};

const Element2 = {
  template: `<transition @enter="transitionEnter" @leave="transitionLeave">
    <div class="element" v-show="show">Element 2</div>
  </transition>`,
  props: {
    show: Boolean
  },
  methods: {
    transitionEnter(el, done) {
      console.log('element 2 enter')
      done()
    },
    transitionLeave(el, done) {
      console.log('------ BUG: never fired -----')
      console.log('element 2 leave')
      done()
    }
  }
};

const Page = {
  template: `<transition @enter="transitionEnter" @leave="transitionLeave">
    <div>
      <p>page {{ id }}</p>
      <component :is="element" :show="elementShow"></component>
    </div>
  </transition>`,
  components: {
    Element1,
    Element2,
  },
  data() {
    return {
      id: this.$route.params.id,
      elementShow: false,
    }
  },
  computed: {
    element() {
      return 'Element' + this.id
    }
  },
  methods: {
    transitionEnter(el, done) {
      console.log('page', this.id, 'enter');
      done();
    },
    transitionLeave(el, done) {
      console.log('page', this.id, 'leave');
      done();
    },
  },
  mounted() {
    this.elementShow = true
  },
  beforeRouteLeave(to, from, next) {
    console.log('------ BUG: never fired -----')
    console.log('page beforeRouteLeave')
    this.elementShow = false
    setTimeout(next)
  },
};

const App = Vue.extend(Index);

const router = new VueRouter({
  routes: [{
    path: '/:id',
    component: Page
  }, {
    path: '*',
    redirect: '/1'
  }]
})

const app = new App({ router }).$mount('#app');
&#13;
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app"></div>
&#13;
&#13;
&#13;

0 个答案:

没有答案