javascript - 来自延迟加载组件的 Angular 2+ 访问/更改变量
问题描述
我想在延迟加载之后使用NewDataSet
我试过 add @Input() DataList
or private DataList
,但没有用。
app.component.html:
<ul>
<li *ngFor="let Data of DataList">{{Data.Name}}></li>
</ul>
<button (click)='getLazy()'>Lazy Load New DataSet</button>
app.component.ts:
import OldDataSet from '../assets/OldDataSet.json';
...
export class AppComponent {
DataList:Array<Object> = OldDataSet;
constructor(
private viewContainerRef: ViewContainerRef,
private cfr: ComponentFactoryResolver
) {}
async getLazy() {
this.viewContainerRef.clear();
const { Lazy1Component } = await import('./lazy1.component');
this.viewContainerRef.createComponent(
this.cfr.resolveComponentFactory(Lazy1Component)
);
}
}
懒惰1.component.ts:
import NewDataSet from '../assets/NewDataSet.json';
...
export class Lazy1Component implements OnInit {
ngOnInit(): void {
this.DataList = NewDataSet
}
}
解决方案
https://stackblitz.com/edit/angular-dynamic-component-problem
请看一个现场版本。我希望这个解决方案能满足您的需求。
app.component.ts
import {
Component,
ViewChild,
ComponentFactoryResolver,
ViewContainerRef,
Type
} from "@angular/core";
import { PlaceholderDirective } from "./placeholder.directive";
import { DatasetAComponent } from "./dataset-a.component";
import { DatasetBComponent } from "./dataset-b.component";
@Component({
selector: "my-app",
templateUrl: "./app.component.html"
})
export class AppComponent {
hostViewContainerRef: ViewContainerRef;
@ViewChild(PlaceholderDirective, { static: false })
datasetHost: PlaceholderDirective;
constructor(private componentFactoryResolver: ComponentFactoryResolver) {}
onLoadA() {
this.loadNewDataset(DatasetAComponent, "dataset A loaded.");
}
onLoadB() {
this.loadNewDataset(DatasetBComponent, "dataset B loaded.");
}
onClear() {
this.hostViewContainerRef.clear();
}
loadNewDataset(
datasetComponent: Type<DatasetAComponent> | Type<DatasetBComponent>,
message: string
) {
const datasetCmpFactory = this.componentFactoryResolver.resolveComponentFactory(
datasetComponent
);
this.hostViewContainerRef = this.datasetHost.viewContainerRef;
this.hostViewContainerRef.clear();
const componentRef = this.hostViewContainerRef.createComponent(
datasetCmpFactory
);
componentRef.instance.message = message;
}
}
数据集-a.component.ts
import { Component, Input } from "@angular/core";
import colors from "./data/a.json";
@Component({
templateUrl: "./dataset-a.component.html"
})
export class DatasetAComponent {
@Input() message: string;
dataset = colors;
}
app.module.ts
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { AppComponent } from "./app.component";
import { PlaceholderDirective } from "./placeholder.directive";
import { DatasetAComponent } from "./dataset-a.component";
import { DatasetBComponent } from "./dataset-b.component";
@NgModule({
imports: [BrowserModule, FormsModule],
declarations: [
AppComponent,
PlaceholderDirective,
DatasetAComponent,
DatasetBComponent
],
bootstrap: [AppComponent],
entryComponents: [DatasetAComponent, DatasetBComponent]
})
export class AppModule {}
app.component.html
<h1>using view container ref</h1>
<button (click)="onLoadA()">Dataset A</button> |
<button (click)="onLoadB()">Dataset B</button> |
<button (click)="onClear()">Clear View</button>
<br />
<ng-template appPlaceholder></ng-template>
推荐阅读
- php - 对象数组的数组交集
- dll - 在一台 PC 上无法使用 PyInstaller 找到 DLL,但可以在另一台 PC 上找到
- python - 如何根据另一个数据框中的组在熊猫数据框中创建指标列?
- reactjs - 两个不同父母的孩子可以在 React 中拥有相同的键吗
- excel - UserForm ComboBox 从动态工作表列表填充,从第一个 ComboBox 的选择中添加第二个 ComboBox
- javascript - React Redux:我的 React 组件没有收到更新的数组作为道具
- c# - Process files concurrently and asynchronously
- html - vue.js 更改整个网站的字体
- c# - 使用 redis-ha 集群进行 Redlock
- ios - Xcode 11.5 - 目前无法安装此应用