Ich begann Angular2 zu lernen, aber .. Ich habe versucht, einen Service und Import in meinem compoment zu schaffen, aber ich bekomme diese Fehlermeldung:Angular2: Injektion einer Service
error TS2339: Property 'commentService' does not exist on type 'CommentsComponent'.
comment.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class CommentService {
testfunction() {
return 'valoare';
}
}
comments.component.ts
import { Component, OnInit } from '@angular/core';
import { CommentService } from '../services/comment.service';
@Component({
template: 'dadada',
providers: [CommentService]
})
export class CommentsComponent implements OnInit {
construct(commentService: CommentService) {
}
ngOnInit() {
console.log(this.commentService.testfunction());
}
}
app.component.ts
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
@Component({
selector: '[web-application]',
templateUrl: 'template/home',
directives: [ROUTER_DIRECTIVES]
})
export class AppComponent { }
app.routes.ts
import { provideRouter, RouterConfig } from '@angular/router';
import { CommentsComponent } from './components/comments.component';
import { HomeComponent } from './components/home.component';
const routes: RouterConfig = [
{ path: '', component: HomeComponent },
{ path: 'comments', component: CommentsComponent }
];
export const appRouterProviders = [
provideRouter(routes)
];
main.ts
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './components/app.component';
import { appRouterProviders } from './app.routes';
import { CommentService } from './services/comment.service'
bootstrap(AppComponent, [
appRouterProviders,
CommentService
])
.catch(err => console.error(err));
Jemand eine Idee, warum ich kann‘ t den Dienst injizieren?
Ich habe es schon versucht, aber ich habe das: Eine Parameter-Eigenschaft ist nur in einer Konstruktor-Implementierung erlaubt. –
Ich sehe. Das klingt vernünftig. Ein Konstruktor sollte "Konstruktor" anstatt "Konstrukt" genannt werden ;-) –
Lol gleiches Problem. Ich hatte einen Grammatikfehler im Wort "Konstruktor".: D – codepleb