vue渲染功能无法正常工作

时间:2017-05-11 07:32:34

标签: javascript vue.js

我提取了崩溃转换表单element.eleme.io进行测试。 但是,代码无法按预期工作。调用了render函数,但没有使用Transition类提供的方法。 试图谷歌如何正确使用渲染功能,但没有太多关于它的信息。谁知道如何解决iusse?

  

崩解tranisition.js

class Transition {
  beforeEnter (el) {
    el.classList.add('collapse-transition')
    if (!el.dataset) el.dataset = {}

    el.dataset.oldPaddingTop = el.style.paddingTop
    el.dataset.oldPaddingBottom = el.style.paddingBottom

    el.style.height = '0'
    el.style.paddingTop = 0
    el.style.paddingBottom = 0
  }

  enter (el) {
    el.dataset.oldOverflow = el.style.overflow
    if (el.scrollHeight !== 0) {
      el.style.height = el.scrollHeight + 'px'
      el.style.paddingTop = el.dataset.oldPaddingTop
      el.style.paddingBottom = el.dataset.oldPaddingBottom
    } else {
      el.style.height = ''
      el.style.paddingTop = el.dataset.oldPaddingTop
      el.style.paddingBottom = el.dataset.oldPaddingBottom
    }

    el.style.overflow = 'hidden'
  }

  afterEnter (el) {
    el.classList.remove('collapse-transition')
    el.style.height = ''
    el.style.overflow = el.dataset.oldOverflow
  }

  beforeLeave (el) {
    if (!el.dataset) el.dataset = {}
    el.dataset.oldPaddingTop = el.style.paddingTop
    el.dataset.oldPaddingBottom = el.style.paddingBottom
    el.dataset.oldOverflow = el.style.overflow

    el.style.height = el.scrollHeight + 'px'
    el.style.overflow = 'hidden'
  }

  leave (el) {
    if (el.scrollHeight !== 0) {
      el.classList.add('collapse-transition')
      el.style.height = 0
      el.style.paddingTop = 0
      el.style.paddingBottom = 0
    }
  }

  afterLeave (el) {
    el.classList.remove('collapse-transition')
    el.style.height = ''
    el.style.overflow = el.dataset.oldOverflow
    el.style.paddingTop = el.dataset.oldPaddingTop
    el.style.paddingBottom = el.dataset.oldPaddingBottom
  }
}

export default {
  name: 'MyCollapseTransition',
  functional: true,
  render (h, { children }) {
    const data = {
      on: new Transition()
    }

    return h('transition', data, children)
  }
}
  

App.vue

<template>
  <div id="app">
    <collapse-transition>
      <div v-show="show">
        <ul>
          <li>Item 1</li>
          <li>Item 2</li>
          <li>Item 3</li>
          <li>Item 4</li>
          <li>Item 5</li>
        </ul>
      </div>
    </collapse-transition>
    <button @click="show = !show">Show</button>
  </div>
</template>

<script>
import CollapseTransition from './utils/collapse-transition'

export default {
  components: {
    CollapseTransition
  },
  data () {
    return {
      show: true
    }
  }
}
</script>

<style>
.collapse-transition {
  transition: 0.3s height ease-in-out, 0.3s padding-top ease-in-out, 0.3s padding-bottom ease-in-out;
}
</style>

1 个答案:

答案 0 :(得分:0)

on属性需要普通对象,而不是类实例。确切地说,Vue使用for .. in循环遍历该对象的属性,这对类方法不起作用。

为什么你甚至会在这里上课?