【发布时间】:2019-10-07 14:20:12
【问题描述】:
我正在尝试为我的应用程序构建动态布局。我有两种不同的布局,一种是DefaultLayout.vue:
<template>
<div>
<main>
<slot/>
</main>
</div>
</template>
第二个是LayoutWithFooter.vue,有两个插槽:
<template>
<div>
<main>
<slot/>
</main>
<footer>
<slot name="footer"/>
</footer>
</div>
</template>
我处理动态布局的无渲染组件如下所示:
<script>
import Vue from 'vue';
import DefaultLayout from './DefaultLayout';
import LayoutWithFooter from './LayoutWithFooter';
export default {
props: {
name: {
type: String,
required: true
}
},
created(){
this.registerComponent("DefaultLayout", DefaultLayout);
this.registerComponent("LayoutWithFooter", LayoutWithFooter);
this.$parent.$emit('update:layout', this.name);
},
methods: {
registerComponent(name, component) {
if(!Vue.options.components[name]) {
Vue.component(name, component);
}
}
},
render() {
return this.$slots.default[0];
},
}
</script>
所有这些都适用于DefaultLayout.vue,但是当我想使用LayoutWithFooter.vue 时,它无法处理其中的两个插槽。这是一个示例用法:
<template>
<layout name="LayoutWithFooter">
<div>
<div>some content</div>
<div slot="footer">content for the footer slot</div>
</div>
</layout>
</template>
现在的问题是,“页脚槽的内容”没有呈现在 LayoutWithFooter.vue 的页脚槽内。
【问题讨论】:
-
请分享详细的代码库,然后这个问题可能会得到建议。
-
您是否尝试将
<div slot="footer">移动为<layout name="LayoutWithFooter">的直接子代? -
@c16n 我面临同样的问题。您找到解决方案了吗?
标签: vue.js