所以我的应用程序是基于 angular 2(angular cli) 和一些 CMS。页面的一些片段是从 CMS 下载的,并显示在 Angular 2 页面上。主要问题是页眉和页脚来自 CMS。 所以我想知道如何添加全局解析器(全局非常重要,我不想为应用程序中的每个路由路径添加解析器),这将强制 Angular 等待 CMS 返回页眉和页脚。我已经通过实现路由接口(interface)成功地使用解析器等待某些路由上的一些数据
export class InboxResolver implements Resolve<MessageListItem[]>
我在一些自定义路由中使用它:
const MessagesRoutes: Routes = [
{
path: 'inbox',
component: InboxComponent,
resolve: { messages: InboxResolver }
}
];
但是如何在一个地方定义一个全局的HeaderAndFooterResolver呢?
最佳答案
不确定您的问题是什么,但您可以使用带有解析器的无组件父路由,并将其与其所有子路由共享
const MessagesRoutes: Routes = [
{ path: '', resolve: { messages: InboxResolver }, children: [
{ path: 'inbox', component: InboxComponent},
{ path: 'xxx', component: XxxComponent},
{ path: 'zzz', component: XxxComponent},
]}
];
关于每个 View 的 Angular 2 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40971327/