Categories
Vue Answers

How to access props in a Vue.js component data function?

Spread the love

Sometimes, we want to access props in a Vue.js component data function.

In this article, we’ll look at how to access props in a Vue.js component data function.

How to access props in a Vue.js component data function?

To access props in a Vue.js component data function, we can get them from this.

For instance, we write

<script>
//...
export default {
  //...
  props: ["messageId"],
  data() {
    const theData = {
      someVar: this.messageId,
    };
    return theData;
  },
  //...
};
</script>

to register the messageId prop.

Then we get the initial value of the messageId prop with this.messageId in the data method.

Conclusion

To access props in a Vue.js component data function, we can get them from this.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *