-
-
Save ChangJoo-Park/b3caa77a2d0f0b97323eede013a3fb44 to your computer and use it in GitHub Desktop.
Simple Vue & Vuex fetch example using @feathersjs for realtime data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vuex/actions.js | |
import feathers from 'feathers/client' | |
import socketio from 'feathers-socketio/client' | |
import io from 'socket.io-client' | |
const socket = io('http://localhost:3030') | |
const app = feathers().configure(socketio(socket)) | |
// Get the message service that uses a websocket connection | |
const messageService = app.service('messages') | |
export const fetchMessages = function ({commit}) { | |
// Call the messages service on the server via websocket | |
messageService.find({}).then(result => { | |
commit('FETCH_MESSAGES', result.data) | |
}) | |
} | |
export const addMessage = function ({ commit }, text) { | |
messageService.create({ text }).then(result => { | |
commit('ADD_MESSAGE', result) | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// App.js | |
<template> | |
<div id="app"> | |
<Messages></Messages> | |
</div> | |
</template> | |
<script> | |
import Messages from './components/Messages' | |
import store from './vuex/store' | |
export default { | |
components: { Messages }, | |
store // ES6 equivalent of store: store | |
} | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vuex/getters.js | |
export const getMessages = state => state.messages |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// components/Messages.vue<template> | |
<div> | |
<input type="text" placeholder="Enter message" v-model="newMessage"> | |
<button type="submit" @click="tryAddMessage">Add message</button> | |
<ul> | |
<li v-for="message in messages"> | |
<span>{{ message.text }}</span> | |
</li> | |
</ul> | |
</div> | |
</template> | |
<script> | |
import { getMessages } from '../vuex/getters' | |
import { fetchMessages, addMessage } from '../vuex/actions' | |
export default { | |
data () { | |
return { | |
newMessage: '' | |
} | |
}, | |
ready () { | |
this.fetchMessages() | |
}, | |
methods: { | |
tryAddMessage () { | |
var text = this.newMessage | |
if (text.trim()) { | |
this.addMessage(text) | |
} | |
} | |
}, | |
vuex: { | |
getters: { | |
messages: getMessages | |
}, | |
actions: { | |
fetchMessages, | |
addMessage | |
} | |
} | |
} | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vuex/store.js | |
import Vue from 'vue' | |
import Vuex from 'vuex' | |
Vue.use(Vuex) | |
const state = { | |
messages: [] | |
} | |
const mutations = { | |
FETCH_MESSAGES (state, messages) { | |
state.messages = messages | |
}, | |
ADD_MESSAGE (state, message) { | |
state.messages.push(message) | |
} | |
} | |
export default new Vuex.Store({ | |
state, | |
mutations | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment