一文理解vue的生命周期

VUE PROTOTYPING

这个概念主要是为了能够撇开庞大的项目依赖,独立debug单个组件。

安装vue/cli-service-global

npm install -g @vue/cli-service-global

开始使用prototyping:创建一个组件命名为lifecycle.vue的文件

<template>
  <div>
    <h1>Vue Lifecycle hooks</h1>
    <ul>
      <li v-for="(item, n) in list" :key="n">
       {{ item }} <a @click="deleteItem(item)">Delete</a>
      </li>
    </ul>
    <strong>Add a new item in the list array and save while running localhost to preview the destroy hooks</strong>
  </div>
</template>
<script>
export default {
  data() {
    return {
      list: [
        'Vue Lifecycle Hooks',
        'beforeCreate : Runs when your component has been initialized. data has not been made reactive and events are not set up in your DOM',
        'created : You will be able to access reactive data and events, but the templates and DOM are not mounted or rendered. This hook is generally good to use when requesting asynchronous data from a server since you will more than likely want this information as early as you can before the virtual DOM is mounted',
        'beforeMount : A very uncommon hook as it runs directly before the first render of your component and is not called in Server-Side Rendering',
        'mounted : Mounting hooks are among the most common hooks you will use since they allow you to access your DOM elements so non-Vue libraries can be integrated',
        'beforeUpdate : Runs immediately after a change to your component occurs, and before it has been re-rendered. Useful for acquiring the state of reactive data before it has been rendered',
        'updated : Runs immediately after the beforeUpdate hook and re-renders your component with new data changes',
        'beforeDestroy : Fired directly before destroying your component instance. The component will still be functional until the destroyed hook is called, allowing you to stop event listeners and subscriptions to data to avoid memory leaks',
        'destroyed : All the virtual DOM elements and event listeners have been cleaned up from your Vue instance. This hook allows you to communicate that to anyone or any element that needs to know this was completed'
      ],
    }
  },
  methods: {
    deleteItem(value) {
      this.list = this.list.filter(item => item !== value)
    },
  },
  beforeCreate() {
    alert('beforeCreate: data is static, thats it')
  },
  created() {
    alert('created: data and events ready, but no DOM')
  },
  beforeMount() {
    alert('beforeMount: $el not ready')
  },
  mounted() {
    alert('mounted: DOM ready to use')
  },
  beforeUpdate() {
    alert(
      'beforeUpdate: we know an update is about to happen, and have the data'
    )
  },
  updated() {
    alert('updated: virtual DOM will update after you click OK')
  },
  beforeDestroy() {
    alert('beforeDestroy: about to blow up this component')
  },
  destroyed() {
    alert('destroyed: this component has been destroyed')
  },
}
</script>

然后通过 vue serve lifecycle.vue打开浏览器,观察localhost:8080, 试着删除,修改刷新,增加列表,体会组件的一生。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容