forked from island-fox-2017/blog-components-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-app.js
114 lines (107 loc) · 2.39 KB
/
vue-app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
Vue.component('side-bar', {
props:['banyakartikel'],
template: `
<div>
<div v-for="article in banyakartikel">
<router-link :to="'/articles/'+article._id" class="list-group-item">{{ article.title }}</router-link></div>
</div>
</div>
`
})
const mainPage = Vue.component('main-page', {
props:['articles'],
template: `
<div>
<div v-for="a in articles">
<h3 class="list-group"><strong>{{ a.title }}</strong></h3>
<p class="text-justify">{{ a.description }}</p>
<button class="btn btn-default"><router-link :to="'/articles/'+a._id">Read more</router-link></button>
</div>
</div>
`
})
const detailArticle = Vue.component('detail-article', {
props:['id'],
data(){
return {
detailArticle: {}
}
},
template: `
<div>
<h3 class="list-group"><strong>{{ detailArticle.title }}</strong></h3>
<p class="text-justify">{{ detailArticle.description }}</p>
</div>
`,
watch: {
id(){
this.getOne()
}
},
methods: {
getOne(){
var self = this;
axios.get(`http://localhost:3000/articles/${this.id}`)
.then(article => {
self.detailArticle = article.data;
console.log(article.data);
})
}
},
created(){
this.getOne()
}
})
// const Foo = Vue.component('routerlink',
// { template: '<div>foo</div>' }
// )
const Home = Vue.component('home-page',
{ template: `
<div>
<h1 class="list-group"><strong>Welcome</h3>
<p class="text-justify">Ini halaman Home</p>
</div>
`}
)
const routes = [
// {path: '/foo', component: Foo},
{path: '/articles/:id', component: detailArticle, props:true},
{path: '/articles', component: mainPage},
{path: '/home', component: Home}
]
const router = new VueRouter({
routes
})
new Vue({
el: '#app',
router,
data: {
// message: 'hello Vue!',
articles: [],
// sub_article: {}
},
created(){
let self = this;
axios.get('http://localhost:3000/articles')
.then(response => {
response.data.map(teaser => {
teaser.description = teaser.description.split("\n")[0]
})
// console.log(response);
self.articles = response.data
})
.catch(err => {
console.log(err);
})
},
computed: {
top3(){
const top3article = this.articles.filter(function(article, index){
if(index < 3){
return article
}
})
return top3article
}
}
})