Skip to content

Commit 083e04a

Browse files
committed
add data-fetching example
1 parent b0f37f6 commit 083e04a

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

examples/data-fetching/Post.vue

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<template>
2+
<div class="post">
3+
<div v-if="error" style="color: red;">
4+
{{ error }}
5+
</div>
6+
<div v-if="post">
7+
<h2>{{ post.title }}</h2>
8+
<p>{{ post.body }}</p>
9+
</div>
10+
</div>
11+
</template>
12+
13+
<script>
14+
import axios from 'axios'
15+
16+
const API_ROOT = 'http://jsonplaceholder.typicode.com'
17+
18+
export default {
19+
data () {
20+
return {
21+
post: null,
22+
error: null
23+
}
24+
},
25+
created () {
26+
this.fetchData()
27+
},
28+
watch: {
29+
'$route' () { this.fetchData() }
30+
},
31+
methods: {
32+
fetchData () {
33+
axios.get(`${API_ROOT}/posts/${this.$route.params.id}`)
34+
.then(res => { this.post = res.data })
35+
.catch(err => { this.error = err.toString() })
36+
}
37+
}
38+
}
39+
</script>

examples/data-fetching/app.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Vue from 'vue/dist/vue'
2+
import VueRouter from 'vue-router'
3+
import Post from './Post.vue'
4+
5+
Vue.use(VueRouter)
6+
7+
const Home = { template: '<div>home</div>' }
8+
9+
const router = new VueRouter({
10+
mode: 'history',
11+
base: __dirname,
12+
routes: [
13+
{ path: '/', component: Home },
14+
{ path: '/post/:id', component: Post }
15+
]
16+
})
17+
18+
const app = new Vue({
19+
router,
20+
template: `
21+
<div id="app">
22+
<h1>Data Fetching</h1>
23+
<ul>
24+
<li><router-link to="/">/</router-link></li>
25+
<li><router-link to="/post/1">/post/1</router-link></li>
26+
<li><router-link to="/post/2">/post/2</router-link></li>
27+
</ul>
28+
<router-view class="view"></router-view>
29+
</div>
30+
`
31+
}).$mount('#app')

examples/data-fetching/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<a href="/">Examples index</a>
3+
<div id="app"></div>
4+
<script src="/__build__/shared.js"></script>
5+
<script src="/__build__/data-fetching.js"></script>

examples/webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ module.exports = {
2525

2626
module: {
2727
loaders: [
28-
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel' }
28+
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel' },
29+
{ test: /\.vue$/, loader: 'vue' }
2930
]
3031
},
3132

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
"test": "npm run lint && flow check"
2424
},
2525
"devDependencies": {
26+
"axios": "^0.13.1",
2627
"babel-core": "^6.11.4",
2728
"babel-eslint": "^6.1.2",
2829
"babel-loader": "^6.2.4",
2930
"babel-preset-es2015": "^6.9.0",
3031
"babel-preset-flow-vue": "^1.0.0",
3132
"buble": "^0.12.3",
33+
"css-loader": "^0.23.1",
3234
"eslint": "^3.0.1",
3335
"eslint-config-vue": "^1.0.3",
3436
"eslint-plugin-flow-vars": "^0.4.0",
@@ -46,6 +48,7 @@
4648
"rollup-watch": "^2.4.0",
4749
"uglify-js": "^2.7.0",
4850
"vue": "^2.0.0-beta.3",
51+
"vue-loader": "^9.2.0",
4952
"webpack": "^1.13.1",
5053
"webpack-dev-middleware": "^1.6.1"
5154
}

0 commit comments

Comments
 (0)