【发布时间】:2017-03-07 09:28:06
【问题描述】:
我正在做一个 Angular2 库,它必须与 SystemJS 和 Webpack 兼容。对于一个组件,我需要检测主体标签的高度和宽度(以像素为单位),然后为子标签设置以像素为单位的尺寸。 Angular LifeCycle 的 SystemJS 和 Webpack 之间的行为会产生不同的结果。我这样做是为了解释它:
foo.component.ts
ngOnChanges() {
console.log("ngOnChanges : " + document.body.clientHeight);
}
ngOnInit(){
this.elHTML.setAttribute("direction", this.direction.toString());
console.log("ngOnInit : " + document.body.clientHeight);
}
ngDoCheck() {
console.log("ngDoCheck : " + document.body.clientHeight);
}
ngAfterContentInit() {
console.log("ngAfterContentInit : " + document.body.clientHeight);
}
ngAfterContentChecked() {
console.log("ngAfterContentChecked : " + document.body.clientHeight);
}
ngAfterViewInit() {
console.log("ngAfterViewInit : " + document.body.clientHeight);
}
ngAfterViewChecked() {
console.log("ngAfterViewChecked : " + document.body.clientHeight);
this.widthHeightApply();
}
widthHeightApply(){
var offsetWidth : number = document.body.clientWidth;
var offsetHeight : number = document.body.clientHeight;
...
}
SystemJS result
ngOnChanges : 767
ngOnInit : 767
ngDoCheck : 767
ngAfterContentInit : 767
ngAfterContentChecked : 767
Webpack result
ngOnChanges : 40
ngOnInit : 40
ngDoCheck : 40
ngAfterContentInit : 206
ngAfterContentChecked : 206
为什么会有不同的结果?怎么可能有同样的结果?
【问题讨论】:
-
实际上,我在使用 webpack 的两个不同项目之间有同样的问题:Angular2-Webpack-starter (body width : "1583px") & preboot/angular-webpack (body width: "1600px")... 注意:不同之处在于 Firefox 上的滚动条宽度:17px。
标签: angular typescript webpack systemjs