angular - 在 viewInit 上使用 *ngFor 和 view-child 动态地将 mat-menu 渲染为导航标题
问题描述
navbar
我的目的是从Router.config
角度材料中动态创建一个。我创建了一个函数来设置标题元素(导航菜单)。模板用于*ngFor
生成路由列表,而子路由写为 subMenu as mat-menu
。我无法决定何时调用该函数。当我调用函数时ngOnInit()
我得到ViewChild
未定义,当我调用它时ngAfterViewInit()
,我得到以下两个错误:-
错误:-
matMenuTriggeredFor:must pass a matMenu instance
HeaderComponent.html:8 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngForOf: undefined'. Current value: 'ngForOf: [object Object],[object Object],[object Object]'
我已经viewChild
向 the 提供了 ref,[matMenuTriggeredFor]
但它似乎没有被分配。
export class HeaderComponent implements OnInit, AfterViewInit, AfterContentInit {
public headerElements = [];
public childElements = {};
@ViewChild("ABOUTUS") ABOUTUS: ElementRef;
@ViewChild("TECHNOLOGY") TECHNOLOGY: ElementRef;
@ViewChild("CAREERS") CAREERS: ElementRef;
@ViewChild("BUSINESSPROFILE") BUSINESSPROFILE: ElementRef;
viewObjects = {
ABOUTUS: this.ABOUTUS,
TECHNOLOGY: this.TECHNOLOGY,
CAREERS: this.CAREERS,
BUSINESSPROFILE: this.BUSINESSPROFILE
};
ngAfterViewInit() {
console.log(this.ABOUTUS);
this._SetHeaderElements(this.router.config);
}
private _SetHeaderElements(_Routes) {
let parentIndex = 0;
_Routes.forEach(element => {
let childIndex = 0;
if (element.path != "" && element.path != "**") {
let title = element.data.title.toUpperCase();
let url = element.path;
let subMenuRefKey = element.data.title.replace(" ", "").toUpperCase();
let subMenuRef = this.viewObjects[subMenuRefKey];
this.headerElements[parentIndex] = { title, url, subMenuRef };
if (element.children) {
this.childElements[subMenuRefKey] = [];
element.children.map(childElem => {
if (childElem.data) {
let title = childElem.data.title.toUpperCase();
let url = childElem.path;
this.childElements[subMenuRefKey][childIndex] = {
title,
url
};
childIndex++;
}
});
}
parentIndex++;
}
});
console.log(this.headerElements);
console.log(this.childElements);
}
}
<!--template-->
<mat-toolbar>
<ng-container *ngFor="let header of headerElements; let ind = index">
<button mat-button [matMenuTriggerFor]="header.subMenuRef">
{{ header.title }}
</button>
</ng-container>
<mat-menu #ABOUTUS="matMenu">
<button *ngFor="let el of childElements.ABOUTUS" mat-menu-item>
{{ el.title }}
</button>
</mat-menu>
<mat-menu #BUSINESSPROFILE="matMenu"></mat-menu>
<mat-menu #TECHNOLOGY="matMenu"></mat-menu>
<mat-menu #CAREERS="matMenu"></mat-menu>
</mat-toolbar>
路线如下:
const APP_ROUTES: Routes = [
{
path: "",
redirectTo: "/home",
pathMatch: "full"
},
{
path: "home",
data: { title: "Home" },
component: HomeComponent
},
{
path: "about-us",
children: ABOUTUS_ROUTES,
data: { title: "About Us" }
},
{
path: "business-profile",
children: BUSINESSPROFILE_ROUTES,
data: { title: "Business Profile" }
},
{
path: "technology",
children: TECHNOLOGY_ROUTES,
data: { title: "Technology" }
},
{
path: "careers",
children: CAREERS_ROUTES,
data: { title: "Careers" }
},
{
path: "**",
redirectTo: "/home",
pathMatch: "full"
}
];
------------------------------子路由------------------ ---------------
const ABOUTUS_ROUTES = [
{
path: "",
component: AboutusComponent
},
{
path: "corporate-summary",
component: CorporateSummaryComponent,
data: { title: "CORPORATE SUMMARY" }
},
{
path: "history",
component: HistoryComponent,
data: { title: "HISTORY" }
},
{
path: "network",
component: NetworkComponent,
data: { title: "NETWORK" }
}
];
const BUSINESSPROFILE_ROUTES = [
{
path: '', component: BusinessProfileComponent
},
{
path: 'chassis', component: ChassisComponent,
data:{title:'Chassis'}
},
{
path: 'susspension', component: SuspenssionsComponent,
data: {title: 'Susspenssions'}
}
];
const TECHNOLOGY_ROUTES = [
{
path: "",
component: TechnologyComponent
},
{
path: "research-and-development",
component: RnDComponent,
data: { title: "Research & Development" }
},
{
path: "ultra-high-tensile-stamping",
component: UhtStampingComponent,
data: { title: "Ultra-High Tensile Stamping" }
}
];
CAREERS_ROUTES = [
{
path: "",
component: CareersComponent
},
{
path: "shop-floor-execellence",
component: ShopFloorExcellenceComponent,
data: { title: "Shop Floor Execellence" }
},
{
path: "graduate-program",
component: GraduateProgramComponent,
data: { title: "Graduate Program" }
}]
解决方案
推荐阅读
- javascript - 如何构造一个 .find() 函数,该函数接受一个值数组并返回一个键对象:值
- javascript - 如何用函数包装导出工厂的组件?
- react-native - React Native - 如何向 Pressable 组件添加不透明度反馈?
- python - 带有pickle通道的python3子进程
- java - 使用axistools-maven-plugin时未检测到全局绑定
- c - 如何找到 Linux 内核读取系统调用的实现?
- terraform - Terraform 仅在条件为真时将值传递给列表
- random - 随机沃克喜欢从上往左移动?
- sql - 无法将字节数据存储到 sqlserver
- javascript - 如何在 vue 中仅显示对象的键值