账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    组件如何作为变量传给slot
    • 2019-09-18 00:00
    • 10
    22
    0

    写了一个子组件TreeShape.vue,想在外面传递组件变量进来:

    <template>
        <!-- parent传进title和content -->
        <div>
            <div class = "tree-title"
                @click = "onClickTreeTitle">
                {{title}}
            </div>
            <slot v-show = "isExpand">
    
            </slot>
        </div>
    </template>
    
    <script>
    export default {
        props:{
            title: {
                type: String,
                default: ""
            }
        },
        data(){
            return {
                isExpand: false
            }
        },
        methods:{
            onClickTreeTitle(){
                this.isExpand = !this.isExpand;
            }
        }
    }
    </script>

    父组件TreeBox.vue如下,里面包含的子组件 TreeShape.vue的个数是动态的,而且里面传递给slot的组件也是动态的,类似下面这样:

    <template>
        <div>
            <TreeShape
                v-for = "treeShape in treeShapeArray"
                :key = "treeShape.id"
                :title="treeShape.title"
                
            >
            ???
            </TreeShape>
        </div>
    </template>
    
    <script>
    import testC1 from './testC1'
    import testC2 from './testC2'
    import TreeShape from './TreeShape'
    const treeShapeArray = [{
        id:"1",
        title: "aaaa",
        comp: testC1
    },
    {
        id:"2",
        title: "bbb",
        comp: testC2
    }];
    export default {
        data(){
            return {
                treeShapeArray
            }
        },
        components:{
            TreeShape,
            testC1,
            testC2
        }
    }
    </script>
    
    <style scoped>
    
    </style>

    求问该如何实现?

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部