簡體   English   中英

我們可以從函數中更改全局變量值並根據局部變量切換它的值嗎?

[英]Can we change global variables value from a function and toggle it's value based on local variable?

我聲明了一個具有全局范圍的變量,並希望從我具有相同變量的函數動態地更改它的值,並希望該變量反映基於局部變量的全局變量的變化。 但是我仍然無法改變全局變量值的值。 任何人都可以建議我如何根據局部變量的值反映全局變量的值?

    export default {
      data() {
        return { 
          status: false //global variable
        };
      },
      methods: {
        state(state) { //here i am receiving a value as true or false 
          this.status = state.value; // and i can see that when i check that on  
          return status; //console but still this doesn't reflect global one
       }
      }

您的代碼中有錯誤

    export default {
      data() {
        return { 
          status: false //global variable
        };
      },
      methods: {
        state(state) { 
          this.status = state.value; 
          return this.status; // <-- this.status - that was the mistake
       }
      }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM