一 创建测试项目
vue init webpack-simple vuedemo
二 进入demo目录
cd vuedemo
三 安装依赖
cnpm install
四 安装vue-resource, vue-router
cnpm install vue-resource --save
cnpm install vue-router --save
五 修改代码
├── src
│ ├── App.vue
│ ├── components
│ │ ├── Content.vue
│ │ ├── Home.vue
│ │ └── News.vue
│ ├── main.js
App.vue
<template>
<div id="app">
<header class="header">
<router-link to="/home">home</router-link>
<router-link to="/news">news</router-link>
</header>
<hr>
<br>
<router-view></router-view>
</div>
</template>
<script>
import Home from './components/Home.vue';
import News from './components/News.vue';
export default {
name: "app",
data() {
return {
msg:'hello vue world!',
};
},
components:{
'v-home':Home,
'v-news':News
},
};
</script>
<style>
.header{
height: 4.4em;
background: #000;
color:#fff;
line-height: 4.4rem;
text-align: center;
}
.header a{
color:#fff;
padding: 0 2 rem;
}
</style>
Content.vue
<template>
<div id="content">
i am content
<h2>{{list.title}}</h2>
<div v-html="list.content"></div>
</div>
</template>
<script>
export default {
data(){
return {
msg: "content",
list: []
}
},
methods:{
requestData(aid){
console.log(aid)
var api = "http://www.phonegap100.com/appapi.php?a=getPortalArticle&aid=" + aid
this.$http.get(api).then((response)=>{
console.log(response.body.result[0])
this.list = response.body.result[0];
},(err)=>{
console.log(err)
})
}
},
mounted(){
console.log(this.$route.params)
var aid = this.$route.params.aid;
this.requestData(aid);
},
}
</script>
Home.vue
<template>
<!-- all content need in root-->
<div>
Home
<br>
<ul>
<li v-for="(item,k) in list" :key="item.id">
<router-link :to="'/pcontent?id='+k">{{k}} -- {{item}} </router-link>
</li>
</ul>
</div>
</template>
<script>
export default {
data(){
return {
msg:'i am home component!',
list:['goods1', 'goods2','goods3']
}
}
}
</script>
<style>
</style>
News.vue
<template>
<!-- all content need in root-->
<div>
News
<br>
<ul>
<li v-for="(item,k) in list" :key="item.id">
<router-link :to="'/content/'+item.aid">{{k}} -- {{item.title}} </router-link>
</li>
</ul>
</div>
</template>
<script>
export default {
data(){
return {
msg:'i am news component!',
list:[]
}
},
methods:{
requestData(){
var api = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1'
console.log(this.$http)
this.$http.jsonp(api).then(function(response){
console.log(response)
this.list = response.body.result;
},function(err){
console.log(err)
})
}
},
mounted(){
this.requestData();
}
}
</script>
<style>
</style>
main.js
import Vue from 'vue'
import App from './App.vue'
import './assets/css/basic.scss'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import VueResource from 'vue-resource'
Vue.use(VueResource)
import Home from './components/Home.vue'
import News from './components/News.vue'
import Content from './components/Content.vue'
import Pcontent from './components/Pcontent.vue'
const routes = [
{path:'/home', component:Home},
{path:'/news', component:News},
{path:'/content/:aid', component:Content},
{path:'/pcontent', component:Pcontent},
{path:'*', redirect:''}
]
const router = new VueRouter({
routes
})
new Vue({
el: '#app',
router,
render: h => h(App)
})
六 运行
npm run dev
image.png image.png
image.png
七 总结
温习知识点
1 vue-resource的jsonp使用
var api = "http://www.phonegap100.com/appapi.php?a=getPortalArticle&aid=" + aid
this.$http.get(api).then((response)=>{
console.log(response.body.result[0])
},(err)=>{
console.log(err)
})
2 vue-resource的get使用
var api = "http://www.phonegap100.com/appapi.php?a=getPortalArticle&aid=" + aid
this.$http.get(api).then((response)=>{
console.log(response.body.result[0])
},(err)=>{
console.log(err)
})
3 动态路由
<router-link :to="'/pcontent?id='+k">{{k}} -- {{item}} </router-link>
八 参考
https://github.com/pagekit/vue-resource/blob/develop/docs/http.md
https://router.vuejs.org/zh/api/#to
网友评论