Angular HttpService没有提供程序
当我运行以下项目时,会出现错误: 没有HttpService的提供商!(AppComponent->HttpService) 有人能帮我吗 我的AppComponent:Angular HttpService没有提供程序,angular,httpservice,angular2-providers,Angular,Httpservice,Angular2 Providers,当我运行以下项目时,会出现错误: 没有HttpService的提供商!(AppComponent->HttpService) 有人能帮我吗 我的AppComponent: import {Component} from 'angular2/core'; import {HttpService} from "./htttp.service"; @Component({ selector: 'my-app', template: ` <div> <div
import {Component} from 'angular2/core';
import {HttpService} from "./htttp.service";
@Component({
selector: 'my-app',
template: `
<div>
<div class="input">
<label for="title">Title</label>
<input type="text" id="title" #title>
</div>
<div class="body">
<label for="body">Body</label>
<input type="text" id="body" #body>
</div>
<div class="user-id">
<label for="user-id">User ID</label>
<input type="text" id="user-id" #userid>
</div>
<button (click)="onPost(title.value, body.value, userid.value)">Post Data</button>
<button (click)="onGetPosts()">Get All Posts</button>
<p>Response: {{response | json}}</p>
</div>,
providers: [HttpService]
`,
})
export class AppComponent {
response: string;
constructor(private _httpService: HttpService){}
onGetPosts(){
this._httpService.getPosts().subscribe(
response => this.response=response,
error => console.log(error)
)
}
}
问题出在哪里?应该是
import {HttpService} from "./http.service";
在声明提供者之前,您忘记关闭模板文字“back quote”
@Component({
selector: 'my-app',
template: `
<div>
<div class="input">
<label for="title">Title</label>
<input type="text" id="title" #title>
</div>
<div class="body">
<label for="body">Body</label>
<input type="text" id="body" #body>
</div>
<div class="user-id">
<label for="user-id">User ID</label>
<input type="text" id="user-id" #userid>
</div>
<button (click)="onPost(title.value, body.value, userid.value)">Post Data</button>
<button (click)="onGetPosts()">Get All Posts</button>
<p>Response: {{response | json}}</p>
</div>`,
providers: [HttpService]
})
@组件({
选择器:“我的应用程序”,
模板:`
标题
身体
用户ID
发布数据
获取所有帖子
响应:{Response | json}
`,
提供者:[HttpService]
})
您的问题就在这里从“/htttp.service”导入{HttpService}
您在htttp
中有3个t
,它应该只有2个!多么愚蠢的错误!:)别担心。现在点击UP
按钮!谢谢是的。它不起作用。我稍后再试
import {HttpService} from "./http.service";
@Component({
selector: 'my-app',
template: `
<div>
<div class="input">
<label for="title">Title</label>
<input type="text" id="title" #title>
</div>
<div class="body">
<label for="body">Body</label>
<input type="text" id="body" #body>
</div>
<div class="user-id">
<label for="user-id">User ID</label>
<input type="text" id="user-id" #userid>
</div>
<button (click)="onPost(title.value, body.value, userid.value)">Post Data</button>
<button (click)="onGetPosts()">Get All Posts</button>
<p>Response: {{response | json}}</p>
</div>`,
providers: [HttpService]
})