2017-12-19 13 views
0

私は思っていますが、あなたの助けが必要です。 私はを作成しています。Axios Real Time(毎回Reload everytime)によって通知される通知コンポーネントはですが、私はそれを作るのが紛らわしいです。リアルタイムで通知のコンポーネントにAPIを取得する - NuxtJS

マイ通知コンポーネント:

<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 
} 

このラインこの$のstore.dispatch( 'getNotification')しません作業?どのようにして最善の方法でそれをやり遂げることができますか、Githubのサンプルプロジェクトがありますか?私を助けてください !!!

答えて

1

サーバ側でレンダリングされているnuxt.jsを使用しています。

mounted()ライフサイクルフックは、サーバー側のレンダリング中に呼び出されません。

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

そうです:あなたは、セットアップ、次のようにいつでもルート変更と呼ばれます$route財産上のウォッチャーをすることができます :

だからcreated()フック

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

EDITでアクションを派遣します。できます。しかし、私はこの問題を手伝ってください。どうすれば、この通知コンポーネントは常にリアルタイムのGetNotificationsメソッドをリロードできますか? –

+0

@FeedGitリロードメソッドとはどういう意味ですか? –

+0

Facebookの通知同様、URLを変更するとリロードされます! –

関連する問題