Vue2

160 阅读1分钟

ref用法

注意

$refs 只会在组件渲染完成之后生效,并且它们不是响应式的。

所以避免在模板或计算属性中访问 $refsvue2官网

拿到dom元素

this.$refs.testDom

demo:

<template>
  <div id="app">
    🔮<div ref="testDom">11111</div> 
    <button @click="getTest">获取test节点</button>
  </div>
</template>

<script>
export default {
  methods: {
    getTest() {
      🔮console.log(this.$refs.testDom)
    }
  }
};
</script>

结果:不含属性,只含dom

<div>11111</div>

拿到一组dom元素

v-for在遍历的元素上绑定ref属性,循环出来有多个重名,得到的就是数组

可以在method/mounted/created结合nextTick

  <ul>
    <li v-for="item in people" ref="refContent">{{item}}</li>
  </ul>
data() {
    return {
      people:['aa','bb','cc','dd','ee']
    }
  },
created() {
    this.$nextTick( ()=>{console.log(this.$refs.refContent)}
    )
  },

结果:

[li,li,li,li,li]

拿到子组件实例的

子组件:

<template>
  <div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      🔮msg: "hello world"
    }
  },
  methods: {
    🔮open() {
      console.log("调用到了")
    }
  }
}

父组件:

<template>
  <div id="app">
    🔮<HelloWorld ref="hello"/>
    <button @click="getHello">获取helloworld组件中的值</button>
  </div>
</template>

元素

this.$refs.hello.msg

方法

this.$refs.hello.open();

组件

有名字

<template>
  <transition :name="transitionName">
    <div v-show="visible" :style="customStyle" class="back-to-ceiling" @click="backToTop">
      <svg width="16" height="16" viewBox="0 0 17 17" xmlns="http://www.w3.org/2000/svg" class="Icon Icon--backToTopArrow" aria-hidden="true" style="height:16px;width:16px"><path d="M12.036 15.59a1 1 0 0 1-.997.995H5.032a.996.996 0 0 1-.997-.996V8.584H1.03c-1.1 0-1.36-.633-.578-1.416L7.33.29a1.003 1.003 0 0 1 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.004z" /></svg>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'BackToTop',  ///有名字
  props: {
    visibilityHeight: {
      type: Number,
      default: 400
    },
    backPosition: {
      type: Number,
      default: 0
    },
    customStyle: {
      type: Object,
      default: function() {
        return {
          right: '50px',
          bottom: '50px',
          width: '40px',
          height: '40px',
          'border-radius': '4px',
          'line-height': '45px',
          background: '#e7eaf1'
        }
      }
    },
    transitionName: {
      type: String,
      default: 'fade'
    }
  },
  data() {
    return {
      visible: false,
      interval: null,
      isMoving: false
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
    if (this.interval) {
      clearInterval(this.interval)
    }
  },
  methods: {
    handleScroll() {
      this.visible = window.pageYOffset > this.visibilityHeight
    },
    backToTop() {
      if (this.isMoving) return
      const start = window.pageYOffset
      let i = 0
      this.isMoving = true
      this.interval = setInterval(() => {
        const next = Math.floor(this.easeInOutQuad(10 * i, start, -start, 500))
        if (next <= this.backPosition) {
          window.scrollTo(0, this.backPosition)
          clearInterval(this.interval)
          this.isMoving = false
        } else {
          window.scrollTo(0, next)
        }
        i++
      }, 16.7)
    },
    easeInOutQuad(t, b, c, d) {
      if ((t /= d / 2) < 1) return c / 2 * t * t + b
      return -c / 2 * (--t * (t - 2) - 1) + b
    }
  }
}
</script>