父组件 : <template> <!-- 如果当前的属性是一个非prop的attribute, 那么该属性会默认添加到子组件的根元素上 --> <show-info name="why" :age="18" :height="1.88" address="广州市" abc="cba" class="active" /> </template> <script> import ShowInfo from './ShowInfo.vue' export default { components: { ShowInfo } } </script> <style scoped> </style>
子组件 : <template> <div class="infos"> <!-- $attrs : 可以得到传递过来非props的元素 { "address": "广州市", "abc": "cba", "class": "active" } :class="$attrs.class" = class='active' --> <h2 :class="$attrs.class">姓名: {{ name }}</h2> <h2>年龄: {{ age }}</h2> <h2>身高: {{ height }}</h2> <!-- 默认值是 : 我是showMessage --> <h2>Message: {{ showMessage }}</h2> <!-- 等于解构 : <div class="others active" address="广州市" abc="cba"></div> --> <div class="others" v-bind="$attrs"></div> </div> </template> <script> export default { // props对象语法(必须掌握) props: { name: { type: String, default: "我是默认name" }, age: { type: Number, required: true, default: 0 }, height: { type: Number, default: 2 }, showMessage: { type: String, default: "我是showMessage" } } } </script> <style scoped> </style>
标签:到子,name,default,元素,props,type,属性 From: https://www.cnblogs.com/qd-lbxx/p/16612399.html