我有一个组件,其中图表样式存储在一个变量中(不能使用width。这是为ngIf准备的)。但是,每当我更改路由时,这个类组件中的所有变量都会获得undefined值。
我尝试使用ngIf,服务(它们也是未定义的),使用ngOnDestroy() (路由更改时不会调用它)
这是我的代码,有更多的解释。
组件类:
您可以看到变量setWidth,它跟踪d3图表的width,当路由更改时它就会变成undefined,我不确定是因为ngOnChange,因为HostListener会检测到窗口的大小调整,并相应地调整width的大小,即使它在路由更改后中断。
export class BrushBarChartComponent implements OnInit, OnChanges
{
@Input() timeSeriesData: Object;
@ViewChild('timeSeriesChart', { static: true }) private timeSeriesChart: ElementRef;
setWidth: number;
constructor() { }
ngOnInit() {}
ngOnChanges() {
this.render();
}
@HostListener('window:resize', ['$event'])
onScreenResize () {
this.render();
}
render() {
let divWidth = this.timeSeriesChart.nativeElement.offsetWidth;
if (divWidth === 0) {
divWidth = this.setWidth;
} else {
this.setWidth = divWidth;
}
const margin = { top: 20, right: 50, bottom: 90, left: 40 },
margin2 = { top: 130, right: 50, bottom: 30, left: 40 },
width = divWidth - margin.left - margin.right;
}
}以下是模板:
<div id="timeSeriesChart" #timeSeriesChart *ngIf="DestroyHide"></div>下面是路由文件如何处理更改路由请求。无法将整个代码发布到文件中。
{
path: 'delete',
loadChildren: () => import('app/delete/delete.module').then(m => m.deleteModule),
canActivate: [AuthGuard],
data: {preload: true}
}发布于 2020-08-14 03:08:59
https://stackoverflow.com/questions/63401195
复制相似问题