props
props写在子组件里面,可以直接写,也可以写一个对象,具体写法如下
/* props:["cmovie"], */
props:{
cmovie:Array,
/* cmessage:String, */
cmessage:{
type:String,
default:"hahhaha",
required:true /* 必传的,不传报错 */
}
}
type规定父组件传过来的类型,有:
String
Number
Boolean
Array
Object
Date
Function
Symbol,
default是默认值,就是父组件不传值,子组件默认收到的值,required:true,这个加上,要是不传值,就报错。
子组件
<template>
<div>
<h1>{{sonMessage}}</h1>
</div>
</template>
<script>
export default {
name:"Son",
props:{
getFatherData:{
type:String
}
},
data(){
return{
sonMessage :this.getFatherData
}
}
}
</script>
<style>
</style>
父组件
<template>
<div>
<son :get-father-data="FatherData"></son>
</div>
</template>
<script>
import {Son} from "./son.vue"
export default{
name:"Father",
components:{
Son
},
data(){
return{
FatherData:"这是父组件的信息"
}
}
}
</script>
<style>
</style>
这里说下,子组件props里面的数据,是驼峰写法,在父组件就要写成xx-xx-xx这种了,专业点说就是:“camelCase (驼峰命名法) 的 prop 名需要使用其等价的 kebab-case (短横线分隔命名) 命名”。