props是只读数据。声明了自定义属性props的init转存在data的count内即可
<template>
<div>
<h5>count组件</h5>
<p>count是:{
{count}}</p>
<button @click="count++">增加 </button>
</div>
</template>
<script>
export default {
props : ['init'],
data(){
return {count: this.init}
},
}
</script>
<style lang='less'>
</style>
给init设置一个默认值(在外界没动态绑定:init时用),
type设置默认值的类型(常见类型都可以)
required:true 来确定其他界面是否绑定了init
props : {
init : {
default : 0,
type : Number,
required : true
}
},
在写组件的时候style可能会出现出现样式重叠的问题,解决用scoped,原理就是在每个渲染标签声明了 data-001 类似的属性
<style lang="less" scoped>
父组件使用子组件更改样式在style内填写(在三方组件