Javascript Vue.js和Vue datepicker:在全局状态下保存拾取的时段

Javascript Vue.js和Vue datepicker:在全局状态下保存拾取的时段,javascript,vue.js,datepicker,Javascript,Vue.js,Datepicker,我有好几页有日期选择器。当我选择日期时,我会初始化一个图表,显示这个时期的数据。当我导航到另一个页面时,datepicker输入以其初始状态显示在此组件上。我希望其他页面上的日期选择器记住我以前选择的一个时期。我想我需要把它传递给全球政府,但我不知道如何传递 以下是我的约会选择者: <template> ... <datepicker v-model="periodStart" minimum-view="month"

我有好几页有日期选择器。当我选择日期时,我会初始化一个图表,显示这个时期的数据。当我导航到另一个页面时,datepicker输入以其初始状态显示在此组件上。我希望其他页面上的日期选择器记住我以前选择的一个时期。我想我需要把它传递给全球政府,但我不知道如何传递

以下是我的约会选择者:

<template>
...
<datepicker v-model="periodStart"
                      minimum-view="month"
                      format="MMMM yyyy"/>
          <datepicker v-model="periodEnd"
                      minimum-view="month"
                      format="MMMM yyyy"/>
...
<template>

<script>
...
data() {
return {
  periodStart: new Date(),
  periodEnd: new Date(),
 };
},
methods: {
...mapActions(['loadActiveUsers']),
report() {
  this.loadActiveUsers({ params: {
    start_date: moment(this.periodStart).startOf('month').format('YYYY-MM-DD'),
    end_date: moment(this.periodEnd).endOf('month').format('YYYY-MM-DD'),
    }
   });
  },
},
...
<script>

我不确定您是否遗漏了复制代码的某些部分

但如果要与Vuex存储中的“报告”全局对象交互,则需要添加缺少的功能:

export default {
    state: {
        report: {
            isLoaded: false,
            data: {},
        },
    },
    actions: {
        setData(context, arr) {
            context.commit('saveData', arr) //the commit method calls the mutator method and sends through the pay load sent into the function from the outside world (see below)
        }
    },
    mutations: {
        saveData(state, val) {
            state.report = val //<access the object here and set your data as needed
        }
    },
    getters: {
        getReport(state) {
            return state.report //return the persisting report variable
        }
    }
}
这意味着您可以在模板中使用

{{report}} //points to the computer reported() returned value
要从组件发送要在全局报告对象中设置的新数据,您需要访问:

this.$store.dispatch('setData', data) //Where dispatch->setData is the action being called in the vuex code
另一方面,如果我从加载不同端点时刷新整个页面的角度来阅读您的问题,那么您可以查看sessionStorage

“”大多数主要web浏览器仍然支持它;然后设计一种方法,使用适当的Vue生命周期挂钩(可能已安装?)在应用程序级别启动,以如上所述将持久化数据设置为vuex模型


签出:有关有用信息,我不确定您是否遗漏了复制代码的某些部分

但如果要与Vuex存储中的“报告”全局对象交互,则需要添加缺少的功能:

export default {
    state: {
        report: {
            isLoaded: false,
            data: {},
        },
    },
    actions: {
        setData(context, arr) {
            context.commit('saveData', arr) //the commit method calls the mutator method and sends through the pay load sent into the function from the outside world (see below)
        }
    },
    mutations: {
        saveData(state, val) {
            state.report = val //<access the object here and set your data as needed
        }
    },
    getters: {
        getReport(state) {
            return state.report //return the persisting report variable
        }
    }
}
这意味着您可以在模板中使用

{{report}} //points to the computer reported() returned value
要从组件发送要在全局报告对象中设置的新数据,您需要访问:

this.$store.dispatch('setData', data) //Where dispatch->setData is the action being called in the vuex code
另一方面,如果我从加载不同端点时刷新整个页面的角度来阅读您的问题,那么您可以查看sessionStorage

“”大多数主要web浏览器仍然支持它;然后设计一种方法,使用适当的Vue生命周期挂钩(可能已安装?)在应用程序级别启动,以如上所述将持久化数据设置为vuex模型


签出:有关有用信息

使用cookies如何不,我相信还有另一种解决方案。比如在我的v型模型中跟踪变化并将其传递给全局状态。我只是不知道如何实现这个。使用cookies怎么样?不,我相信还有另一个解决方案。比如在我的v型模型中跟踪变化并将其传递给全局状态。我只是不知道如何实现这一点。很好,这也是Vue中状态(相对于会话)管理背后的理论非常感谢!你给了我一些想法很好,这也是Vue中状态(而不是会话)管理背后的理论非常感谢!你给了我一些想法