- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 Material 标签:https://material.angular.io/components/tabs/overview
我有一个带有选项卡 View 的页面,我想通过单击按钮来为每个选项卡填充一个自定义组件。假设我有两个组件,选择器为“customcomponent1”和“customcomponent2”。我想让它这样当我点击一个按钮时,一个新的“对象”将被添加到列表中,如下所示:
{'component_name':'customcomponent1'}
一个新的标签“Tab2”将动态创建,如果它是这样的话:
<mat-tab-group>
<mat-tab label="Tab1">
<button (click)="createNewTabWithComponent('customcomponent1')"></button>
</mat-tab>
<mat-tab label="Tab2">
<customcomponent1></customcomponent1>
</mat-tab>
</mat-tab-group>
如果我再次点击按钮...我会得到一个新标签:
<mat-tab-group>
<mat-tab label="Tab1">
<button (click)="createNewTabWithComponent('customcomponent1')"></button>
</mat-tab>
<mat-tab label="Tab2">
<customcomponent1></customcomponent1>
</mat-tab>
<mat-tab label="Tab3">
<customcomponent2></customcomponent2>
</mat-tab>
</mat-tab-group>
我如何使用 angular2/4 完成此操作?我通常会使用 $compile 之类的东西,但我认为 angular2/4 没有。我想避免的是为每个组件创建一堆选项卡(假设我有 10 个组件,真的不想为每个组件创建多个 mat-tab 占位符并在每个组件上设置显示/隐藏标志)和 '硬编码其中的一个组件。
如果解决方案显示如何使用选择器的名称“动态”添加组件(用户在文本框中键入并点击按钮以“添加到列表”,这也是一个经过深思熟虑的答案。
我能想到的一个例子是,如果有一个文本框,有人可以输入任何字符串。如果字符串与组件的名称相匹配,则该组件会以编程方式“动态显示”在屏幕上,就好像它是原始模板的一部分一样。
我正在寻找的我知道不存在的伪代码(我认为,但如果它存在会很好):
<button (click)="addNameOfComponentToListOfCustomComponentStrings('text-from-a-textbox')">Add Component</button>
<div *ngFor="let string_name_of_component in listofcustomcomponentstrings">
<{{string_name_of_component}}><</{{string_name_of_component}}>
</div>
将它们放入选项卡将是一个加号。如果这不可行,请发帖。否则,请发帖。
如果可能有一种解决方法可以使用一些“嵌套 Angular 路由”来完成,它根据 ngFor 中组件的名称拉出组件,那也可能有效......我对任何想法持开放态度。
最佳答案
对于您提出的问题(不想使用 10 个带 *ngIf 的占位符 mat-tab 来隐藏内容),您可以通过路由和延迟加载做得更好。
例如,您的带有标签的页面位于 /tabs
和它的模块是MyTabsModule
.该模块的路由配置如下所示:
const routes = [{
path: 'tabs',
component: MyTabsComponent,
}];
假设您有两个选项卡,left
和 right
.您现在需要的是在惰性模块中添加子组件。像这样简单的东西:
const routes = [{
path: 'tabs',
component: MyTabsComponent,
children: [
{
path: 'left',
loadChildren: 'app/lazy/left.module#LazyLeftModule'
},
{
path: 'right',
loadChildren: 'app/lazy/right.module#LazyRightModule'
}
]
}];
你的 MyTabsComponent
需要以某种方式展示这一点,如何?这是Material文档说(简化):
<h2>My tabs component</h2>
<nav mat-tab-nav-bar>
<a mat-tab-link routerLink="left">Left tab</a>
<a mat-tab-link routerLink="right">Right tab</a>
</nav>
<router-outlet></router-outlet>
但是如果你有多个标签怎么办?嗯,文档说这个,我简化了前面的例子:
<a mat-tab-link
*ngFor="let link of navLinks"
routerLinkActive #rla="routerLinkActive"
[routerLink]="link.path"
[active]="rla.isActive">
{{link.label}}
</a>
现在,您可以创建多个惰性组件,以及这些惰性路由的路由配置。事实上,您甚至可以创建一个脚本来生成1 您的路由配置,甚至可以在构建过程中为您生成那些惰性模块。
1 - Angular Schematics ?
这是假设您知道自己拥有哪些组件。如果您只想让用户在您的文本框 中输入内容怎么办?并自己挑选任何组件?比如,有一个下拉菜单,让用户决定他们想要的所有选项卡,然后最重要的是,延迟加载它们?
怎么样?首先,您的路线 child 现在有点不同:
{
path: 'tabs',
component: MyTabsComponent,
children: [{
path: ':tab',
loadChildren: 'app/lazy/lazy.module#LazyWrapperModule',
}
}
现在,您的 LazyWrapperModule
导出 LazyWrapperComponent
.该组件在模板中有自己的本地路由器导出。它还会根据 url 加载一个组件:
constructor(private route: ActivatedRoute, private router: Router) {}
ngOnInit() {
this.activatedRoute.params.subscribe(params => {
const tab = params.tab;
if (!this.isRouteValid(tab)) {
return this.router.navigate(['error'])
}
this.router.navigate([tab]);
});
}
并且 LazyWrapperModule 也有路由配置:
@NgModule({
imports: [
RouterModule.forChild([{
path: 'tab1',
component: Tab1
},
{
path: 'tab2',
component: Tab2
},
...
{
path: 'tab100',
component: Tab100
}]
],
...
现在,这看起来好多了。您可以从 TabsModule 导航到任何内容。然后它通过参数加载一个组件。您的组件可以,例如如果用户输入了错误的选项卡,则显示错误选项卡,或者您可以简单地提供带有“允许”选项卡列表的预输入等等。有趣的东西!
但是如果您不想限制用户怎么办?你想让他们在那个文本框中输入“my-most-dynamic-component-ever”吗?
您可以简单地动态创建一个组件。再次有一个包装器组件,它创建并注入(inject)组件。例如。模板:
<input [(ngModel)]="name" required>
<textrea [(ngModel)]="template">
<button (click)="createTemplate()">Create template</button>
<div #target></div>
那么组件可以是这样的:
class MyTabsComponent {
@ViewChild('target') target;
name = 'TempComponent';
template: '<span class="red">Change me!</span>';
styles: ['.red { border: 1px solid red; }']
constructor(private compiler: Compiler,
private injector: Injector,
private moduleRef: NgModuleRef<any>) {
}
createTemplate() {
const TempComponent = Component({ this.template, this.styles})(class {});
const TempModule = NgModule({
declarations: [TempComponent]
})(class {});
this.compiler.compileModuleAndAllComponentsAsync(TempModule)
.then((factories) => {
const f = factories.componentFactories[0];
const cmpRef = f.create(this.injector, [], null, this.m);
cmpRef.instance.name = this.name;
this.target.insert(cmpRef.hostView);
});
}
... // other stuff
}
现在,具体如何使用它,取决于您的具体需求。例如。作为家庭作业,您可以尝试像上面这样动态创建组件,并将其注入(inject)到 <mat-tab-group>
中更多以上。或者,动态创建一个到现有组件的路由作为延迟加载的链接。或者……可能性。我们爱他们。
关于angular - 如何使用 Angular 2/4 以编程方式使用 'name/selector of the component' 添加带有组件的新选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49245339/
我一直在阅读有关汇编函数的内容,但对于是使用进入和退出还是仅使用调用/返回指令来快速执行,我感到很困惑。一种方式快而另一种方式更小吗?例如,在不内联函数的情况下,在汇编中执行此操作的最快(stdcal
我正在处理一个元组列表,如下所示: res = [('stori', 'JJ'), ('man', 'NN'), ('unnatur', 'JJ'), ('feel', 'NN'), ('pig',
最近我一直在做很多网络或 IO 绑定(bind)操作,使用线程有助于加快代码速度。我注意到我一直在一遍又一遍地编写这样的代码: threads = [] for machine, user, data
假设我有一个名为 user_stats 的资源,其中包含用户拥有的帖子、评论、喜欢和关注者的数量。是否有一种 RESTful 方式只询问该统计数据的一部分(即,对于 user_stats/3,请告诉我
我有一个简单的 api,它的工作原理是这样的: 用户创建一个请求 ( POST /requests ) 另一个用户检索所有请求 ( GET /requests ) 然后向请求添加报价 ( POST /
考虑以下 CDK Python 中的示例(对于这个问题,不需要 AWS 知识,这应该对基本上任何构建器模式都有效,我只是在这个示例中使用 CDK,因为我使用这个库遇到了这个问题。): from aws
Scala 中管理对象池的首选方法是什么? 我需要单线程创建和删除大规模对象(不需要同步)。在 C++ 中,我使用了静态对象数组。 在 Scala 中处理它的惯用和有效方法是什么? 最佳答案 我会把它
我有一个带有一些内置方法的类。这是该类的抽象示例: class Foo: def __init__(self): self.a = 0 self.b = 0
返回和检查方法执行的 Pythonic 方式 我目前在 python 代码中使用 golang 编码风格,决定移动 pythonic 方式 例子: import sys from typing imp
我正在开发一个 RESTful API。其中一个 URL 允许调用者通过 id 请求特定人员的记录。 返回该 id 不存在的记录的常规值是什么?服务器是否应该发回一个空对象或者一个 404,或者其他什
我正在使用 pathlib.Path() 检查文件是否存在,并使用 rasterio 将其作为图像打开. filename = pathlib.Path("./my_file-name.tif") 但
我正在寻找一种 Pythonic 方式来从列表和字典创建嵌套字典。以下两个语句产生相同的结果: a = [3, 4] b = {'a': 1, 'b': 2} c = dict(zip(b, a))
我有一个正在操裁剪理设备的脚本。设备有时会发生物理故障,当它发生时,我想重置设备并继续执行脚本。我有这个: while True: do_device_control() device
做组合别名的最pythonic和正确的方法是什么? 这是一个假设的场景: class House: def cleanup(self, arg1, arg2, kwarg1=False):
我正在开发一个小型客户端服务器程序来收集订单。我想以“REST(ful)方式”来做到这一点。 我想做的是: 收集所有订单行(产品和数量)并将完整订单发送到服务器 目前我看到有两种选择: 将每个订单行发
我知道在 Groovy 中您可以使用字符串调用类/对象上的方法。例如: Foo."get"(1) /* or */ String meth = "get" Foo."$meth"(1) 有没有办法
在 ECMAScript6 中,您可以使用扩展运算符来解构这样的对象 const {a, ...rest} = obj; 它将 obj 浅拷贝到 rest,不带属性 a。 有没有一种干净的方法可以在
我有几个函数返回数字或None。我希望我的包装函数返回第一个不是 None 的结果。除了下面的方法之外,还有其他方法吗? def func1(): return None def func2(
假设我想设计一个 REST api 来讨论歌曲、专辑和艺术家(实际上我就是这样做的,就像我之前的 1312414 个人一样)。 歌曲资源始终与其所属专辑相关联。相反,专辑资源与其包含的所有歌曲相关联。
这是我认为必须经常出现的问题,但我一直无法找到一个好的解决方案。假设我有一个函数,它可以作为参数传递一个开放资源(如文件或数据库连接对象),或者需要自己创建一个。如果函数需要自己打开文件,最佳实践通常
我是一名优秀的程序员,十分优秀!