0

I'm working on the code at Info.vue

data() {
        return {
       option: [],
    }
}

created() {
  console.log(this.option);
}

methods: {
// I get the value from the form on change
  myChange: function (name, age) {
   this.option.push({name: name, age: age})
}

result :

[__ob__: Observer]
0:
name: ("zoka")
age: ("18")
__ob__: Observer {value: {…}, dep: Dep, vmCount: 0}

I can't get the value in this.option, give me an idea. Thanks

json
  • 67
  • 4
  • 15
  • `option` will be wrapped in an observable object in order for the Vue mechanics to work. See [this question](https://stackoverflow.com/questions/52873516/vue-js-returns-ob-observer-data-instead-of-my-array-of-objects). Are you having a problem using the value in your code, or are you just confused by the `console.log` output? – David Weldon Jul 08 '21 at 03:41
  • hmm..I got the value when i change the input, and i want to put that value in an array :option..But the return result i cant get the result in option – json Jul 08 '21 at 03:53
  • related question: https://stackoverflow.com/questions/52873516/vue-js-returns-ob-observer-data-instead-of-my-array-of-objects – fredrivett Jun 20 '22 at 12:51

1 Answers1

1

@json see below for how best to see what you have within your data property option. Where exactly are you trying to see your option property? In the console logs or in your template?

<script>
export default {
  data() {
    return {
      option: [],
    };
  },
  watch: {
    option() {
      // or check in a watcher. This will fire
      // anytime the `option` property is updated
      console.log("option within watcher:", this.option);
    },
  },
  methods: {
    myChange(name, age) {
      this.option.push({ name, age });

      // you can check here if you want
      console.log("option within myChange:", this.option);
    },
  },
  created() {
    // option is the default of an empty array here
    console.log("option within created:", this.option);
  },
};
</script>

You really should invest time into reading the Vue docs though. They've spent a lot of time putting them together and they can easily answer this question for you. The official docs can be found here.
Read the docs and become familiar with Vue and it will become a lot more enjoyable. You'll be flying through it all in no time :)

tony19
  • 125,647
  • 18
  • 229
  • 307
RuNpiXelruN
  • 1,850
  • 2
  • 17
  • 23
  • Thank you for your advice, but i still can't get the data from `[__ob__: Observer]`. – json Jul 08 '21 at 07:16
  • 1
    @json do you mean when you log the output of option in created() it doesn't have the data? Try just rendering the output of option in your template like {{ option }} and see if there are values there. Or check in mounted() – arapl3y Jul 09 '21 at 00:23