【问题标题】:How to get value [__ob__: Observer] in vuejs如何在 vuejs 中获取价值 [__ob__: Observer]
【发布时间】:2021-09-18 12:37:02
【问题描述】:

我正在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})
}

结果:

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

我无法获得 this.option 中的值,请给我一个想法。谢谢

【问题讨论】:

  • option 将被包裹在一个可观察的对象中,以便 Vue 机制工作。见this question。您在使用代码中的值时遇到问题,还是只是被 console.log 输出弄糊涂了?
  • hmm..我在更改输入时得到了值,我想将该值放入数组中:option..但返回结果我无法在 option 中得到结果

标签: vue.js vue-component


【解决方案1】:

@json 请参阅下文,了解如何最好地查看数据属性 option 中的内容。您究竟想在哪里查看您的 option 属性?在控制台日志或您的模板中?

<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>

不过,您确实应该花时间阅读 Vue 文档。他们花了很多时间把它们放在一起,他们可以很容易地为你回答这个问题。 官方文档可以在here找到。
阅读文档并熟悉 Vue,它会变得更加有趣。你很快就会飞过这一切:)

【讨论】:

  • 感谢您的建议,但我仍然无法从[__ob__: Observer] 获取数据。
  • @json 你的意思是当你在 created() 中记录选项的输出时它没有数据?尝试在模板中渲染 option 的输出,例如 {{ option }} 并查看那里是否有值。或者签入mounted()
猜你喜欢
  • 2018-05-10
  • 2019-10-07
  • 2020-04-23
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 2020-10-18
  • 2017-05-28
  • 1970-01-01
相关资源
最近更新 更多