简体   繁体   English

Vue.js:从“父”Vue 获取数据

[英]Vue.js: Get data from "parent" Vue

I have an (unchangeable) DOM structure as followed:我有一个(不可更改的)DOM 结构,如下所示:

<div id="indexVue">
  ...
  <div id="childVue">
  ...
  </div>
  ...
</div>

And two js files:还有两个js文件:

index.js: index.js:

var child = require('childVue');

module.exports = new Vue({
  el: '#indexVue',
  ...
});

childVue.js: childVue.js:

module.exports = new Vue({
  el: '#childVue',
  methods: {
    something: function(){
      // Parent data needed here ...
    },
    ...
  }
});

As shown, I need the data of the indexVue in childVue .如图所示,我需要indexVuechildVue的数据。 Is there any way to pass it to it?有没有办法将它传递给它? I tried to pass it to a function with (v-on="click: childFunction($data)") , but that only (logically) returns the data attribute from the childVue and not from the indexVue .我试图将它传递给一个带有(v-on="click: childFunction($data)")的函数,但这只是(逻辑上)从childVue而不是从indexVue返回数据属性。

Google does not really help, as Vue is not well-documented.谷歌并没有真正提供帮助,因为 Vue 没有很好的文档记录。

The real file and DOM structure are way bigger and more complicated, but necessary for my problem are only these files.真正的文件和 DOM 结构更大更复杂,但我的问题需要的只是这些文件。

Also I am not allowed to use jQuery here, which would make it a task of seconds.此外,我不允许在这里使用 jQuery,这将使其成为几秒钟的任务。

The answer from Pantelis is not true anymore.潘泰利斯的回答不再正确。 Vue.js removed the inherit property. Vue.js 移除了继承属性。

The best way to do that is to pass data through properties;最好的方法是通过属性传递数据;

<div id="indexVue">
  <child-vue :some-data="someData"></child-vue>
</div>

index.js: index.js:

module.exports = new Vue({
  el: '#indexVue',
  data: {
    someData: "parent's data"
  },
  components: {
    childVue: require('childVue')
  }
});

childVue.js: childVue.js:

module.exports = {
  template: '<div>{{someData}}</div>',
  methods: {
    something: function(){
      // you can access the parent's data
      console.log(this.someData)
    }
  },
  props: ['some-data'] // kebab case here available as camelcase in template
};

Please note the props property in childVue.js and the case ( camelCase vs kebab-case ) used for the property name请注意childVue.js中的props属性以及用于属性名称的大小写( camelCase vs kebab-case

You could also access it by this.$parent.someData in case you cannot bind it on a prop :) for example:您也可以通过 this.$parent.someData 访问它,以防您无法将其绑定到道具上:) 例如:

data() {
 return {
  parentData: this.$parent.someData
 }
}

I suggest to use a child component to inherit the scope of its parent.我建议使用child component继承其父组件的范围。

index.html索引.html

<div id="indexVue">
  <child-vue></child-vue>
</div>

index.js: index.js:

module.exports = new Vue({
  el: '#indexVue',
  data: {
    someData: "parent's data"
  },
  components: {
    childVue: require('childVue')
  }
});

childVue.js: childVue.js:

module.exports = {
  inherit: true,
  template: '<div>...</div>',
  methods: {
    something: function(){
      // you can access the parent's data
      console.log(this.someData)
    }
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM