vue之插槽03
app.vue
<template> <div> <h1>App 根组件</h1> <hr /> <!-- 使用自定义组件 --> <my-test> <template #default="{ msg, info }"> <p>{{ msg }}</p> <p>{{ info.address }}</p> </template> </my-test> <hr /> <my-table> <template #default="{ user }"> <td>{{ user.id }}</td> <td>{{ user.name }}</td> <td> <input type="checkbox" :checked="user.state" /> </td> </template> </my-table> </div> </template> <script> // 导入组件 import MyTest from './MyTest.vue' import MyTable from './MyTable.vue' export default { name: 'MyApp', // 注册组件 components: { MyTest, MyTable, }, } </script> <style lang="less" scoped></style>
MyTest.vue
<template> <div> <h3>这是 TEST 组件</h3> <slot :info="infomation" :msg="message"></slot> </div> </template> <script> export default { name: 'MyTest', data() { return { // 信息数据 infomation: { phone: '138xxxx6666', address: '中国北京', }, message: 'abc' } }, } </script> <style></style>
MyTable.vue
<template> <table class="table table-bordered table-striped table-dark table-hover"> <!-- 表头区域 --> <thead> <tr> <th>Id</th> <th>Name</th> <th>State</th> </tr> </thead> <!-- 表格主体区域 --> <tbody> <!-- 循环渲染表格数据 --> <tr v-for="item in list" :key="item.id"> <slot :user="item"></slot> </tr> </tbody> </table> </template> <script> export default { name: 'MyTable', data() { return { // 列表的数据 list: [ { id: 1, name: '张三', state: true }, { id: 2, name: '李四', state: false }, { id: 3, name: '赵六', state: false }, ], } }, } </script> <style lang="less" scoped></style>
浙公网安备 33010602011771号