2017-12-19 3 views
0

Ich habe eine dumme Frage, die ich denke, aber ich brauche deine Hilfe. Ich erstelle eine Benachrichtigung Komponenten, die immer noti von Axios Real Time (immer neu laden), aber ich bin verwirrend, es zu machen.Get API in Komponenten der Benachrichtigung durch Echtzeit - NuxtJS

Meine Komponenten Benachrichtigung:

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

Module Benachrichtigung: /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 
} 

Diese Zeile dieses $ store.dispatch ('getNotification') nicht Arbeit? Wie kann ich es am besten machen oder hast du ein Beispielprojekt in Github, zeig es mir. Bitte hilf mir !!!

Antwort

1

Sie verwenden nuxt.js, die serverseitig gerendert wird.

mounted() Lifecycle-Hook wird beim serverseitigen Rendern nicht aufgerufen.

Versand So die Aktion in created() Haken

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

EDIT: Sie können auf $route Eigenschaft ein Beobachter-Setup, die aufgerufen werden, um die Route ändert, wenn Sie wie folgt vor:

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

Das ist richtig . Es klappt. Aber bitte helfen Sie mir das Problem, wie kann ich diese Benachrichtigung Komponenten immer laden Methoden GetNotifications in Real Time? –

+0

@FeedGit was meinst du mit Reload-Methoden? –

+0

Wie Benachrichtigung von Facebook, lädt es bei Änderung URL !! –

Verwandte Themen