本篇内容介绍了“vue框架render方法怎么替换template”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
import Vue from 'vue'
const Item = Vue.component('Item', {
template: `<div>
<h3>子组件</h3>
<slot></slot>
</div>`
})
const app = new Vue({
el: '#app',
template: `<div ref="myDiv"> <item ref="item">
<p ref="p">this is a slot</p>
</item>
</div>`,
data: {
count: 0 },
components: {
Item
}
})
const app = new Vue({
el: '#app',
data: {
count: 0
},
render (createElement) {
return createElement(
'div', {
ref: 'myDiv',
// domProps: {
// innerHTML: '<span>注意:添加该属性会把后面的子节点覆盖</span>'
// },
attrs: {
id: 'test-id',
title: 'test-title'
}
},
[
createElement('item', {
ref: 'item'
},
[
createElement('p', {
ref: 'p'
}, 'this is a slot')
])
])
},
components: {
Item
}
})
1.如上面更改后的代码,render方法内传入createElement函数,接下来使用createElement函数来创建节点。
2.函数方法格式 createElement('节点或组件名称', {节点属性}, [子节点])
3.先创建一个div元素, 内部包含ref='myDiv'的属性, 使用数组存放其子节点
4.数组内子节点是 item组件, 属性是 ref="item", 其子节点为p, 依次类推逐层创建子节点, 最后的文本节点使用字符串或变量即可,无需再用[]包含。
App.vue(主入口文件)
<template>
<ParentCmp />
</template>
<script>
import ParentCmp from './ParentCmp';
export default {
components: {
ParentCmp
},
}
</script>
ParentCmp.vue (template写法)
<template>
<div>
<h2>我是parent组件</h2>
<hr />
<User 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>
<h5>{{text}}</h5>
<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('h2', '我是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>
<h2>我是parent组件</h2>
<hr />
<User
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('h5', 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>
<h5>{this.text}</h5>
{this.getSlot('header')}
{this.$slots.default}
{this.getSlot('footer')}
{this.getSlot('item', this.item)}
{this.getSlot('list', {list: this.list})}
</div>
); */
}
}
“vue框架render方法怎么替换template”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。