VueJS get data as object

久未见 提交于 2021-02-07 11:16:28

问题


Here is a VueJS component:

<template>
  <a @click="log">click me<a>
</template>

<script>
  export default {
    data() {
      return {
        a: "a",
        b: "something",
        foo: { bar: "baz" },
        // etc.
      }
    },
    methods: {
      log() {
        // console.log( data ); 
        // ???
      }
    }
  }
</script>

I want to access the data from the log function and get it as an object (just like in its declaration). I know I can get data like this :

log() {
  console.log( this.a );
  console.log( this.b );
  console.log( this.foo );
}

But what I want is the whole data as an object (because I want to send the data via an event to a parent component).

Is there a way to get the whole data object inside a method of a component?


回答1:


You can access the current component's data object using this.$data.

Reference: Link

So the log function should be like:

log() {
  console.log(this.$data.a);
  console.log(this.$data.b);
  console.log(this.$data.foo);
}



回答2:


I think what you are looking for is to return the whole data object, like this:

log() {
  console.log(this.$data);
}


来源:https://stackoverflow.com/questions/46091418/vuejs-get-data-as-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!