父组件向子组件传递数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<child v-bind:msg = "parentMsg"></child>
</div>
<script>
//定义一个组件
Vue.component("child",{
props:['msg'],
template:'<h1>{{msg}}</h1>'
});
var vm = new Vue({
el:"#app",
data:{
parentMsg:'父组件内容'
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
请输入:<input v-model="parentMsg"></input>
<child v-bind:msg = "parentMsg"></child>
</div>
<script>
//定义一个组件
Vue.component("child",{
props:['msg'],
template:'<h1>{{msg}}</h1>'
});
var vm = new Vue({
el:"#app",
data:{
parentMsg:''
}
})
</script>
</body>
</html>
第一步:子组件有一个数据counter,有一个按钮,当点击时实现数据加一
第二步:父组件有一个数据total,当子组件点击按钮时会发出一个信号,相应的父组件会执行incrementTotal这个函数,这个函数功能是让total加一
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<p>{{total}}</p>
<child v-on:increment="incrementTotal"></child>
<child v-on:increment="incrementTotal"></child>
</div>
<script>
//定义一个组件
Vue.component("child",{
template:'<button v-on:click="incrementHandle">{{counter}}</button>',
data:function () {
return {
counter:0
}
},
methods:{
incrementHandle:function () {
this.counter++;
this.$emit('increment') //注意:要加单引号
}
}
});
var vm = new Vue({
el:"#app",
data:{
total:0
},
methods:{
incrementTotal:function () {
this.total++;
}
}
})
</script>
</body>
</html>
Axios
{
"name":"狂神说java",
"url": "http://baidu.com",
"page": 1,
"isNonProfit":true,
"address": {
"street": "含光门",
"city":"陕西西安",
"country": "中国"
},
"links": [
{
"name": "B站",
"url": "https://www.bilibili.com/"
},
{
"name": 4399,
"url": "https://www.4399.com/"
},
{
"name": "百度",
"url": "https://www.baidu.com/"
}
]
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
</head>
<body>
<div id="app">
</div>
<script>
var vm = new Vue({
el:'#app',
mounted(){ //钩子函数
//获取数据,参数url
axios.get('../data.json').then(response=>(console.log(response.data))) //然后
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
</head>
<body>
<div id="app">
<h1>{{info.name}}</h1>
</div>
<script>
var vm = new Vue({
el:'#app',
data(){
return{
//请求的返回参数格式必须和json字符串格式保持一致
info:{
name:null,
url:null,
address:{
street:null,
city:null,
country:null
}
}
}
},
mounted(){ //钩子函数
//获取数据,参数url
axios.get('../data.json').then(response=>(this.info=response.data)) //然后
}
})
</script>
</body>
</html>
Vuex
https://www.jianshu.com/p/2e5973fe1223
npm install vuex --save
初始化store下index.js中的内容
import Vue from 'vue'
import Vuex from 'vuex'
// 挂载Vuex
Vue.use(Vuex)
// 创建Vuex对象
const store = new Vuex.Store({
state: {
songId: ''
}
})
export default store
将store挂载到当前项目的Vue实例当中去
import Vue from 'vue'
import Vuex from 'vuex'
// 挂载Vuex
Vue.use(Vuex)
// 创建Vuex对象
const store = new Vuex.Store({
state: {
songId: '',
commentType: ''
},
mutations: {
setSongIdAndCommentType (state, song) {
this.state.songId = song.songId
this.state.commentType = song.commentType
}
}
})
export default store
在组件中使用
this.$store.commit('setSongIdAndCommentType', {songId: songId, commentType: commentType})