1、#axios: vue中的ajax,是用来前后端交互的。
2、
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
<script src="js/axios.js"></script>
</head>
<body>
<div id="app">
<router-link to='/home'>首页</router-link>
<router-link to='/detail'>详情页</router-link>
<router-view></router-view>
</div>
<script>
var Home={
template:`
<h1>这是首页内容</h1>
`
}
var Detail={
template:`
<div>
<h1>这是详情页内容</h1>
<table border=1 cellspacing=0>
<thead>
<tr>
<td>编号</td>
<td>品名</td>
<td>单价</td>
<td>数量</td>
<td>小计</td>
</tr>
</thead>
<tbody>
<tr v-for='value in fruList'>
<td>{{value.num}}</td>
<td>{{value.pname}}</td>
<td>{{value.price}}</td>
<td>{{value.count}}</td>
<td>{{value.sub}}</td>
</tr>
</tbody>
</table>
</div>
`,
data:function(){
return{
fruList:null
}
},
mounted:function(){
var self=this;
axios({
method:'get', //发送数据的方式
url:'json/fruit.json'
}).then(function(resp){ //请求成功
console.log(resp.data)
self.fruList=resp.data
}).catch(function(err){ //请求失败
})
}
}
const routes=[
{path:'/',component:Home},
{path:'/home',component:Home},
{path:'/detail',component:Detail}
]
const router=new VueRouter({
routes:routes
})
new Vue({
el:'#app',
router:router
})
</script>
</body>
3、父传子、子传父(作业)
<body>
<div id="app">
<father></father>
</div>
<script>
Vue.component('father',{
template:`
<div>
<ul>
<li v-for='value in arr'>{{value}}</li>
</ul>
<user @send='rcMsg' userName='Jack'></user>
<user @send='rcMsg' userName='Rose'></user>
</div>
`,
data:function(){
return{
arr:[]
}
},
methods:{
rcMsg:function(txt){
this.arr.push(txt)
}
}
})
Vue.component('user',{
props:['userName'],
template:`
<div>
<label>{{userName}}</label>
<input type='text' v-model='inputVal'>
<button @click='sendMsg'>发送</button>
</div>
`,
data:function(){
return{
inputVal:''
}
},
methods:{
sendMsg:function(){
this.$emit('send',this.userName+':'+this.inputVal)
}
}
})
new Vue({
el:'#app'
})
</script>
</body>