Home > Net >  vuejs get value from main model to object?
vuejs get value from main model to object?

Time:08-17

I have a basic ask. I am trying to get the data from the main area of my vue model to inside an object. I tried using app.$data.name i also tried this.name, but I cannot get it to work without error.

new Vue({
  el: "#app",
  data: {
  name:"Bobby",
      currentCard: {},
        currentCard: {
        author: this.name,

},
  },
  methods: {

 
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
{{currentCard.author}}
</div>

CodePudding user response:

I think that is not possible, you could use a computed value.

new Vue({
  el: "#app",
  data() {
    return {
      name: "Bobby"
    };
  },
  computed: {
    currentCard() {
       return {
         author: this.name
       };
    }
  },
  methods: {}
 })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
{{currentCard.author}}
</div>

  • Related