【发布时间】:2018-10-10 14:48:55
【问题描述】:
TL;DR:是否可以将内容从子组件传递到父组件中引用的组件的特定插槽?
我正在使用Vue Webpack Template 附带的Vue Router,它提供了开箱即用的单页应用程序的基础。
我面临的问题是我有一个主布局,简化如下:
<template>
<div id="app">
<Nav/>
<APageWithDifferentNav/>
</div>
</template>
<script>
import Home from "./components/views/Home";
import APageWithDifferentNav from "./components/views/APageWithDifferentNav";
import Nav from "./components/Nav";
export default {
name: "App",
components: {
Nav, Home, APageWithDifferentNav
}
};
</script>
导航组件是这样的:
<template>
<div class="nav">
<ul class="nav__list">
<li class="nav__list-item">always show this nav item</li>
</ul>
<slot>
<ul class="nav__list replace-this">
<li class="nav__list-item">only show this</li>
<li class="nav__list-item">if no slot</li>
<li class="nav__list-item">content provided</li>
</ul>
</slot>
</div>
</template>
<script>
export default {
name: "Nav"
};
</script>
我希望能够从视图组件内更改Nav 组件内的插槽。
例如,在结帐或入职流程中,您希望限制向用户显示的导航选项,并换入其他内容以突出显示他们在旅途中的位置。
我有没有办法做到这一点,而不必在每个页面的模板中移动 <Nav/>?
I've put together a more indepth code example to show what I'm after which you can view here.
非常感谢任何帮助。
【问题讨论】:
标签: vue.js vuejs2 vue-component vue-router