VUE 2关于儿童组件的道具未定义

发布于 2025-02-13 18:10:38 字数 576 浏览 0 评论 0原文

因此,我试图将道具传递给儿童组件,如下所示。

<Project :grossMarginPerResource="grossMarginPerResource" :threshold="threshold" :orderBy="orderBy" @refetch="refetch" v-if="group_by == 'Project'"></Project>

道具时,子组件都会接受此道具

props: ['grossMarginPerResource', 'orderBy', 'show_costs','threshold'],

每当我尝试访问此

,要么通过登录控制台,它将其打印为未定义,在父级中记录它,显示以下内容:

{ "green": "1000", "yellow": "400", "red": "400" } 

但是,如果我在子组件中使用:

console.log(this.threshold)

它将打印出未定义的未定义

So I'm trying to pass a prop down to a child component as follows.

<Project :grossMarginPerResource="grossMarginPerResource" :threshold="threshold" :orderBy="orderBy" @refetch="refetch" v-if="group_by == 'Project'"></Project>

The child component accepts this prop

props: ['grossMarginPerResource', 'orderBy', 'show_costs','threshold'],

Whenever I try to access this prop, either by console logging it, it prints out as undefined,

Logging it in the parent component shows the following:

{ "green": "1000", "yellow": "400", "red": "400" } 

but if I do in the child component:

console.log(this.threshold)

it prints out undefined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南巷近海 2025-02-20 18:10:38

它应该有效,要找到根本原因,我刚刚创建了此代码段,并且根据您上面提到的代码正常工作。您能在所有生命周期挂钩中返回threshold数据,请浏览一下您面临哪个位置的问题。

演示

Vue.component('Project', {
  props: ['threshold'],
  template: '<pre>{{ threshold }}</pre>',
  mounted() {
    console.log(this.threshold)
  }
});

var app = new Vue({
  el: '#app',
  data: {
        threshold: { "green": "1000", "yellow": "400", "red": "400" }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <Project :threshold="threshold"></Project>
</div>

It should work, To find the root cause I just created this code snippet and it is working fine as per the code you mentioned above. Can you please have a look and confirm at which place you are facing issue as console.log will return the threshold data in all the life cycle hooks.

Demo :

Vue.component('Project', {
  props: ['threshold'],
  template: '<pre>{{ threshold }}</pre>',
  mounted() {
    console.log(this.threshold)
  }
});

var app = new Vue({
  el: '#app',
  data: {
        threshold: { "green": "1000", "yellow": "400", "red": "400" }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <Project :threshold="threshold"></Project>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文