1.字符串数组,对象
数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
<div id="app">
<!-- 使用组件称为父组件 -->
<!-- 绑定msg进行动态改变 -->
<mycom1 hello="你好" parent-msg="这是来自父组件的msg"></mycom1>
</div>
<script>
//创建组件称为子组件,父组件将数据传递给子组件
//小驼峰命名,html不能识别大小写,需要将m小写
Vue.component('mycom1',{
props:['parentMsg','hello'],
template:`<div>
<p>{{hello}}</p>
<p>{{parentMsg}}</p>
<p>{{childmsg}}</p>
</div>`,
data(){
return{
childmsg:'这是子组件'
}
}
})
</script>
<script>
var vm=new Vue({
el:'#app',
} );
</script>
</body>
</html>
数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
<div id="app">
<!-- 使用组件称为父组件 -->
<mycom1 :parent-msg="msg"></mycom1>
<p>父组件的msg值:<input type="text" v-model="msg"></p>
</div>
<script>
var vm=new Vue({
el:'#app',
data:{
msg:''
},
method:{},
components:{
mycom1:{
template:`<div>
<p>{{parentMsg}}</p>
</div>`,
props:['parentMsg'],
}
}
} );
</script>
</body>
</html>
对象
props不可更改
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
<div id="app">
<!-- 使用组件称为父组件 -->
<mycom1 :parent-msg="msg"></mycom1>
</div>
<script>
var vm=new Vue({
el:'#app',
data:{
msg:'父组件的msg'
},
components:{
mycom1:{
template:`<div>{{parentMsg}}---{{sonfromparent}}</div>`,
props:['parentMsg'],
data(){
return{
sonfromparent:this.parentMsg
}
}
}
}
} );
</script>
</body>
</html>
style绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
<div id="app">
<!-- 使用组件称为父组件 -->
<mycom2 :width="msg"></mycom2>
</div>
<script>
Vue.component('mycom2',{
template:`<div :style='childwidth' style='border:1px solid black'>
我是子组件mycom2
</div>`,
props:['width'],
computed:{
childwidth:function(){
return{
width:this.width+'px'//width:200px
}
}
}
})
var vm=new Vue({
el:'#app',
data:{
msg:'200'
},
} );
</script>
</body>
</html>


18万+

被折叠的 条评论
为什么被折叠?



