props 类型
为什么需要 props 类型呢?就比如我们子组件需要用到父组件的数据,我们到底该使用何种方式传递进去呢?我们都知道在原生 DOM 中有一种 data- 属性,可以将数据绑定,所以类似这种方式,props 就应运而生了。
我们还是接着上节课的例子,在 src/views/TestCom.vue,接收父组件传递进来的属性 title:
<template>
<div class="test-com-wrap">{
{title}}</div>
</template>
<script>
export default {
name: "TestCom",
props: {
title: {
type: String,
default: "",
},
},
};
</script>
然后我们在 src/views/TemplateM.vue 来使用 TestCom 组件,向 TestCom 组件传递 title 属性:
<template>t
<div class="template-m-wrap">
<test-com title="这个是组件"></test-com>
</div>
</template&g