简体   繁体   English

为什么这个简单的数据初始化函数在这个 vue 组件中不起作用?

[英]Why doesn't this simple data initialisation function work in this vue component?

I have a vue 3 component.我有一个 vue 3 组件。 The relevant script code below;相关脚本代码如下;

<script>
/* eslint-disable */

export default {
  name: "BarExample",
  data: dataInitialisation,
  methods: {
    updateChart,
  }
};

function dataInitialisation()
{
  return {
      chartOptions: {
        plotOptions: {
          bar: {
            horizontal: true
          }
        },
        xaxis: {
          //categories: [1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999],
          categories: [1991, 1992],
        }
      },
      series: [
        {
          name: "series-1",
          data: [30, 40],
        }
      ]
    };
}
</script>

The above code works fine.上面的代码工作正常。

However, if I were to modify the function dataInitialisation() code into this;但是,如果我将函数dataInitialisation()代码修改为这个;

function dataInitialisation()
{
  init_data = {
      chartOptions: {
        plotOptions: {
          bar: {
            horizontal: true
          }
        },
        xaxis: {
          //categories: [1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999],
          categories: [1991, 1992],
        }
      },
      series: [
        {
          name: "series-1",
          data: [30, 40],
        }
      ]
    };

  return init_data;
}

With the function above, the vue website turned blank and no error message appeared.有了上面的功能,vue网站变成空白,没有出现错误信息。 What is wrong?怎么了? Both functions look pretty much the same to me.这两个功能在我看来几乎相同。

EDIT: I noticed another strange behaviour.编辑:我注意到另一个奇怪的行为。 I added a meaningless line x=2 to the function and this caused the website to go blank too.我在函数中添加了一条无意义的行x=2 ,这也导致网站空白。

function dataInitialisation()
{
  x = 2; //meaningless line caused website to go blank

  return {
      chartOptions: {
        plotOptions: {
          bar: {
            horizontal: true
          }
        },
        xaxis: {
          //categories: [1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999],
          categories: [1991, 1992],
        }
      },
      series: [
        {
          name: "series-1",
          data: [30, 40],
        }
      ]
    };
}

I will answer my own question.我会回答我自己的问题。 I got the answer thanks to @3limin4t0r in the comment section.感谢评论部分中的@3limin4t0r,我得到了答案。

I made a rookie mistake.我犯了一个菜鸟错误。 I forgot the let in front of the variable init_data .我忘了let在变量前面init_data There was no error message in javascript. javascript 中没有错误消息。

function dataInitialisation() {
  let init_data = {
    chartOptions: {
      plotOptions: {
        bar: {
          horizontal: true,
        },
      },
      xaxis: {
        //categories: [1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999],
        categories: [1991, 1992],
      },
    },
    series: [
      {
        name: "series-1",
        data: [30, 40],
      },
    ],
  };

  return init_data;
}

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

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