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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Import Statement With an Emscripten-Generated WebAssembly Module in Vue.js
  • TestCafe Integration With Cucumber
  • Cutting-Edge Object Detection for Autonomous Vehicles: Advanced Transformers and Multi-Sensor Fusion
  • Writing DTOs With Java8, Lombok, and Java14+

Trending

  • Evolution of Cloud Services for MCP/A2A Protocols in AI Agents
  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • AI, ML, and Data Science: Shaping the Future of Automation
  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.6K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Import Statement With an Emscripten-Generated WebAssembly Module in Vue.js
  • TestCafe Integration With Cucumber
  • Cutting-Edge Object Detection for Autonomous Vehicles: Advanced Transformers and Multi-Sensor Fusion
  • Writing DTOs With Java8, Lombok, and Java14+

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!