我有一个父组件和几个子组件显示为标签。我很难控制儿童ngOnInit的行为,因为当从一个标签切换到另一个标签时,孩子的ngOnInit不会触发。
父亲是应用程序管理,它的html是:
<mat-tab-group dynamicHeight="true">
<mat-tab label="Application Loader">
<app-loader [deviceId]="deviceId"></app-loader>
</mat-tab>
<mat-tab label="Application Config">
<app-config [deviceId]="deviceId"></app-config>
</mat-tab>
<mat-tab label="Docker">
<app-docker [deviceId]="deviceId"></app-docker>
</mat-tab>
<mat-tab label="Application Logs">
<app-logs [deviceId]="deviceId"></app-logs>
</mat-tab>
<mat-tab label="Ack Logs">
<exec-logs [deviceId]="deviceId"></exec-logs>
</mat-tab>
<mat-tab label="Docker Logs">
<docker-logs [deviceId]="deviceId"></docker-logs>
</mat-tab>
</mat-tab-group>当在应用程序的ngOnint中引入一个ngOnint时,这种方法是行不通的。我需要从应用程序加载程序中的事件中实现在app-docker中的订阅,就像应用程序更改在app-docker中显示一样,但是,由于ngOninit在导航到该标签时不会触发,所以我不知道如何解决这个问题。
VC的结构是aa,如下所示:
>app-config
>app-docker
>app-loader
>app-logs
>docker-logs
>exec-logs
app-management.component.html
app-management.component.scss
app-management.component.ts
app-management.service.ts我试过这个:在我的应用程序-docker.ts上
import { Component, OnInit, Input, ChangeDetectorRef } from '@angular/core';
import { AppManagementService } from "../app-management.service";
import { SnackMessageService } from "app/main/common/services/snackMessage.service";
import { Subscription } from 'rxjs';
@Component({
selector: "app-docker",
templateUrl: "./app-docker.component.html",
styleUrls: ["./app-docker.component.scss"],
})
export class AppDockerComponent implements OnInit {
@Input() deviceId: string;
configErrorMessage: string;
dataContainer: any;
application: any;
private appChangeSub: Subscription;
constructor(
private appManagementService: AppManagementService,
private snackMessageService: SnackMessageService, private cd: ChangeDetectorRef) {
this.dataContainer = {
containerInfo: [],
dockerInfo: [],
timestamp: ""
}
this.application = {
lastUpdate: new Date(0),
registryId: "",
applicationId: ""
}
}
ngOnInit() {
this.appManagementService.getDevicepplication(this.deviceId).then(response => {
this.application = response
if (this.application.applicationId !== "") {
this.postContainerInfo();
}
this.appChangeSub = this.appManagementService.onSelectedApplicationsChanged.subscribe(app => {
this.application.applicationId = app
})
}).catch(()=> this.configErrorMessage = "Oops, could not get application info")
}
public postContainerInfo() {
this.appManagementService.postContainerInfo(this.deviceId).then(() => { this.getDockerContainerInfo() }).catch(() => this.configErrorMessage = "Oops, could not send container info message.")
}
public getDockerContainerInfo() {
this.appManagementService.getDockerContainerInfo(this.deviceId).then(response => {
this.dataContainer = response
}).catch(() => this.configErrorMessage = "Oops, could not get docker data.");
}应用程序加载器加载一个新的应用程序,每次应用程序更改时,我都需要在没有按任何按钮的情况下显示这些更改。这有可能吗?
我的应用程序加载器组件.ts
import { Component, OnInit, Input, OnDestroy } from "@angular/core";
import { FormGroup, FormBuilder, Validators } from "@angular/forms";
import { RegistryModel } from "app/main/registry/registry.model";
import { ApplicationModel } from "app/main/registry/application.model";
import { AppManagementService } from "../app-management.service";
import { SnackMessageService } from "app/main/common/services/snackMessage.service";
import { MatDialogRef, MatDialog } from "@angular/material";
import { FuseConfirmDialogComponent } from "@fuse/components/confirm-dialog/confirm-dialog.component";
@Component({
selector: "app-loader",
templateUrl: "./app-loader.component.html",
styleUrls: ["./app-loader.component.scss"]
})
export class AppLoaderComponent implements OnInit, OnDestroy {
@Input() deviceId: string;
ngOnInit() {}
public sendRegistryForm(): void {
this.confirmDialogRef = this.matDialog.open(FuseConfirmDialogComponent, {
disableClose: false
});
this.confirmDialogRef.componentInstance.confirmMessage = "Are you sure you want to send this application to the device?";
this.confirmDialogRef.afterClosed()
.subscribe(result => {
if (result) {
this.appManagementService.sendApplicationToDevice(this.deviceId, this.registryForm.value.registryId, this.registryForm.value.applicationId).then(() => {
this.loaderErrorMessage = null;
this.snackMessageService.sendMessage("Application sent");
}).catch(() => this.loaderErrorMessage = "Oops, could not send the application to the device.");
}
this.confirmDialogRef = null;
});
}
}在appmanagementservice中,每次加载新应用程序时,我都尝试使用next实现发射器,但是,我不确定这是否正确。app-docker中的postContainerInfo方法是向mqtt服务发送一条消息,更新一个ddbb,然后通过getContainerInfo()更新信息。
我的应用程序管理。服务:
import { Injectable } from "@angular/core";
import { RegistryModel } from "app/main/registry/registry.model";
import { BackEndCommunicationService } from "app/main/common/services/beComm.service";
import { BehaviorSubject, Subject } from "rxjs";
@Injectable({
providedIn: "root"
})
export class AppManagementService {
onSelectedApplicationsChanged: Subject<any>;
constructor(private backEndCommunicationService: BackEndCommunicationService) {
this.onSelectedApplicationsChanged = new Subject();
}
public getDevicepplication(deviceId: string): Promise<any> {
return new Promise((resolve, reject) => {
this.backEndCommunicationService.getResource("/devices/" + deviceId).then((response: any) => {
this.onSelectedApplicationsChanged.next()
resolve(response.response.application);
}).catch(error => {
console.log(error);
reject("Error getting persistant size")
})
})
}
public sendApplicationToDevice(deviceId: string, registryId: string, applicationId: string): Promise<void> {
return new Promise((resolve, reject) => {
const sendObject = {
data: {
registryId: registryId,
applicationId: applicationId
}
};
this.backEndCommunicationService.postResource("/devices/" + deviceId + "/application", sendObject).then(() => {
resolve();
}).catch(error => {
console.log(error);
reject("Error sending app to device");
});
});
}发布于 2020-05-18 12:11:59
ngOnInit不会运行,因为DOM:选项卡标签和内容构建了一次,而不是每次点击就重新生成。
您可以使用selectedTabChange事件对选项卡更改作出反应。你到底想达到什么目的?
https://stackoverflow.com/questions/61869228
复制相似问题