美文网首页
vue中 template 和 render用法对比

vue中 template 和 render用法对比

作者: 三省吾身_9862 | 来源:发表于2020-10-28 17:06 被阅读0次
  • App.vue(主入口文件)
<template>
    <ParentCmp />
</template>

<script>
import ParentCmp from './ParentCmp';

export default {
    components: {
        ParentCmp
    },
}
</script>
image.png
  • ParentCmp.vue (template写法)
<template>
    <div>
        <h1>我是parent组件</h1>
        <hr />
        <User style="background: #ccc" text="我是传入的文本">
            <template v-slot:header>
                <p>这是名字为header的slot</p>
            </template>
            <p>这是填充默认slot数据</p>
            <template v-slot:footer>
                <p>这是名字为footer的slot</p>
            </template>
            <template v-slot:item="props">
                <p>名字为item的作用域插槽。显示数据{{props}}</p>
            </template>
            <template v-slot:list="props">
                <p>名字为list的作用域插槽。显示数据{{props}}</p>
            </template>
        </User>
    </div>
</template>

<script>
import User from './User'
export default {
    components: {
        User
    },

    props: {},

    data() {
        return {}
    },

    methods: {}
}
</script>
  • User.vue (template写法)
<template>
    <div>
        <h4>{{text}}</h4>
        <slot name="header"></slot>
        <slot>默认的user slot</slot>
        <slot name="footer"></slot>
        <slot name="item" v-bind="item">item作用域插槽,展示姓名 {{item.name}}</slot>
        <slot name="list" v-bind="{list}">list作用域插槽</slot>
    </div>
</template>

<script>
export default {
    props: {
        text: String
    },
    data() {
        return {
            item: {
                name: '张三',
                age: 28,
                works: '前端、后端、设计、产品'
            },
            list: ['a','b','c']
        }
    }
}
</script>

  • ParentCmp.js (render写法)
import User from './User'

export default {
    props: {},
    data() {
        return {}
    },
    methods: {},
    render(h) {
        return h('div',[
            h('h1', '我是parent组件'),
            h('hr'),
            h(User, {
                props: {
                    text: '我是传入的文本'
                },
                style: {
                    background: '#ccc'
                },
                // 作用域插槽写在scopedSlots里
                scopedSlots: {
                    item: props => h('p', `名字为item的作用域插槽。显示数据${JSON.stringify(props)}`),
                    list: props => h('p', `名字为list的作用域插槽。显示数据${JSON.stringify(props)}`)
                }
            }, 
            // 非作用域插槽写数组里
            [
                h('p', {slot: 'header'}, '这是名字为header的slot'),
                h('p', '这是填充默认slot数据'),
                h('p', {slot: 'footer'}, '这是名字为footer的slot'),
            ])
        ]);
        // jxs写法
        /* return (
            <div>
                <h1>我是parent组件</h1>
                <hr />
                <User 
                    style="background: #ccc" 
                    text="我是传入的文本" 
                    scopedSlots={
                        {
                            item: props => (<p>名字为item的作用域插槽。显示数据{JSON.stringify(props)}</p>),
                            list: props => (<p>名字为list的作用域插槽。显示数据{JSON.stringify(props)}</p>),
                        }
                    }
                >
                    <p slot="header">这是名字为header的slot</p>
                    <p>这是填充默认slot数据</p>
                    <p slot="footer">这是名字为footer的slot</p>
                </User>
            </div>
        ); */
    }
}
  • User.js (render写法)
export default {
    props: {
        text: String
    },
    data () {
        return {
            item: {
                name: '张三',
                age: 28,
                works: '前端、后端、设计、产品'
            },
            list: ['a', 'b', 'c']
        }
    },
    methods: {
        getSlot (name, data) {
            if (this.$scopedSlots[name]) {
                return this.$scopedSlots[name](data);
            } else if (this.$slots[name]) {
                return this.$slots[name];
            }
    
            return undefined;
        },
    },
    render (h) {
        return h('div', [
            h('h4', this.text),
            this.getSlot('header'),
            this.$slots.default,
            this.getSlot('footer'),
            this.getSlot('item', this.item),
            this.getSlot('list', {list: this.list}),
        ])
        // jxs写法
        /* return (
            <div>
                <h4>{this.text}</h4>
                {this.getSlot('header')}
                {this.$slots.default}
                {this.getSlot('footer')}
                {this.getSlot('item', this.item)}
                {this.getSlot('list', {list: this.list})}
            </div>
        ); */
    }
}

相关文章

  • vue中 template 和 render用法对比

    App.vue(主入口文件) ParentCmp.vue (template写法) User.vue (templ...

  • vue 的 render 函数

    render 函数的基本用法 什么是render函数vue通过 template 来创建你的 HTML。但是,在特...

  • Vue中的render渲染函数

    Vue中的render渲染函数 render函数只支持jsx写法创建虚拟Dom节点。vue组件中的template...

  • render函数的一些小知识

    Vue 选项中的 render 函数若存在,则 Vue 构造函数不会从 template 选项或通过 el 选项指...

  • Vue_template和render

    Vue_template和render template一个替换挂载的元素模板。挂载元素的内容都将被忽略,除非模板...

  • Vue render 以及createElement 解析

    Vue中的template 里面使用的模版是HTML语法组件的页面,在Vue中都会被编译成render函数,Vue...

  • ###VUE下

    渲染函数和jsx 在vue中我们可以不用template来指定组件的模板,而是用render函数来创建虚拟dom结...

  • Vue 学习笔记 render函数

    Vue 学习笔记 九 、render函数 9.1 render函数初步了解 template下只允许有一个子节点 ...

  • Vue 之 render 函数

    一、render 函数初步了解 在使用 Vue 组件的过程中,当 template 中的内容较多时,可以在外部用 ...

  • 浅析Vue

    【目录】一、如何搭建一个Vue项目二、Vue两个版本的区别三、template 和 render 怎么用四、tem...

网友评论

      本文标题:vue中 template 和 render用法对比

      本文链接:https://www.haomeiwen.com/subject/dyqcvktx.html