Nuxt.js: Routes transitions javascript hooks

Created on 3 Dec 2016  路  8Comments  路  Source: nuxt/nuxt.js

Any way to add the javascript hooks to the <transition>?

<transition
  v-on:before-enter="beforeEnter"
  v-on:enter="enter"
  v-on:after-enter="afterEnter"
  v-on:enter-cancelled="enterCancelled"
  v-on:before-leave="beforeLeave"
  v-on:leave="leave"
  v-on:after-leave="afterLeave"
  v-on:leave-cancelled="leaveCancelled"
>
  <!-- ... -->
</transition>

Maybe we could simply add transition: { hooks: true } as a viable option in nuxt-config? And then add the same as above into <transition> (seems overkill to be able to name the hooks yourself).

Thing to watch out for though:

It鈥檚 also a good idea to explicitly add v-bind:css="false" for JavaScript-only transitions so that Vue can skip the CSS detection. This also prevents CSS rules from accidentally interfering with the transition.

This feature request is available on Nuxt.js community (#c27)

Most helpful comment

Hi @PatrikLythell thanks for creating this issue, I thought about implementing it, it's now in the Roadmap for the 1.0.

I will try to find a nice way to implement it.

All 8 comments

Hi @PatrikLythell thanks for creating this issue, I thought about implementing it, it's now in the Roadmap for the 1.0.

I will try to find a nice way to implement it.

@PatrikLythell

It is now in the 0.9.0, the documentation is not updated yet but you can add the hooks like this:

nuxt.config.js:

module.exports = {
  transition: {
    beforeEnter: function () {
       // beforeEnter hook
    }
  }
}

It works also in the page components and the children components 馃憤
To disable CSS, juste like the documentation of Vue.js says, you can put css: false to disable CSS transitions.

You can see the list of keys & hooks here: https://vuejs.org/v2/api/#transition

appearClass, appearActiveClass, beforeAppear, appear and afterAppear are removed from the list because of the SSR feature of Nuxt.js.

@Atinux with 0.9.5 can't manage to trigger the hook from nuxt.config.js, is this known bug?

Hi @vadimsg

It will be possible in the next release.

In the meantime, you can add the hooks in your layouts/default.vue.

@Atinux FYI:

transition: { beforeEnter () { // beforeEnter hook } }
This makes build fail "Module build failed: SyntaxError: Unexpected token, expected , (42:55)"

transition: { beforeEnter: function () { console.log('beforeEnter'); } }
And this works. Need to upgrade global webpack v? Or it's nuxt bug?

@vadimsg it's a known issue of serialize-javascript that we use for shorthand method definition: https://github.com/yahoo/serialize-javascript/issues/24

For the hooks definitions in nuxt.config.js, you have to use the ES2015 syntax:

...
transition: {
  beforeEnter: function () {
     console.log('beforeEnter');
  }
}
...

@Atinux How should transitions within the same route be handled?

Example. /post-1 coming from another route will transitiion
However if there are related posts within post-1 if I click on another post inside that page post-2 will not transition because technically is within the same page

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danieloprado picture danieloprado  路  3Comments

shyamchandranmec picture shyamchandranmec  路  3Comments

msudgh picture msudgh  路  3Comments

nassimbenkirane picture nassimbenkirane  路  3Comments

VincentLoy picture VincentLoy  路  3Comments