Better error messages when an `@Output` property is not initialized.
See original GitHub issueRelated: #3754 (should be fixed together)
Listing an event in a directive without creating an associated EventEmitter
instance results in cryptic error:
TypeError: Cannot read property ‘observer’ of undefined at Function.ObservableWrapper.subscribe (angular2.js:19403) at EventEmitterAccessor.subscribe (angular2.js:26062) at AppViewManagerUtils._setUpEventEmitters (angular2.js:20932) at AppViewManagerUtils._hydrateView (angular2.js:20896) at AppViewManagerUtils.hydrateRootHostView (angular2.js:20814) at AppViewManager.createRootHostView (angular2.js:23990) at angular2.js:13594 at Zone.run (zone.js:113) at Zone.run (angular2.js:12928) at zoneBoundFn (zone.js:86)
To reproduce:
@Directive({
selector: '[foo]'
})
class Foo {
@Output() foo; // not initialized to = new EventEmitter();
}
Issue Analytics
- State:
- Created 8 years ago
- Reactions:9
- Comments:11 (7 by maintainers)
Top Results From Across the Web
An error occurred: @Output not initialized - angular
The problem: I was initializing the EventEmmitter object in the ngOnInit method from my component class. Solution: I moved the initialization ...
Read more >How to fix "variable might not have been initialized" error in ...
The error message is very clear, it's saying that local variable "b" has not initialized until line 14, where it has been read,...
Read more >Input() and @Output() properties - Angular Hispano
An @Output() property should normally be initialized to an Angular EventEmitter with values flowing out of the component as events. Just like with...
Read more >Writing Functions
The Function node allows JavaScript code to be run against the messages that are passed through it. The message is passed in as...
Read more >Class Constructor Methods - MATLAB & Simulink
properties · end ; methods function ; %% Pre Initialization %% % Any code not using output argument (obj) ; if · %...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For me the error stopped when I changed the import statement from import { EventEmitter } from ‘events’; to import { EventEmitter } from ‘@angular/core’. The first option was suggested by angular as default but I think that the second one should be used. Everything works fine now after I changed it.
Example: @Output() variableToEmit: EventEmitter<string> = new EventEmitter();
showsString(text: any) { this.variableToEmit.emit(text); }
Hope that helps .
Hi @mhevery @vicb I submitted a PR for this issue some time ago. I just fixed a conflict, lint errors and tests. See #19116
Please let me know if there is something I can do. Cheers!