Skip to content

Commit 5a1145c

Browse files
committed
add a page : ajax best practice to me
1 parent 332929c commit 5a1145c

File tree

7 files changed

+87
-5
lines changed

7 files changed

+87
-5
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs"
1414
},
1515
"dependencies": {
16+
"axios": "^0.15.3",
1617
"element-ui": "^1.1.2",
18+
"lodash": "^4.17.4",
1719
"normalize.css": "^5.0.0",
1820
"vue": "^2.1.0",
1921
"vue-router": "^2.1.1"

src/components/common/Container.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22
<div id="container">
33
<TopBar></TopBar>
44
<transition>
5-
<router-view></router-view>
5+
6+
<el-row type="flex" class="row-bg" justify="center">
7+
<el-col :span="20"><router-view></router-view></el-col>
8+
<!--<el-col :span="6"><router-view></router-view></div></el-col>-->
9+
</el-row>
10+
11+
<!--<el-row>
12+
<el-col :span="23"><router-view></router-view></div></el-col>
13+
</el-row>-->
614
</transition>
715
</div>
816
</template>

src/components/common/NavMenu.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
<div class="logo">
44
<router-link to="/">
55
<!--<img src="../../assets/images/logo.svg" />-->
6+
<p>星辰</p>
67
</router-link>
78
</div>
89
<el-submenu index="1">
910
<template slot="title"><i class="el-icon-menu"></i>导航一</template>
10-
<el-menu-item index="1-1"><router-link to="/page1" class="nav-link">二级导航一</router-link></el-menu-item>
11+
<el-menu-item index="1-1"><router-link to="/question" class="nav-link">Ajax Demo</router-link></el-menu-item>
1112
<el-menu-item index="1-2"><router-link to="/page2" class="nav-link">二级导航二</router-link></el-menu-item>
1213
</el-submenu>
1314
<el-menu-item index="2"><router-link to="/page3" class="nav-link"><i class="el-icon-message"></i>导航二</router-link></el-menu-item>

src/components/common/style.scss

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,11 @@
148148
height: 60px;
149149
text-align: center;
150150
background-color: #20202A;
151-
img {
152-
margin-top: 12px;
151+
p {
152+
margin: 0;
153+
padding-top: 20px;
154+
font-size: 20px;
155+
color: white;
153156
}
154157
}
155158
.nav-link {

src/pages/home/home.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template lang="html">
22
<div class="home">
3-
<h1>这是首页</h1>
3+
<!--<h1>这是首页</h1>-->
44
<!--<img src="../assets/images/home.jpg" width="100%" />-->
55
<p>首页</p>
66
</div>

src/pages/question/index.vue

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<template>
2+
<div>
3+
<el-form ref="form" :model="yesno" label-width="80px">
4+
<el-form-item label="问题:">
5+
<el-input v-model="yesno.question"></el-input>
6+
<p v-bind:style="{ color: yesno.answerColor }">{{ yesno.answer }}</p>
7+
<img :src="yesno.img">
8+
</el-form-item>
9+
</el-form>
10+
</div>
11+
</template>
12+
13+
<script>
14+
import _ from 'lodash'
15+
import axios from 'axios'
16+
17+
export default {
18+
props: ['msg'],
19+
data: function () {
20+
return {
21+
yesno: {
22+
question: '',
23+
answer: '提个对错问题?',
24+
answerColor: null,
25+
img: ''
26+
}
27+
}
28+
},
29+
watch: {
30+
'yesno.question': function (newQuestion) {
31+
this.yesno.answer = '正在等你停止输入'
32+
this.yesno.img = ''
33+
this.getAnswer()
34+
}
35+
},
36+
methods: {
37+
getAnswer: _.debounce(
38+
function () {
39+
let vm = this.yesno
40+
if (vm.question.indexOf('?') === -1) {
41+
vm.answer = '需要一个英文问号结尾. ;-)'
42+
return
43+
}
44+
vm.answer = '正在想...'
45+
axios.get('https://yesno.wtf/api/')
46+
.then(function (response) {
47+
vm.answer = _.capitalize(response.data.answer)
48+
vm.img = _.capitalize(response.data.image)
49+
vm.answerColor = 'green'
50+
})
51+
.catch(function (error) {
52+
vm.answer = 'Error! Could not reach the API. ' + error
53+
vm.answerColor = 'red'
54+
})
55+
},
56+
// 这是我们为用户停止输入等待的毫秒数
57+
500
58+
)
59+
}
60+
}
61+
</script>
62+
<style>
63+
64+
</style>

src/routes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ export default [
22
{
33
path: '/',
44
component: resolve => require(['./pages/home/home.vue'], resolve)
5+
},
6+
{
7+
path: '/question',
8+
component: resolve => require(['./pages/question'], resolve)
59
}
610
]

0 commit comments

Comments
 (0)