VueJS 获取数据作为对象 [英] VueJS get data as object

查看:32
本文介绍了VueJS 获取数据作为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 VueJS 组件:

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>

我想从 log 函数访问 data 并将其作为对象获取(就像在其声明中一样).我知道我可以得到这样的数据:

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?

推荐答案

您可以使用 this.$data 访问当前组件的数据对象.

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

参考:链接

所以日志函数应该是这样的:

So the log function should be like:

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

这篇关于VueJS 获取数据作为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆