简体   繁体   English

angular 4 viewchild nativeElement offsetWidth 意外更改

[英]angular 4 viewchild nativeElement offsetWidth changing unexpectedly

I have a component like so:我有一个像这样的组件:

import { Component, OnInit, OnChanges, ViewChild, ElementRef, Input, Output, ViewEncapsulation, EventEmitter, AfterViewInit } from '@angular/core';
import * as d3 from 'd3';

@Component({
  selector: 'coefficient-histogram',
  templateUrl: './coefficient-histogram.component.html',
  styleUrls: ['./coefficient-histogram.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class CoefficientHistogramComponent implements OnInit {

  @ViewChild('coefficienthistogram') private chartContainer:ElementRef;

  constructor() { }

  ngOnInit() {
    let element = this.chartContainer.nativeElement;
    console.log(element.offsetWidth);
  }
}

with a template like so:使用这样的模板:

<div class="coefficenthistogram" #coefficienthistogram style="width: 100%; height: 100%"></div>

When I first browse to the page containing the component (or refresh the page) the console logs offsetWidth as being 504, which is the right size because the enclosing div which holds this element is that size.当我第一次浏览到包含该组件的页面(或刷新页面)时,控制台将 offsetWidth 记录为 504,这是正确的大小,因为包含此元素的封闭 div 就是该大小。 However, when I navigate to another page then return to this one, it reports a different size (308), which does not match the container in which it resides.但是,当我导航到另一个页面然后返回到这个页面时,它报告了不同的大小 (308),这与它所在的容器不匹配。 Further, if I navigate to yet a different page, then return to this one, it reports yet a different size (126).此外,如果我导航到另一个页面,然后返回到这个页面,它会报告不同的大小 (126)。

The page itself does not change - the container of this component does not change size.页面本身不会改变——这个组件的容器不会改变大小。 I've verified this by in the Chrome debugger - the enclosing div has a width of 504 whether I load the page new or navigate to it from other pages.我已经在 Chrome 调试器中验证了这一点 - 无论我是加载新页面还是从其他页面导航到它,封闭 div 的宽度都是 504。

What would cause the offsetWidth to be reported differently based on which other pages I navigate to???什么会导致根据我导航到的其他页面不同地报告 offsetWidth ???

I am using native angular 4 routing in the application, and I have nested/child routes.我在应用程序中使用原生 angular 4 路由,并且我有嵌套/子路由。 I don't know if this information is helpful or not.我不知道这些信息是否有帮助。

Any help here much appreciated.非常感谢这里的任何帮助。

I had met the similar issue, resolved by setting read parameter 我遇到了类似的问题,可以通过设置读取参数来解决

import 进口

import { ElementRef } from '@angular/core';

ViewChild ViewChild

@ViewChild(MyComponent, { read: ElementRef }) myComponent : ElementRef 

use 采用

onClick(){
    console.log("directive width:", this.myComponent.nativeElement.offsetWidth)
}

check for meta properties on doc 检查 文档的 元属性

The first issue is that you can't work with your view html in ngOnInit. 第一个问题是您不能使用ngOnInit中的view html。 You have to implements AfterViewInit interface and use method: 您必须实现AfterViewInit接口并使用方法:

  ngAfterViewInit() {
    // ...
  }

In this method you can work with your html component because component is rendered and exist. 在此方法中,您可以使用html组件,因为组件已呈现并存在。 Try it, i hope maybe it will help. 试试吧,我希望它会有所帮助。

This is a common pain point in client development.这是客户端开发中常见的痛点。 It's usually recommend to implement AfterViewInit and place your code in a timeout.通常建议实现 AfterViewInit 并将您的代码置于超时状态。 However I've ran into issues with this too, specifically, the kind of behavior you describe.但是,我也遇到了这个问题,特别是您描述的行为类型。 I recommend leveraging RxJs and implementing something like the following:我建议利用 RxJs 并实现如下内容:

ngAfterViewInit(){ interval(300).pipe(take(2)).subscribe(() => { //your code here// }); }

This gives a pretty good delay to let the view content render and also makes a second pass avoiding having to use an inefficient AfterViewChecked hack etc.这为让视图内容呈现提供了相当好的延迟,并且还进行了第二遍,避免了必须使用低效的 AfterViewChecked hack 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular:在ViewChild上未定义nativeElement - Angular: nativeElement is undefined on ViewChild Angular 的 nativeElement 的 offsetWidth 为 0 - Angular's nativeElement's offsetWidth is 0 角度2 ElementRef nativeElement offsetWidth始终为0 - Angular 2 ElementRef nativeElement offsetWidth always 0 Angular 6-nativeElement span类的@ViewChild访问值 - Angular 6 - @ViewChild Access value of nativeElement span class Angular @viewChild 元素的 "nativeElement: 和 "value" 未定义 - Angular @viewChild element's "nativeElement: and "value" are undefined Angular 8 @ViewChild 模板变量错误 - 无法读取未定义的属性“nativeElement” - Angular 8 @ViewChild Template Variable Error - Cannot read property 'nativeElement' of undefined Angular 2 ViewChild不起作用-无法读取未定义的属性nativeElement - Angular 2 viewchild not working - cannot read property nativeElement of undefined 使用@viewchild 时,无法以角度读取未定义的属性“nativeElement” - Cannot read property 'nativeElement' of undefined in angular when using @viewchild Angular 4 无法使用 ViewChild 读取未定义的属性 nativeElement - Angular 4 cannot read property nativeElement of undefined using ViewChild 如何从 Ionic 4/Angular7 中的@ViewChild 获取 nativeElement? - How to get nativeElement from @ViewChild in Ionic 4/Angular7?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM