【问题标题】:Load Component with Vue Router使用 Vue 路由器加载组件
【发布时间】:2019-06-26 18:27:39
【问题描述】:

我想像这样一次加载 3 个组件:

<app-header />
<app-main />
<app-footer />

但我也想在此页面中加载Router View

<app-header />
<router-view />
<app-footer />

当我点击router-link 时,&lt;app-main /&gt; 将消失,&lt;router-view /&gt; 将可见。

有没有更好的方法来处理它而不使用 if 或 show?

【问题讨论】:

    标签: vue.js vuejs2 vue-component vue-router


    【解决方案1】:

    您可以像这样通过插槽将路由器视图传递给您的应用程序主组件:

    <app-header />
    <app-main>
      <router-view/>
    </app-main>
    <app-footer />
    

    你还需要像这样在你的 app-main 组件中插入一个 slot 标签:

    <template>
      <!-- your code -->
        <slot></slot>
      <!-- ... -->
    </template>
    

    更多详情请访问https://vuejs.org/v2/guide/components-slots.html

    【讨论】:

    • 谢谢,但我需要将 app-main 替换为 router-view。
    猜你喜欢
    • 2018-10-11
    • 2019-05-25
    • 2019-09-29
    • 1970-01-01
    • 2017-08-29
    • 2021-11-06
    • 2017-05-08
    • 2020-10-05
    • 2019-08-26
    相关资源
    最近更新 更多