Vuex Chartist显示数据

时间:2018-02-20 13:21:06

标签: javascript arrays vuejs2 vuex

我的显示数据存在问题

这是我的App.vue文件:

mounted() { 
    this.$store.dispatch('getLocation'),
    this.$store.dispatch('renderChart', this.$el);
},

我的store.js看起来像那样

mutations: {
    setLocation (state, { locations, forecast }) {
      state.location = locations;
      state.forecast = forecast.consolidated_weather.map(item => {
        return Number(Math.round(item['the_temp']))
      });
      state.data.series.push(state.forecast);
      console.log(state.data.series)
    },
    setChart(state, {context, payload}) {
      state.chartist = new Chartist['Line'](context, state.data, state.options, state.responsiveOptions)
      console.log(context,)
    }
  },
  actions: {
    renderChart({commit}, context, payload) {
      commit('setChart', {context, payload})
    },
    getLocation ({ commit }) {
      const url = `${API_URL}location/560743/`    
      axios.get(url)
        .then(response => commit('setLocation', { 
          locations: response.data,
          forecast: response.data
         }))
        .catch(e => console.log(e))
    }
  }

刷新浏览器时,数据无法加载。但是当我将this.$store.dispatch('renderChart', this.$el);移动到created()时,会显示数据,但刷新后不再加载。它可能是生命周期钩子的东西,但我不是很确定。有什么想法吗?

0 个答案:

没有答案