简体   繁体   English

Javascript:加载页面时出错

[英]Javascript: Error when loading the page

I'm making an API call, and every time the page loads it comes with this error and I don't know why.我正在进行 API 调用,每次页面加载时都会出现此错误,我不知道为什么。 Can anyone tell me why?谁能告诉我为什么? I've tried to put it:我试着说:

什么浏览器说

Here is the markup where I use the total amount:这是我使用总金额的标记:

<div class="summary__graph">
        <div class="graph__header">
          <h3 class="headline">Fordeling</h3>
          <p class="answers">{{company.amount.total}} besvarelser</p>
        </div>

Here's where I define my company I fill with total value这是我定义我的公司的地方,我填写了总价值

 data () {
    return {
      selected: 1,
      datacollection: null,
      datacollection2: null,
      datacollection3: null,
      company: '',
      isActive: false,
      scoreLine: null,
      timeBar: null,
      answerPercent: null

    }
  },

vue.js mounted and methods vue.js 挂载和方法

  mounted () {
    this.getStoreScore()
    this.fillData()
    this.fillData2()
    this.fillData3()
    this.fillDataScoreLine()
    this.fillDataTimeBar()
    this.fillDataAnswerPercent()
  },


getStoreScore () {
      return axios.post('API', {
        storeId: '5b7515ed5d53fa0020557447'
      }).then(response => {
        this.company = {
          'storeScore': response.data.result,
          'amount': {
            'total': response.data.amount.total,
            'zero': {
              'amount': response.data.amount.zero,
              'percentage': response.data.amount.zero / response.data.amount.total * 100
            },
            'one': {
              'amount': response.data.amount.one,
              'percentage': response.data.amount.one / response.data.amount.total * 100
            },
            'two': {
              'amount': response.data.amount.two,
              'percentage': response.data.amount.two / response.data.amount.total * 100
            },
            'three': {
              'amount': response.data.amount.three,
              'percentage': response.data.amount.three / response.data.amount.total * 100
            }
          }
        }
        return this.company
      })
    },

Can anyone tell me why it gives me that error?谁能告诉我为什么它给了我这个错误? :D :D

Because you're making an API call, the page gets rendered before you receive any data.因为您正在进行 API 调用,所以页面会在您收到任何数据之前呈现。 You get the error because company.amount is undefined when the page gets rendered.您收到错误是因为在呈现页面时company.amount未定义。 There are a few fixes possible: either you delay your page rendering until all the data is received, or you write a quick if condition around the有一些可能的修复:要么延迟页面渲染,直到收到所有数据,要么编写一个快速的 if 条件

<p class="answers">{{company.amount.total}} besvarelser</p>

More info: https://v2.vuejs.org/v2/guide/conditional.html更多信息: https ://v2.vuejs.org/v2/guide/conditional.html

Edit: Your code would become编辑:您的代码将变为

<p v-if="company.amount" class="answers">{{company.amount.total}} besvarelser</p>

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

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