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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How To Make a Windows Keylogger By Yourself
  • The Complete Guide to Stream API and Collectors in Java 8
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Migrating from React Router v5 to v6: A Comprehensive Guide

Trending

  • Strategies for Securing E-Commerce Applications
  • Data Lake vs. Warehouse vs. Lakehouse vs. Mart: Choosing the Right Architecture for Your Business
  • Designing AI Multi-Agent Systems in Java
  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. The Difference between Created and Mounted in Vue

The Difference between Created and Mounted in Vue

Mounted and Created sound similar - but they do different things. Let's investigate.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
May. 24, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
6.8K Views

Join the DZone community and get the full member experience.

Join For Free

In a previous article, I covered all the different lifecycle hooks in Vue. One of the things that most people get confused about when talking about lifecycle hooks, is the difference between created and mounted. They both have similar names, and they feel like they should do the same thing, but there are some subtle differences.

The Difference between Created and Mounted in Vue

To begin with, both created() and mounted() have access to the data and props on your prototype. For example, both hooks will console log 'My Message' below, as well as the default value for myProp, which is 'Some Prop':

Vue.js Component
 
export default {
    data() {
        return {
            msg: 'My Message'
        }
    },
    props: {
        myProp: {
            type: String,
            default: 'My Prop'
        }
    },
    created() {
        console.log(this.msg);
        console.log(this.myProp);
    },
    mounted() {
        console.log(this.msg);        
        console.log(this.myProp);
    }
}

Prop Inheritance for Created and Mounted

Note: even if you set a property, it will still be available both in created() and mounted()

The fundamental difference between created() and mounted() is that you do not have access to the DOM in created() yet. In our example, if we try to reference this.$el, it will return null in created(), and it will return the DOM element in mounted():

Vue.js Component
 
export default {
    created() {
        // Returns null
        console.log(this.$el);
    },
    mounted() {
        // Returns the DOM element in console:
        console.log(this.$el);     
    }
}

As such, any DOM manipulation, or even getting the DOM structure using methods like querySelector will not be available in created(). As mentioned in the article on lifecycle hooks, created() is great for calling APIs, while mounted() is great for doing anything after the DOM elements have completely loaded.

Composition API and Created or Mounted

The one caveat to this is that if you are using the Composition API, created(), and indeed beforeCreated() no longer exists. You have to instead use setup(). This function takes the place of both created() and beforeCreated(). Therefore, the DOM is still not available in setup(). The Composition API mounted() remains unchanged.

API Hook Prototype Console (video game CLI) Data (computing) Element Inheritance (object-oriented programming) Property (programming)

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Make a Windows Keylogger By Yourself
  • The Complete Guide to Stream API and Collectors in Java 8
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Migrating from React Router v5 to v6: A Comprehensive Guide

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!