简体   繁体   English

如何从vue组件中在vue中更改存储文件中的全局变量

[英]How to change global variable in the store file in vue from a vue component

I'm taking over a project and this project has a global variable defined in one of the store modules. 我正在接管一个项目,这个项目在一个商店模块中定义了一个全局变量。 My goal is to modify the value of this variable by creating an check boxed input area in one of my components. 我的目标是通过在我的一个组件中创建一个复选框输入区域来修改此变量的值。 Thus, when the check box is checked, I'd like the value of the global variable to change. 因此,当选中复选框时,我想要更改全局变量的值。 I'm having no luck doing this so far. 到目前为止,我没有运气。

What I have so far is something like this: 到目前为止我所拥有的是这样的:

store/modules/one.js: 存储/模块/ one.js:

const date = true

export const globalStore = new Vue({
data: {
  globalvar: date
 }

}) })

Then in the vue component 然后在vue组件中

  <div>
    <h2>Hotel Date Check</h2>
    <input type="checkbox" id="checkbox" v-model="dateTest" v-on:change="dateType()">
    <label for="checkbox">{{ dateTest }}</label>
  </div>

methods: { 
  dateType() {
    globalStore.globalvar = false
    console.log(globalStore.globalvar)
  },
}

I can change the value of of the variable easily in the vue component but I need it changed in the Vuex store/modules/one.js and once it's changed there to affect data-making it reactive essentially. 我可以在vue组件中轻松更改变量的值,但是我需要在Vuex store / modules / one.js中更改它,并且一旦在那里更改它就会影响数据 - 使其基本上处于反应状态。 I've been wondering if I have to set up state for this to happen but hoping I'll not have to modify things to go down that route. 我一直想知道我是否必须为此设置状态,但希望我不必修改那条路线。 Any ideas would be great. 任何想法都会很棒。 Thank you for the help. 感谢您的帮助。

 import { One } from 'your route'; 

Then use One.globalvar

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

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