组件 --->父控件 (事件派发 ) 生命周期

来源:互联网 发布:长沙unity3d招聘 编辑:程序博客网 时间:2024/06/05 14:55

vuejs 5.6- 4:00

1.scroll组件

methods:{    if(this.listenScroll){      let me = this;        this.scroll.on('scroll',(pos)=>{          //派发事件出去,外部可监听到scroll事件,得到position          me.$emit('scroll',pos);        })    }}

2.listView父组件

<scroll ref='listview'         :listenScroll='listenScroll'        @scroll="scroll"        ></scroll>
export default{  //获取数据实例已经创建完成之后被调用。在这一步,实例已完成以下的配置:数据观测(data observer),属性和方法的运算, watch/event 事件回调。然而,挂载阶段还没开始,$el 属性目前不可见。  created(){     this.listenScroll = true  }, //数据源  data(){      return {        scrollY:-1,        currentIndex:0      }  },  // el被新创建的vm.$el替换,并挂载到实例上去之后调用该钩子。mounted获取DOM高度。  mounted() {    this.imageHeight = this.$refs.bgImage.clientHeight    this.$refs.list.$el.style.top = `${this.imageHeight}px`  },  //计算属性  computed: {      age: function() {           return this.childrens.age +10;      }   },  //属性  props:{    title:{      type: String,      default:'正在载入'    }    },  //方法   methods:{     scroll(pos){          },     calculateHeight(){     //do code     }  },  //监听  watch:{    data(){      setTimeout( ()=>{        this._calculateHeight()      },20)    },    scrollY(newY){    }  }  //注册组件  components:{     Scroll  }}
原创粉丝点击