DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building Enterprise-Grade Real-Time IoT Dashboards with Vue 3, MQTT, and Kafka
  • Momento Migrates Object Cache as a Service to Ampere® Altra®
  • Why I Ditched Redis for Cloudflare Durable Objects in My Rate Limiter
  • Real-Object Detection at the Edge: AWS IoT Greengrass and YOLOv5

Trending

  • Optimizing High-Volume REST APIs Using Redis Caching and Spring Boot (With Load Testing Code)
  • How to Save Money Using Custom LLMs for Specific Tasks
  • AI Agents in Java: Architecting Intelligent Health Data Systems
  • Why Your DLP Policies Fall Short the Moment AI Agents Enter the Picture
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Vue.js Series: Lifecycle Hooks

Vue.js Series: Lifecycle Hooks

A developer offers a discussion of various methods fellow web developers can use throughout the lifecycle of a Vue.js application.

By 
Nirmal Hota user avatar
Nirmal Hota
·
Jun. 15, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
45.9K Views

Join the DZone community and get the full member experience.

Join For Free

lifecycle hooks are the defined methods which get executed in a certain stage of the vue object lifespan. starting from the initialization, to when it gets destroyed, the object follows different phases of life. here is a famous diagram indicating the hook sequence.

(image source: https://vuejs.org/v2/guide/instance.html#lifecycle-diagram )

let's add our code to the hooks and see the phases of how they get fired.

<!doctype html >
<html>
<head>
<div id='div1' v-bind:title="div_title">
{{hello_message}}
</div>
</head>

<body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var v1 = new vue({
el: "#div1",
data: {
hello_message: "hello, there welcome to vuejs world",
div_title: "this is my intro div",
},
beforecreate: function(){ 
alert('before create');
},
created: function(){ 
alert('created');
},
beforemount: function(){ 
alert('before mount');
},
mounted: function(){ 
alert('mounted');
},
beforeupdate: function(){ 
alert('before update');
},
updated: function(){ 
alert('updated');
},
beforedestroy: function(){ 
alert('before destroy');
},
destroyed: function(){ 
alert('destroyed');
}
});

// to fire update
v1.$data.hello_message = "new message";

// this can be invoked to destroy the object, which will fire the destroy hook
//v1.$destroy();
</script>
</body>
</html>

beforecreate (a new object is born)

a vue object is instantiated with the new method. it creates an object of the vue class to deal with the dom elements. this phase of life of the object can be accessed by the beforecreated hook. we can insert our code in this hook to be executed before the object gets initialized.

image title

created (object born with default characteristics)

in this phase of life, the object and its events are fully initialized. created is the hook to access this phase and to write code.

image title

beforemounted (object taking shape to fit in the dom)

this hook is called beforemounted . in this phase, it checks if any template is available in the object to be rendered in the dom. if no template is found, then it considers the outer html of the defined element as a template.

image title

mounted (dom is ready and placed inside the page)

once the template is ready. it fits the data into the template and creates the renderable element. replaces the dom element with this new data filled element. it all happens in the mounted hook.
image title

beforeupdate (changes made and yet getting ready to update the dom)

upon changes made by the external event/user input this hook, i.e. beforeupdate , gets fired before the changes reflecting the original dom element.

to fire the beforeupdated hook, i have added the following code. it changes the hello_message in the runtime by updating the dom.

// to fire update
v1.$data.hello_message = "new message";

updated (changes rendered in the dom)

then the changes get rendered on the screen by actually updating the dom object and fires the updated hook.
image title

beforedestroy (object is ready to die)

just before the vue object gets destroyed and freed up from the memory, the deforedestroy hook gets fired and allows us to handle our custom code in it.

in order to fire this hook, i have added the following code to destroy the vue object.

// this can be invoked to destroy the object, which will fire the destroy hook
v1.$destroy();

destroyed (object died and removed from the memory)

the destroyed hook gets invoked after successfully running destroy on the object.

summary

we can use the lifecycle hooks to add our customized code in different phases of the vue object lifespan. it will help us control the flow during object creation for rendering in the dom, and updating and deleting the object.

hope this helps.

happy coding!

Hook Object (computer science) Vue.js

Published at DZone with permission of Nirmal Hota. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building Enterprise-Grade Real-Time IoT Dashboards with Vue 3, MQTT, and Kafka
  • Momento Migrates Object Cache as a Service to Ampere® Altra®
  • Why I Ditched Redis for Cloudflare Durable Objects in My Rate Limiter
  • Real-Object Detection at the Edge: AWS IoT Greengrass and YOLOv5

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook