简体   繁体   English

Vue.js 命名为 javascript 钩子

[英]Vue.js named javascript hooks

I am trying to hook up Vue.js with velocity.js.我正在尝试将 Vue.js 与velocity.js 连接起来。 The guide gives an example but does not use a named transition.该指南提供了一个示例,但没有使用命名转换。 Currently my transition looks like this:目前我的过渡看起来像这样:

<transition name="collapse">

https://v2.vuejs.org/v2/guide/transitions.html#JavaScript-Hooks https://v2.vuejs.org/v2/guide/transitions.html#JavaScript-Hooks

The example given from the Vue.js documentation sets up a transition element like so: Vue.js 文档中给出的示例设置了一个过渡元素,如下所示:

<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"
>

Is there not a way to do this automatically, without having to set all of this up beforehand?有没有办法自动执行此操作,而无需事先设置所有这些? The second step of course would be simply having a named animation without having to set all of this up.第二步当然是简单地拥有一个命名动画,而无需设置所有这些。

Preferably the methods in my Vue.js component would like something like this:我的 Vue.js 组件中的方法最好是这样的:

collapseEnter, collapseEnterCancelled, etc.

But this does not seem to work.但这似乎不起作用。 Do I really have to set up all the directives or is there an easier way?我真的必须设置所有指令还是有更简单的方法?

You need to.你需要。 But it is possible to bind those hook functions automatically by creating an abstract component which will wrap the <transition> .但是可以通过创建一个包装<transition>的抽象组件来自动绑定这些钩子函数。

This is hacky but should work.这很hacky,但应该可以。

First we register a component called autoTransition :首先我们注册一个名为autoTransition的组件:

Vue.component('autoTransition', {
  props: ['name', 'vm'],
  functional: true,
  abstract: true,
  render (h, ctx) {
    return h('transition', {
      name: ctx.props.name,
      on: {
        'before-enter': ctx.props.vm.beforeEnter,
        'enter': ctx.props.vm.enter,
        // ... other hooks
      }
    }, ctx.children)
  }
})

And you use it like a normal transition, but instead of passing all the hooks, you can now simply pass the entire vm (can be referenced by $root ) and the name in case you need:你可以像普通的过渡一样使用它,但不是传递所有的钩子,你现在可以简单地传递整个vm机(可以被$root引用)和name以备不时之需:

<auto-transition name="myTransition" :vm="$root">
  <h1 v-show="someBool">hello</h1>
</auto-transition>

AFAIK the name is used only when you are applying CSS classes to transition. AFAIK 该名称仅在您将 CSS 类应用于过渡时使用。 It has nothing to do with javascript hooks.它与javascript钩子无关。 So yes, you need to define all hooks explicitly.所以是的,您需要明确定义所有挂钩。

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

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