Angular 没有日志服务提供商?
这是我的日志服务:Angular 没有日志服务提供商?,angular,Angular,这是我的日志服务: /* * * ./app/comments/components/comment.service.ts * * */ // Imports import { Injectable } from '@angular/core'; import { Http, Response, Headers, RequestOptions } from '@angular/http'; import { Log } from '../model/log.model
/* * * ./app/comments/components/comment.service.ts * * */
// Imports
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Log } from '../model/log.model';
import {Observable} from 'rxjs/Rx';
// Import RxJs required methods
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class LogService {
userData: any;
private date;
// Resolve HTTP using the constructor
constructor (private http: Http) {}
CheckIn (body): Observable<Log[]> {
this.date = new Date();
//let bodyString = JSON.stringify(body); // Stringify payload
var bodyString = 'user_email='+body.email +'&checkin='+this.date;
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded'});
let options = new RequestOptions({ headers: headers }); // Create a request option
return this.http.post('/checkin', bodyString, options) // ...using post request
.map(response => { return response.json()}) // ...and calling .json() on the response to return data
.catch((error:any) => Observable.throw(error.json().error || 'Server error' )); //...errors if any
}
}
这是我的日志组件:
import {Component, EventEmitter, Input, OnChanges,OnInit} from '@angular/core';
import {Observable} from 'rxjs/Rx';
import {Router} from '@angular/router';
import { LogService } from '../services/logService.service';
import {User,SharedService} from '../../SharedService'
@Component({
selector: 'dashboard',
template: `
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>
<div class="panel-body">
You are logged in! Welcome <span>{{ user.name }} </span>
</div>
<button *ngIf="checking" type="submit" class="btn btn-primary btn-block" click)="checkout(user.email)">Checkout</button>
<button *ngIf="!checking" type="submit" class="btn btn-warning btn-block" (click)="checkin(user.email)">Checkin</button>
</div>
</div>
</div>
</div>
`
})
export class LogsComponent {
private checking = false;
user:User;
constructor(
private logService: LogService,
private router: Router,
private ss:SharedService
){
this.user=ss.getUserDetail();
}
checkin(values){
var current = this;
// Variable to hold a reference of addComment/updateComment
let logOperation:Observable<any>;
logOperation = this.logService.CheckIn(values);
logOperation.subscribe(
res => {
if(res.isLoggedIn == true){
console.log('usao u res',res.user)
this.ss.setUserDetail(res.user);
current.router.navigate(['/home']);
}
},
err => { console.log(err) }
);
}
}
从'@angular/core'导入{Component,EventEmitter,Input,OnChanges,OnInit};
从'rxjs/Rx'导入{Observable};
从'@angular/Router'导入{Router};
从“../services/LogService.service”导入{LogService};
从“../../SharedService”导入{User,SharedService}
@组成部分({
选择器:“仪表板”,
模板:`
仪表板
您已登录!欢迎{{user.name}
结账
签到
`
})
导出类日志组件{
私人检查=假;
用户:用户;
建造师(
私人日志服务:日志服务,
专用路由器:路由器,
私有ss:SharedService
){
this.user=ss.getUserDetail();
}
签入(值){
无功电流=此;
//用于保存addComment/updateComment引用的变量
让logOperation:可观察的;
logOperation=this.logService.CheckIn(值);
logOperation.subscribe(
res=>{
如果(res.isLoggedIn==true){
console.log('usao u res',res.user)
这个.ss.setUserDetail(res.user);
current.router.navigate(['/home']);
}
},
err=>{console.log(err)}
);
}
}
有什么建议说明我为什么会出现这个错误吗?您是否在其他组件中使用了
LogService
。没有。。仅在日志组件中尝试将其添加到@Component.providers
arraynow我得到这个:self.parent.context.checkin不是一个函数。。这是另一个问题还是?我不知道,LogModule
是根模块吗?
import {Component, EventEmitter, Input, OnChanges,OnInit} from '@angular/core';
import {Observable} from 'rxjs/Rx';
import {Router} from '@angular/router';
import { LogService } from '../services/logService.service';
import {User,SharedService} from '../../SharedService'
@Component({
selector: 'dashboard',
template: `
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>
<div class="panel-body">
You are logged in! Welcome <span>{{ user.name }} </span>
</div>
<button *ngIf="checking" type="submit" class="btn btn-primary btn-block" click)="checkout(user.email)">Checkout</button>
<button *ngIf="!checking" type="submit" class="btn btn-warning btn-block" (click)="checkin(user.email)">Checkin</button>
</div>
</div>
</div>
</div>
`
})
export class LogsComponent {
private checking = false;
user:User;
constructor(
private logService: LogService,
private router: Router,
private ss:SharedService
){
this.user=ss.getUserDetail();
}
checkin(values){
var current = this;
// Variable to hold a reference of addComment/updateComment
let logOperation:Observable<any>;
logOperation = this.logService.CheckIn(values);
logOperation.subscribe(
res => {
if(res.isLoggedIn == true){
console.log('usao u res',res.user)
this.ss.setUserDetail(res.user);
current.router.navigate(['/home']);
}
},
err => { console.log(err) }
);
}
}