Access data from template to Vue Instance

Viewed 409

I'm very new to vue, so this is properly a basic question. I have a value in my template from an parsed object prop like this:

<h1>{{myval.theme}}</h1>

This gives and display the value in the browser. However, I need to obtain the value and store it in the data section of the instance. How can I store the data in the "getTheValue" data string? This is what I currenct have, and this does not work:

props: {
    myval: Object
  },
data() {
    return {
      getTheValue: this.myval.theme
     };
  },
1 Answers

The best way its to use computed here

computed: {
  getTheValue() {
    return this.myval.theme
  }
}

and call it same way:

this.getTheValue

its best solutions - cached, non-reactive


also if you still want to use data you can assign in lifetime-hooks like:

mounted() {
  this.getTheValue = this.myval.theme
}

But its crude solution, and I highly recommend to use computed variant

Related