Vue生命周期挂钩为事件(hook:beforeDestroy)

时间:2019-04-15 06:43:53

标签: vue.js

如何使用vue hooks-as-events语法?

例如this.$once('hook:beforeDestroy')

我进行了搜索,但在官方文档中找不到任何引用。已弃用吗?

1 个答案:

答案 0 :(得分:2)

由于某些原因,多数民众赞成不在官方文档中。

您可以将vue实例生命周期挂钩用作从子级到父级的事件,

类似于:<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ad_scrollview"> <com.facebook.ads.NativeAdLayout android:id="@+id/native_ad_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" /> </ScrollView> </FrameLayout>

我看到了here

这是一个简单的示例:

@hook:destroyed="changeMsg()"
Vue.component('greeting', {
  template: '<h1>im the child component</h1>'
});

var vm = new Vue({
  el: '#app',
  data(){
    return {
      msg:'not destroyed yet...',
      isDead:false
    }
  },
  mounted(){
    setTimeout(this.die,4000)
  },
  methods:{
    changeMsg(){
      this.msg  = 'the child component is destroyed now';
    },
    die(){
      this.isDead  = true;
    }
  }
});

相关问题