【问题标题】:Vue undefined data before data fetched获取数据之前的Vue未定义数据
【发布时间】:2019-02-10 08:04:31
【问题描述】:

我的一些数据有问题。 当我加载我的仪表板时,它给了我一个错误,它无法读取未定义的数量。 当我重新加载页面时,它可以工作。 所以这是我登录数据不起作用的地方。 但是在重新加载时它应该如何工作。

我在两个组件的图表之前都尝试过使用 v-if,并尝试定义一个对象并将其填充到图表组件上。然后说 v-if。没有任何作用。谁能帮忙?

我有 3 个组件

  1. 登录(成功登录后组件推送到仪表板)
  2. 仪表板(带有导入图表的组件)
  3. 图表(这里是生成的图表)

登录:这里是我登录并将路由推送到仪表板的地方

<template>
  <button @onclick="login()"> Login </button>
</template>
<script>
  heres the login functions that initialize the session i fill with 
  user data.
</script>

dashboard: 我在这里用我想要的数据填充会话。

<template>
  <p v-if="company.thirtydayScore"> {{company.thirtydaysscore}} </p>
  <chart> </chart>
</template>
<script>
 data(){
   company{
     thirtydaysScore: null
     amount: null and so on
   }
 },
 async beforeMount () {
  await this.fetchData()
  await this.$eventHub.$emit('runToday')
 }
 methods: {
   fetchData(){
     heres api call and fetching of data
     this.company = response.data
     this.$session.set('data', this.company)
   }
 }
</script>
  1. 图表

     <template>
      <myChart> </myChart>
     </template>
     <script>
      data(){
       company{
          thirtydaysScore: null
          amount: null and so on
       }
     },
     async beforeMount () {
      var self = this
      await this.$eventHub.on('runToday'){
        self.fillData()
       }
     }
     methods: {
       fillData(){
         here do i fill the data i use in charts from this.$session.get('data')
       }
     }
     </script>
    

错误图片

登录hastebin:https://hastebin.com/imeqaxevim.xml

仪表板hastebin:https://hastebin.com/ixuyiroyuy.xml

图表hastebin:https://hastebin.com/iheqiditiy.xml

【问题讨论】:

    标签: javascript vue.js vue-component vue-router


    【解决方案1】:

    我不确定我的问题是否完全相同,但是我在 vee-validate 中遇到过这个错误很多。似乎启动数据和组件之间不匹配。可能会有更好的解决方案,但我总是用v-if = "company &amp;&amp; company.amount" 非常务实地解决这些错误。

    【讨论】:

    • 这是您使用的实际代码吗?数据不应该返回一个函数吗?像这样:data () { return { company: {amount} }}
    • 我已经链接了hastebin ;)
    • @AndersBootsmannLarsen 是的,在我发布后看到了,哈哈。对我来说看起来不错。没有线索。我认为您需要使用 vue devtools 对此进行调试,以查看出错时数据中的实际内容。乍一看代码似乎很好,所以 devtools 是唯一的方法。
    猜你喜欢
    • 1970-01-01
    • 2018-04-12
    • 2019-02-08
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2019-01-20
    相关资源
    最近更新 更多