简体   繁体   English

Nuxt.js 页面转换,@leave 永远不会执行

[英]Nuxt.js page transitions, @leave never executing

I'm trying to have page transitions using with nuxt and Greensock (gsap), I can't seem to get it working.我正在尝试使用 nuxt 和 Greensock (gsap) 进行页面转换,但我似乎无法让它工作。 I don't want to animate page transitions using css only, because I want to easily chain animations on different elements.我不想只使用 css 为页面过渡设置动画,因为我想轻松地将动画链接到不同的元素上。

Previously, with Vue I could simply use:以前,使用 Vue,我可以简单地使用:

<transition @enter="enter" @leave="leave" appear mode="out-in">
  <router-view />
</transition>

export default {
  methods: {
    enter(el, done) {
      console.log('enter');
      // ... gsap animation code
    },
    leave(el, done) {
      console.log('leave');
      // ... gsap animation code
    }
  }
}

Now, with Nuxt, I can't seem to get it working.现在,有了 Nuxt,我似乎无法让它工作。 I use the exact same code in layouts/default.vue .我在layouts/default.vue中使用完全相同的代码。 Except I replace <router-view/> with <nuxt/> .除了我用<nuxt/>替换<router-view/> > 。

The "enter", "beforeEnter" methods works fine. “enter”、“beforeEnter” 方法工作正常。 It gets called on the initial page load, as well as anytime I change pages.它会在初始页面加载以及我更改页面的任何时候被调用。 However, the "leave" method never gets called.但是,永远不会调用“leave”方法。

What am I not understanding?我不明白什么? I just need one place to manage all of my page transitions with javascript.我只需要一个地方来使用 javascript 管理我的所有页面转换。 I've been googling for a while now, looking through lots of examples, I can't seem to figure out what I'm doing wrong.我已经在谷歌上搜索了一段时间,浏览了很多例子,我似乎无法弄清楚我做错了什么。

Spent a while trying to solve the same problem, and as far as I can tell you can't use the <transition> component to specify JS hook callbacks around a <nuxt/> component, presumably due to Nuxt handling routing differently.花了一段时间试图解决同样的问题,据我所知,您不能使用<transition>组件来指定围绕<nuxt/>组件的 JS 挂钩回调,可能是由于 Nuxt 处理路由的方式不同。

You need to define the transition property on each page, where enter() and leave() are specific to that particular page .您需要在每个页面上定义transition属性,其中enter()leave()特定于该特定页面 For example, if you are on the Home page and navigate to the About page, Home's leave() and About's enter() will both trigger.例如,如果您在 Home 页面并导航到 About 页面,Home 的leave()和 About 的enter()都会触发。

You can use a mixin to simplify adding different transitions to different pages:您可以使用mixin来简化向不同页面添加不同的转换:

~/mixins/transitions.js ~/mixins/transitions.js

export const typeA = {
  transition: {
    ...
  }
}

export const typeB = {
  transition: {
    ...
  }
}

Component零件

import { typeA } from '~/mixins/transitions'

export default {
  mixins: [
    typeA
  ]
}

You can also define a default transition in nuxt.config.js , but you will only have access to the page element, which might not be suitable for more complex transitions.您还可以在nuxt.config.js中定义默认转换,但您只能访问页面元素,这可能不适合更复杂的转换。

I had the same issue, I followed @mstslv and used transition property on page components, it's the best solution so far to get proper results.我遇到了同样的问题,我关注了@mstslv 并在页面组件上使用了转换属性,这是迄今为止获得正确结果的最佳解决方案。 You can programmatically do transitions by checking this.$route.path for instance.例如,您可以通过检查this.$route.path以编程方式进行转换。

If you want to avoid redundancy, you can use the following code如果要避免冗余,可以使用以下代码

import transition from "~/plugins/transitions"

export default {
..
transition,
..
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM