- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这是一个关于如何使用 Angular 2 以“正确”方式实现所需功能的概念性问题。
我的应用程序有一个导航菜单、一个工具栏和一个内容区域。后者包含主要的 <router-outlet>
并显示不同的 View ,如列表和详细信息。
我想要实现的是工具栏显示不同的组件,具体取决于内容区域中呈现的组件/ View 。例如,列表组件需要工具栏中的搜索控件,而详细信息组件需要保存按钮。
A) 我的第一次尝试是添加另一个(命名)<router-outlet>
到工具栏,根据静态路由显示工具栏组件。这有什么不对的地方:
B) 我的第二次尝试是在主视图组件的 ngOnInit
中强制导航到工具栏组件(也使用命名的工具栏导出)。 ,它耦合得更紧密。什么味道不好:
ngOnDestroy
上的工具栏 socket ,但我还没有找到方法。C) 给路由器最后一次机会,因为我发现这种方法有效:
const ROUTES: Routes = [
{path: "buildings", children: [
{path: "", component: BuildingListComponent, pathMatch: "full", outlet: "primary"},
{path: "", component: BuildingListToolbarComponent, pathMatch: "full", outlet: "toolbar"},
{path: ":id", component: BuildingDashboardComponent, outlet: "primary"}
]}
];
想法是路由器会选择匹配路径每个导出。但是(不,它本来可以如此简单)不幸的是,这不起作用:
const ROUTES: Routes = [
{path: "buildings", children: [
{path: "list", component: BuildingListComponent, pathMatch: "full", outlet: "primary"},
{path: "list", component: BuildingListToolbarComponent, pathMatch: "full", outlet: "toolbar"},
{path: ":id", component: BuildingDashboardComponent, outlet: "primary"}
]}
];
它似乎(也可能是意外地)只适用于空路径。为什么,为什么?
D) 一个完全不同的策略是重新设计我的组件层次结构,以便每个主视图组件都包含一个适当的工具栏,并使用 multi-slot content projection .还没有尝试过,但我担心工具栏的多个实例会遇到问题。
有时候,这似乎是一个常见的用例,我想知道 Angular 2 专家将如何解决这个问题。有什么想法吗?
最佳答案
按照 Günter Zöchbauer(谢谢!)的建议,我最终向工具栏添加和删除了动态组件。所需的工具栏组件在路由的数据属性中指定,并由包含工具栏的中央组件(导航栏)评估。
请注意,导航栏组件不需要了解工具栏组件(在功能模块中定义)的任何信息。
希望这对某人有帮助。
buildings-routing.module.ts
const ROUTES: Routes = [
{path: "buildings", children: [
{
path: "",
component: BuildingListComponent,
pathMatch: "full",
data: {toolbar: BuildingListToolbarComponent}
},
{
path: ":id",
component: BuildingDashboardComponent,
data: {toolbar: BuildingDashboardToolbarComponent}
}
]}
];
@NgModule({
imports: [
RouterModule.forChild(ROUTES)
],
exports: [
RouterModule
]
})
export class BuildingsRoutingModule {
}
navbar.component.html
<div class="navbar navbar-default navbar-static-top">
<div class="container-fluid">
<form class="navbar-form navbar-right">
<div #toolbarTarget></div>
</form>
</div>
</div>
navbar.component.ts
@Component({
selector: 'navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.scss']
})
export class NavbarComponent implements OnInit, OnDestroy {
@ViewChild("toolbarTarget", {read: ViewContainerRef})
toolbarTarget: ViewContainerRef;
toolbarComponents: ComponentRef<Component>[] = new Array<ComponentRef<Component>>();
routerEventSubscription: ISubscription;
constructor(private router: Router,
private componentFactoryResolver: ComponentFactoryResolver) {
}
ngOnInit(): void {
this.routerEventSubscription = this.router.events.subscribe(
(event: Event) => {
if (event instanceof NavigationEnd) {
this.updateToolbarContent(this.router.routerState.snapshot.root);
}
}
);
}
ngOnDestroy(): void {
this.routerEventSubscription.unsubscribe();
}
private updateToolbarContent(snapshot: ActivatedRouteSnapshot): void {
this.clearToolbar();
let toolbar: any = (snapshot.data as {toolbar: Type<Component>}).toolbar;
if (toolbar instanceof Type) {
let factory: ComponentFactory<Component> = this.componentFactoryResolver.resolveComponentFactory(toolbar);
let componentRef: ComponentRef<Component> = this.toolbarTarget.createComponent(factory);
this.toolbarComponents.push(componentRef);
}
for (let childSnapshot of snapshot.children) {
this.updateToolbarContent(childSnapshot);
}
}
private clearToolbar() {
this.toolbarTarget.clear();
for (let toolbarComponent of this.toolbarComponents) {
toolbarComponent.destroy();
}
}
}
引用资料:
https://vsavkin.com/angular-router-understanding-router-state-7b5b95a12eab
https://engineering-game-dev.com/2016/08/19/angular-2-dynamically-injecting-components
Angular 2 dynamic tabs with user-click chosen components
Changing the page title using the Angular 2 new router
关于 Angular 2 : Display different "toolbar" components depending on main navigation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41036566/
这是工具栏布局的 XML... 我正在使用支持库将工具栏用作我的操作栏。我在 onCreate 中这样做了... Toolbar mToolbar = (Toolbar)getLayoutInf
是否有任何属性/配置表明工具栏在向右溢出时应滚动到最后一项?我有一个垂直工具栏。它应该滚动到右侧的最后一项 我的配置如下: var toolbar = Ext.create('Ext.toolbar.
我使用 toolbar.Template() 和 Toolbar.Excel() 但 toolbar.excel() 不显示,只是 toolbar.Template() 显示。 .Excel(exce
我试图将 justify-content:center 放在工具栏内容的中心。在定位工具栏时,我可以看到扩展的元素,其中之一是 md-toolbar-row 给 justify-content:cen
我有一个toolbar.xml: 我的另一个 Activity 布局有一个包含标签: 我已经在我的扩展 AppCompatActivity 的 Activity 中实现了它: Toolbar
这是主要 Activity ,如 您可以清楚地看到 import android.support.v7.widget.Toolbar; 已经存在。 package app.com.example.an
我正在使用 TabLayout 折叠 ToolbarLayout,除了向上滚动时我的选项卡没有卡在工具栏下方之外,一切正常。 下面是我的代码:
有人可以向我解释一下它们之间的区别以及为什么它们不能互换吗? 导入 android.widget.Toolbar 会导致编译错误,而导入 android.support.v7.widget.Toolb
我收到这个错误 Caused by: java.lang.ClassCastException: android.support.v7.widget.Toolbar cannot be cast t
我正在寻找一种干净的方法来调整 Material-UI AppBar 后面的内容的偏移量。 我认为 theme.mixins.toolbar 会自动适应,但事实并非如此。 (使用这里描述的主题密度 P
我试过很多方法。但没有工作。期待这样的风格 ion-toolbar { contain: none; .toolbar-container { overflow: vi
我想在 md-toolbar 中使用 mf-tabs,我使用 Sithdown 在 https://github.com/angular/material/issues/1076 中提供的解决方案 它
Django 1.10 django-debug-toolbar == 1.5 新项目(我刚刚安装了Django)。尝试安装Django调试工具栏。 文档:https://django-debug-t
我正在升级我之前制作的一款游戏的用户界面,并且正朝着 Google 的 Material Design 方向发展。作为代码升级的一部分,我将向后兼容性支持库 android-support-v7-ap
您好,我是 Xamarin 的新手,我试图在 MainActivity 的 OnCreate() 中找到一个 View ,但找不到它(?)并且返回为 null。 protected override
是否可以将SAS脚本或宏分配给基本SAS中的工具栏按钮?即可以'dm'宏或sas脚本吗? 最佳答案 当然。这是一种方法: 转到“工具”->“自定义”。 选择“自定义”选项卡 通过单击“添加工具”(最左
我创建了一个简单的ALV网格,并用数据填充了网格,现在在选择屏幕之后显示了该网格。我没有使用自定义容器,而是以全屏显示网格。 是否有ALV网格对象的属性,该属性使工具栏具有通常位于网格顶部的按钮过滤器
我想为我的网站制作一个像谷歌工具栏这样的浏览器工具栏。它应该与所有流行的浏览器兼容。 我应该使用哪种语言来制作它?我可以使用任何示例/指南吗? 最佳答案 对于 Firefox,从这里开始:https:
我新下载了一个 jupyter 并尝试使用它。 但是,使用我的 mac 和 windows 计算机, 我在 jupyter 中看不到工具栏。 我尝试搜索以解决此问题,并尝试在 Mac 和 window
我在我的应用程序中使用工具栏。它应该如下所示: 但是,有时候,工具栏背景是错误的!我不知道为什么会这样...... 这是我的布局: ...... 这是我的工具栏: 帮助!有什么想法吗? 设备
我是一名优秀的程序员,十分优秀!