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

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

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue render写起来太长了,有没有优雅的写法?
    23
    0

    比如 iview tree

    https://www.iviewui.com/compo...

    <template>
        <Tree :data="data5" :render="renderContent"></Tree>
    </template>
    <script>
        export default {
            data () {
                return {
                    data5: [
                        {
                            title: 'parent 1',
                            expand: true,
                            render: (h, { root, node, data }) => {
                                return h('span', {
                                    style: {
                                        display: 'inline-block',
                                        width: '100%'
                                    }
                                }, [
                                    h('span', [
                                        h('Icon', {
                                            props: {
                                                type: 'ios-folder-outline'
                                            },
                                            style: {
                                                marginRight: '8px'
                                            }
                                        }),
                                        h('span', data.title)
                                    ]),
                                    h('span', {
                                        style: {
                                            display: 'inline-block',
                                            float: 'right',
                                            marginRight: '32px'
                                        }
                                    }, [
                                        h('Button', {
                                            props: Object.assign({}, this.buttonProps, {
                                                icon: 'ios-add',
                                                type: 'primary'
                                            }),
                                            style: {
                                                width: '64px'
                                            },
                                            on: {
                                                click: () => { this.append(data) }
                                            }
                                        })
                                    ])
                                ]);
                            },
                            children: [
                                {
                                    title: 'child 1-1',
                                    expand: true,
                                    children: [
                                        {
                                            title: 'leaf 1-1-1',
                                            expand: true
                                        },
                                        {
                                            title: 'leaf 1-1-2',
                                            expand: true
                                        }
                                    ]
                                },
                                {
                                    title: 'child 1-2',
                                    expand: true,
                                    children: [
                                        {
                                            title: 'leaf 1-2-1',
                                            expand: true
                                        },
                                        {
                                            title: 'leaf 1-2-1',
                                            expand: true
                                        }
                                    ]
                                }
                            ]
                        }
                    ],
                    buttonProps: {
                        type: 'default',
                        size: 'small',
                    }
                }
            },
            methods: {
                renderContent (h, { root, node, data }) {
                    return h('span', {
                        style: {
                            display: 'inline-block',
                            width: '100%'
                        }
                    }, [
                        h('span', [
                            h('Icon', {
                                props: {
                                    type: 'ios-paper-outline'
                                },
                                style: {
                                    marginRight: '8px'
                                }
                            }),
                            h('span', data.title)
                        ]),
                        h('span', {
                            style: {
                                display: 'inline-block',
                                float: 'right',
                                marginRight: '32px'
                            }
                        }, [
                            h('Button', {
                                props: Object.assign({}, this.buttonProps, {
                                    icon: 'ios-add'
                                }),
                                style: {
                                    marginRight: '8px'
                                },
                                on: {
                                    click: () => { this.append(data) }
                                }
                            }),
                            h('Button', {
                                props: Object.assign({}, this.buttonProps, {
                                    icon: 'ios-remove'
                                }),
                                on: {
                                    click: () => { this.remove(root, node, data) }
                                }
                            })
                        ])
                    ]);
                },
                append (data) {
                    const children = data.children || [];
                    children.push({
                        title: 'appended node',
                        expand: true
                    });
                    this.$set(data, 'children', children);
                },
                remove (root, node, data) {
                    const parentKey = root.find(el => el === node).parent;
                    const parent = root.find(el => el.nodeKey === parentKey).node;
                    const index = parent.children.indexOf(data);
                    parent.children.splice(index, 1);
                }
            }
        }
    </script>
    

    感觉多级镶嵌的时候,一个属性一行太长了,可读性不好,能不能想html那样

    <div :a="a" @b="b" style="">
        <div>
            <div></div>
        </div>
    </div>
    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 在Vue中,可以使用数组字面量的方式一次性渲染多个组件。这是一种更简洁的写法,但是可能会增加一些性能开销。

      ```javascript

      ```

      这样,每个组件都只有一行代码,读起来会更简洁。但是,需要注意的是,这种方式并不推荐在大型项目中使用,因为会导致代码更难以维护和理解。通常,我们会推荐使用数组字面量的方式一次性渲染多个组件。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部