【问题标题】:Get API in components of Notification by Real Time - NuxtJS实时获取通知组件中的 API - NuxtJS
【发布时间】:2017-12-19 07:10:16
【问题描述】:

我想我有一个愚蠢的问题,但我需要你的帮助。 我正在创建一个通知组件,它总是通过 Axios Real Time(每次重新加载)得到通知,但我很困惑。

我的通知组件:

<template>
   <ul class="tab-content">
     <notification-item></notification-item>
   </ul>
</template>
<script>
import ItemNotification from '~/components/header/NotificationItem.vue'
export default {
  components: {
    'notification-item': ItemNotification
  },
  created () {
    this.$store.dispatch('getNotification')
  }
}
</script>

模块通知:/store/notification.js:

import api from '~/plugins/axios'

const state = () => {
  return {
    notifications: null
  }
}

const actions = {
  getNotification ({commit}, config) {
    api.get(`/notifications/`, config)
    .then(response => {
      commit('GET_NOTIFICATION', response.data)
    })
  }
}
const getters = {}

const mutations = {
  GET_NOTIFICATION (state, notifications) {
    state.notifications = notifications
  }
}

export default {
  state,
  actions,
  getters,
  mutations
}

这一行 this.$store.dispatch('getNotification') 不起作用?我怎样才能以最好的方式做到这一点,或者你们有 Github 中的示例项目给我看。请帮帮我!!!

【问题讨论】:

    标签: vue.js vuejs2 vuex nuxt.js vuex-modules


    【解决方案1】:

    您使用的是服务器端渲染的 nuxt.js。

    mounted() 生命周期钩子在服务器端渲染期间未被调用。

    所以在created()钩子中调度动作

    created () { 
       this.$store.dispatch('getNotification') 
    }
    

    编辑: 您可以在$route 属性上设置一个观察者,只要路由发生如下变化,就会调用该观察者:

    watch: { 
        '$route' (to, from) { 
            // react to route changes... 
            this.$store.dispatch('getNotification') 
        } 
    }
    

    【讨论】:

    • 没错。有用。但是请帮我解决这个问题,我怎样才能让这个通知组件总是重新加载方法 GetNotifications in Real TIme?
    • @FeedGit 重新加载方法是什么意思?
    • 点赞Facebook的通知,更改网址时会重新加载!!
    猜你喜欢
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    相关资源
    最近更新 更多