8000 fetch server side msg · masteramuk/examples@6cb30bd · GitHub
[go: up one dir, main page]

Skip to content

Commit 6cb30bd

Browse files
committed
fetch server side msg
1 parent 85a5b32 commit 6cb30bd

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed
< 10000 /div>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
exports.install = function () {
2+
F.route('/api/hello_msg', hello_json)
3+
}
4+
5+
function hello_json () {
6+
this.json({msg: 'Hello from API'})
7+
}

nuxt-middleware/nuxt.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
module.exports = {
2+
build:{
3+
vendor: ['axios']
4+
},
25
srcDir: 'nuxt/'
36
}

nuxt-middleware/nuxt/pages/index.vue

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<template>
22
<div>
33
<h3>Hello from Total.js and Nuxt.js</h3>
4-
<div class="react-container">
4+
<div class="container">
5+
<p>Value from server</p>
6+
<p>{{serverMsg}}</p>
7+
</div>
8+
<div class="container">
59
<p>These values are React</p>
610
<input :value="msg" @input="(e)=>{setMsg(e.target.value)}"/>
711
<p>{{msg}}</p>
@@ -12,17 +16,25 @@
1216
</template>
1317
<script>
1418
import {mapState, mapMutations} from 'vuex'
19+
import axios from 'axios'
1520
export default {
1621
computed: {
17-
...mapState(['msg', 'counter'])
22+
...mapState(['msg', 'counter', 'serverMsg'])
1823
},
1924
methods: {
2025
...mapMutations(['setMsg', 'increase', 'decrease'])
26+
},
27+
fetch: function ({store, req}) {
28+
return axios.get(req.uri.protocol + '//' + req.uri.host+ '/api/hello_msg').then(function (res) {
29+
store.commit('setServerMsg', res.data.msg)
30+
}).catch(function (e) {
31+
console.log(e)
32+
})
2133
}
2234
}
2335
</script>
2436
<style scoped>
25-
.react-container {
37+
.container {
2638
padding: 0 20px 20px 20px;
2739
margin: 10px;
2840
border-color: darkgray;

nuxt-middleware/nuxt/store/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const store = function () {
44
return new Vuex.Store({
55
state: {
66
msg: '',
7-
counter: 0
7+
counter: 0,
8+
serverMsg: ''
89
},
910
mutations: {
1011
setMsg: function (state, msg){
@@ -15,6 +16,9 @@ const store = function () {
1516
},
1617
decrease: function (state) {
1718
state.counter -= 1
19+
},
20+
setServerMsg: function (state, msg) {
21+
state.serverMsg = msg
1822
}
1923
}
2024
})

nuxt-middleware/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"author": "ckpiggy",
1515
"license": "MIT",
1616
"dependencies": {
17+
"axios": "^0.16.2",
1718
"nuxt": "^1.0.0-alpha.4",
1819
"total.js": "^2.6.3-3"
1920
}

0 commit comments

Comments
 (0)
0