Vue.js Vues-如何在渲染函数中使用v-for和作用域插槽
我有一个组件,呈现导航链接。它基于Vue.js Vues-如何在渲染函数中使用v-for和作用域插槽,vue.js,vue-component,vue-router,vuejs3,Vue.js,Vue Component,Vue Router,Vuejs3,我有一个组件,呈现导航链接。它基于scopedSlots特性。该组件运行良好,但我想对其进行改进。这是它当前的使用方式: <horizontal-navigation :items="items" #default="{ item }"> <navigation-item :item="item"></navigation-item> </horizontal-navigation>
scopedSlots
特性。该组件运行良好,但我想对其进行改进。这是它当前的使用方式:
<horizontal-navigation :items="items" #default="{ item }">
<navigation-item :item="item"></navigation-item>
</horizontal-navigation>
下面是水平导航组件模板:
<template>
<div class="horizontal-navigation">
<slot v-for="(item, index) in items" :key="index" :item="item"></slot>
</div>
</template> -->
我在文档中找到的最接近的东西是:
render() {
if (this.items.length) {
return Vue.h('ul', this.items.map((item) => {
return Vue.h('li', item.name)
}))
} else {
return Vue.h('p', 'No items found.')
}
}
有什么建议吗?在模板中尝试使用条件呈现来呈现
插槽或回退内容:
<div class="horizontal-navigation">
<template v-for="(item, index) in items" >
<template v-if="$slots.default">
<slot :item="item"></slot>
</template>
<template v-else>
<router-link class="navigation-item" :path="item.path">{{ item.title }}</router-link>
</template>
</template>
</div>
{{item.title}
在模板中,尝试使用条件呈现来呈现插槽或回退内容:
<div class="horizontal-navigation">
<template v-for="(item, index) in items" >
<template v-if="$slots.default">
<slot :item="item"></slot>
</template>
<template v-else>
<router-link class="navigation-item" :path="item.path">{{ item.title }}</router-link>
</template>
</template>
</div>
{{item.title}
您使用的是vue 3吗?@BoussadjraBrahim,是的,siri可以建议使用模板语法的解决方案,可以吗?@BoussadjraBrahim,请使用,这将非常棒!您正在使用vue 3吗?@BoussadjraBrahim,是的,siri可以建议使用模板语法的解决方案,可以吗?@BoussadjraBrahim,请这样做,这将非常棒!
render() {
if (this.items.length) {
return Vue.h('ul', this.items.map((item) => {
return Vue.h('li', item.name)
}))
} else {
return Vue.h('p', 'No items found.')
}
}
<div class="horizontal-navigation">
<template v-for="(item, index) in items" >
<template v-if="$slots.default">
<slot :item="item"></slot>
</template>
<template v-else>
<router-link class="navigation-item" :path="item.path">{{ item.title }}</router-link>
</template>
</template>
</div>