javascript - 使用同一组件的不同属性构建 n 个实例
问题描述
我有一个组件,它有一个字符串列表作为属性。我想创建该组件的 n 个实例,其中包含不同的字符串列表并呈现它们。我能怎么做?
<mat-sidenav-container>
<mat-sidenav #sidenav>
<mat-nav-list>
<ng-template let-art let-last="last" ngFor [ngForOf]="categories">
<a mat-list-item [routerLink]="['/category/', art.name]"> {{art.name}} </a>
</ng-template>
<a mat-fab color="accent" class="mat-fab-bottom-right fixed z-3" (click)="prueba()">
<mat-icon>add</mat-icon>
</a>
</mat-nav-list>
</mat-sidenav>
<mat-sidenav-content>
<div style="height: 88vh;">
<router-outlet></router-outlet>
</div>
</mat-sidenav-content>
</mat-sidenav-container>
import { Component } from '@angular/core';
import { Routes, Router, Route } from '@angular/router';
import { AppRoutingModule } from './app-routing.module';
import { CategoryComponent } from './category/category.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
routes: Routes = AppRoutingModule.getRoutes();
categories : Array<CategoryComponent>;
title = 'app works!';
i: number;
constructor(private router: Router){
this.i = 11;
console.log(this.routes);
this.categories = new Array();
}
prueba(){
var a : CategoryComponent = new CategoryComponent();
a.setName(this.i.toString())
this.i++;
this.categories.push(a);
}
navegar(ruta:string){
this.router.navigate([ruta]);
}
}
解决方案
推荐阅读
- javascript - 如何在动态组件标签内重用vue中的组件?
- javascript - 基于 JSON 对象时如何使用 ASP 更新 URL 地址
- assembly - 两个输入并将它们相乘。如何做到这一点?
- react-native - 反应原生图像背景不是全屏
- discord - 是否可以在不和谐中使用功能性前缀文本?像悬停或可点击
- c - C - memcpy with char * 长度大于源字符串长度
- sql - 如何创建从表中读取的动态视图?
- excel - 如何使用 Office JS API 在 Excel 中对行或列进行分组
- git - 您的分支领先于“origin/foobranch” N 次提交 - 什么提交?
- drake - 在 Ubuntu 18.04 上本地安装 Drake 的问题