question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Ivy] @ViewChild doesn't read me ElementRef

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/core

Is this a regression?

Maybe, it seems to be available if we read this status

Description

I’m testing Ivy on a little application with the beta.3 but I have a component like this:

@Component({
    selector: 'tracklist',
    templateUrl: './tracklist.component.html',
    styleUrls: ['./tracklist.component.css'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class TracklistComponent implements AfterViewInit {
...
    @ViewChild('matTable', {read: ElementRef}) elRef;

constructor(private route: ActivatedRoute, private renderer: Renderer2) { }


ngAfterViewInit() {
...
  this.renderer.setStyle(this.elRef.nativeElement, 'overflow-y', 'hidden');
}

When I run ng serve --aot and I instantiate this component, I have this error:


Error TypeError : Cannot read property 'nativeElement' of undefined

elRef is always undefined.

🌍 Your Environment

Angular Version:


Angular CLI: 7.2.4
Node: 10.10.0
OS: darwin x64
Angular: 8.0.0-beta.3
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router
... service-worker

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.12.4
@angular-devkit/build-angular     0.12.4
@angular-devkit/build-optimizer   0.12.4
@angular-devkit/build-webpack     0.12.4
@angular-devkit/core              7.2.4
@angular-devkit/schematics        7.2.4
@angular/cdk                      7.3.1
@angular/cli                      7.2.4
@angular/material                 7.3.1
@ngtools/webpack                  7.2.4
@schematics/angular               7.2.4
@schematics/update                0.12.4
rxjs                              6.3.3
typescript                        3.2.4
webpack                           4.28.4

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
gkalpakcommented, Feb 11, 2019

FYI: I just run in the inheritance issue myself and working on a fix for ngcc.

2reactions
alexzuzacommented, Feb 11, 2019

@wKoza Thank you for the repo.

You’re right that the error Cannot read property 'toArray' of undefined at mergeQueryListAndSet can’t be seen with OnPush since that error is eaten by this code.

https://github.com/angular/angular/blob/81329c85b3194d3ee1b3614b6349edc6c2fd6a60/packages/core/src/render3/instructions.ts#L2826-L2834

And you are also right that the error has something to do with ngcc.

It doesn’t generate features: [InheritDefinitionFeature] for component definitions like MatTable, CdkRowDef, etc.

cc: @petebacondarwin

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 8 viewChild returns undefined - Stack Overflow
I had a similar problem where ViewChild component is undefined in onInit() method. This fixed the issue: // Ensure Change Detection runs ...
Read more >
In-depth analysis of ViewChild, ElementRef and ViewChildren ...
In the parent component, we are using divelement2 as ElementRef. Now write a setter method that will set ele2 when the child element...
Read more >
Component Queries Metadata Appears To Be Broken When ...
When Ivy is enabled, @Component() queries don't work - only @ViewChild() (and similar) decorators appear to work. However, if Ivy is ...
Read more >
Angular Integration with Stencil
Stencil can generate Angular component wrappers for your web components. This allows your Stencil components to be used within an Angular application. The ......
Read more >
Querying the DOM with @ViewChild and @ViewChildren
Notice that we have used ngAfterViewInit() hooks. Why, you might ask? When the application loads, the child element may not be loaded completely ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found