我是角色2的新手。我正在尝试通过一个yutube视频来学习角度服务。 Angular 2 Tutorial (2016) - Services
但我收到错误
无法解析'ContactListComponent'(?)的所有参数。确保 所有参数都使用Inject进行修饰或具有有效类型
如何解决我的问题。
我的代码如下:
contact.ts
export interface Contact {
firstname : string,
lastname : string,
address : string
}
模拟contact.ts
import {Contact} from "./contact";
export const CONTACTS: Contact[] =[
{firstname : "John", lastname : "A", address : "john home"},
{firstname : "David", lastname : "B", address : "david home"},
{firstname : "Johnson", lastname : "C", address : "Johny home"},
{firstname : "Bobby", lastname : "D", address : "Bobby home"},
];
contact.service.ts
import { Injectable } from 'angular2/core';
import { CONTACTS } from "./mock-contact";
@Injectable()
export class ContactService {
getContacts(){
return Promise.resolve(CONTACTS);
}
}
接触list.component.ts
import {Component} from 'angular2/core';
import {ContactComponent} from "./contact.component";
import { ContactService } from './contact.service';
import {Contact} from './contact';
import {OnInit} from 'angular2/core';
@Component({
selector : "contact-list"
template : `
<ul>
<li *ngFor="#contact of contacts"
(click)="onSelect(contact)" [class.clicked] =" selectedContact === contact">
{{contact.firstname}}s App
</li>
</ul>
<contact [contact]="selectedContact"></contact>
`,
styleUrls: ["../src/css/app.css"],
providers : [ContactService]
directives : [ContactComponent],
})
export class ContactListComponent implements OnInit {
public contacts : Contact[];
public selectedContact = {};
constructor(private _contactService: ContactSevice) {}
onSelect(contact){
this.selectedContact = contact;
}
getContacts(){
this._contactService.getContacts().then((contacts:Contact[]) => this.contacts = contacts)
}
ngOnInit():any{
this.getContacts();
}
}
有人可以帮我解决这个问题吗?我真的陷入了这个问题。
答案 0 :(得分:3)
似乎是一个错字:
constructor(private _contactService: ContactSevice) {}
应该是
constructor(private _contactService: ContactService) {}
(缺少r
)