简体   繁体   English

在 function 中修改全局变量的值

[英]modifying value of global variable within a function

I am using vue to try and set the values of checkboxes.我正在使用 vue 来尝试设置复选框的值。 Here is some code below:下面是一些代码:

let variable = false;

export default {
  name: "nyle-home",

  created() {
      this.notifstate = this.$route.params.data[0].state;
      variable = this.notifstate;
      console.log(variable); //outputs true
  },
  data() {
    return {
      weather: {
        state: variable,
        number: "",
      },
      submitted: false
    };
  },
};

I am trying to set state equal to the valuable of variable after created() executes which is true.我正在尝试将 state 设置为等于 created() 执行后变量的值,这是真的。 However, the code is setting state equal to the initial value of variable which is false.但是,代码将 state 设置为等于 false 的变量的初始值。 How can I ensure that state takes on the value of variable after created()?如何确保 state 在 created() 之后采用变量的值?

Set your data directly in the 'created' hook:直接在“created”钩子中设置你的数据:

  created() {
      this.notifstate = this.$route.params.data[0].state;
      variable = this.notifstate;
      console.log(variable); //outputs true
      this.weather.state = this.notifstate;
  },

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

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