前言:
vue框架中的主要传值方式有3种,父组件向子组件传值,子组件向父组件传值,以及兄弟组件间传值,下面分别介绍下这三种传值情况。
【父组件向子组件传值—通过props】
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>父组件向子组件传值</title>
<style>
[v-cloak]{
display: none;
}
</style>
</head>
<body>
<div id="app" v-cloak>
</div>
<script src="../js/vue.js"></script>
<script>
//子组件
let ChildrenComponent={
// props:["post-title","sub-title"],
props:{
"post-title":{
type:String,
default:'123'
},
"sub-title":{
type:String,
default:''
}
},
template:`
<div>{{postTitle}}</div>
`
};
//父组件,// <ChildrenComponent></ChildrenComponent>
let ParentComponent={
data(){
return{
title:'我是父组件的title',
subTitle:'我是父组件的subTitle'
}
},
template:`
<div>
<children-component :post-title="title" :sub-title="subTitle"></children-component>
</div>
`,
components:{
ChildrenComponent
}
}
//常用
new Vue({
el:"#app",
components:{
ParentComponent
},
template:`<parent-component></parent-component>`
});
</script>
</body>
</html>
【子组件向父组件传值—$emit和ref】
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>子组件向父组件传值</title>
<style>
[v-cloak]{
display: none;
}
</style>
</head>
<body>
<div id="app" v-cloak>
</div>
<script src="../js/vue.js"></script>
<script>
//子组件
let ChildrenComponent={
template:`
<button type="button" @click="toValue">子组件向父组件传值</button>
`,
methods:{
toValue(){
this.$emit('handlerClick',"我是子组件值1","我是子组件值2");
}
}
};
//父组件,// <ChildrenComponent></ChildrenComponent>
let ParentComponent={
data(){
return{
title1:"",
title2:""
}
},
template:`
<div>
<children-component @handlerClick="getValue"></children-component>
{{title1}}--{{title2}}
</div>
`,
components:{
ChildrenComponent
},
methods:{
getValue(val1,val2){
this.title1 = val1;
this.title2 = val2;
}
}
}
//常用
new Vue({
el:"#app",
components:{
ParentComponent
},
template:`<parent-component></parent-component>`
});
</script>
</body>
</html>
【兄弟组件传值----通过$ emit和$on】
**前言:**传值的兄弟组件使用$ emit,接受值的兄弟组件使用$ on,都需要先let vue = new Vue();再vue.$ emit(),vue.$on()
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>兄弟组件间传值</title>
<style>
[v-cloak]{
display: none;
}
</style>
</head>
<body>
<div id="app" v-cloak>
</div>
<script src="../js/vue.js"></script>
<script>
let vue = new Vue();
//A组件
let AComponent={
template:`
<button type="button" @click="toValue">A组件给B组件传值</button>
`,
methods:{
toValue(){
vue.$emit('handlerClick',"我是子组件值1","我是子组件值2");
}
}
};
//B组件
let BComponent={
data(){
return{
title1:"",
title2:""
}
},
template:`
<div>
{{title1}}--{{title2}}
</div>
`,
created(){
vue.$on("handlerClick",(val1,val2)=>{
this.title1 = val1;
this.title2 = val2;
})
}
}
//常用
new Vue({
el:"#app",
components:{
AComponent,
BComponent
},
template:`
<div>
<AComponent></AComponent>
<BComponent></BComponent>
</div>
`
});
</script>
</body>
</html>
【父组件调用子组件的方法属性----ref】
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>父组件调用子组件的方法,通过ref</title>
<style>
[v-cloak]{
display: none;
}
</style>
</head>
<body>
<div id="app" v-cloak>
</div>
<script src="../js/vue.js"></script>
<script>
//子组件
let ChildComponent={
props:{
value:String
},
template:`
<div>
<div>子组件</div>
</div>
`,
methods:{
send(){
console.log("父组件调用子组件的方法22222");
}
}
};
//父组件
let ParentComponent={
data(){
return{
}
},
template:`
<div>
<ChildComponent ref="childValues"></ChildComponent>
<button type="button" @click="getValue">父组件调用子组件的方法</button>
</div>
`,
components:{
ChildComponent
},
methods:{
getValue(){
//方式一
// this.$refs.childValues.send();
//方式二
this.$refs['childValues'].send();
}
}
}
//常用
new Vue({
el:"#app",
components:{
ParentComponent
},
template:`
<div>
<ParentComponent></ParentComponent>
</div>
`
});
</script>
</body>
</html>