问题
I have an root-app component which is defined like this in the template.
template: `
<dev-table (complete)="onSelect(developer)"></dev-table>
<dev-details [selectedDeveloper]="selectedDeveloper"></dev-details>
`
directives: [DevDetailsComponent, DevTableComponent],
providers: [DevValueService, provide(DevService, {useClass: DevService})]
is a list and on selection of one of the internal list it should send the value of the list (developer) which is passed into as selected developer. @Input is defined right and is taken into correctly. But the @output is giving an error Error: Output is not defined
What definition is needed or what is the definition method. I am missing something.
This is the class definition:
@Component({
selector: 'dev-table',
template: `
<ul class="dev">
<li *ngFor="#developer of developers"
[class.selected]="developer === selectedDeveloper;this.complete.next();"
(click)="onSelect(developer)">
<span class="spanbackground">{{developer.name}}</span> - {{developer.skill}}
</li>
</ul>
`,
providers: [DevService]
})
export class DevTableComponent implements OnInit{
public developers : Developer[];
public selectedDeveloper : Developer;
constructor(private _DevService: DevService) { }
@Output() complete = new EventEmitter();
public onSelect(developer: Developer) { this.selectedDeveloper = developer; }
getDevelopers(){
this._DevService.getDevelopers().then(developers => this.developers = developers)
}
ngOnInit(){
this.getDevelopers();
}
}
UPDATED: The final working code did not have developer === selectedDeveloper;this.complete.next();
rather this.complete.next()
was put into the onSelect function.
回答1:
"@output is giving an error Error: Output is not defined What definition is needed or what is the definition method. I am missing something." :
import {Output} from "angular2/core";
You have to import the definition of any class you are using.
回答2:
If you want to pass the value to the parent component you can leverage custom event. This code is located in the template of the parent component:
@Component({
(...)
template: `
<dev-table (complete)="someMethod($event.value)"></dev-table>
`
})
export class ParentComponent {
someMethod(value) {
console.log('complete event - value = ' + value);
}
}
To trigger the event, you could do something like that in your child component:
@Component({
(...)
template: `
(...)
<span (click)="triggerCompleteEvent()">Trigger complete event</span>
`
})
export class DevTableComponent implements OnInit{
@Output()
complete:EventEmitter;
constructor() {
this.complete = new EventEmitter();
}
triggerCompleteEvent() {
this.complete.emit(someValue);
}
}
someValue
corresponds to the value you want the event contains and event subscribes can get.
Hope it helps you, Thierry
来源:https://stackoverflow.com/questions/34808134/error-output-is-not-defined-when-passing-value-outside-a-directive-to-parent