作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个带有确认框的 AlertService。我从我的服务中调用它。如果用户点击是,那么我想调用 REST 服务。但是我收到了这个错误:
我相信因为我是从 alertService.confirm() 内部调用的,而 alertService 没有声明 http,所以我收到了这个错误。但我不确定解决此问题的合适方法是什么。
ERROR TypeError: Cannot read property 'http' of undefined
at inspection.service.ts:91
at Object.siFn (alert.service.ts:53)
at Object.eval [as handleEvent] (AlertComponent.html:25)
at handleEvent (core.es5.js:11998)
at callWithDebugContext (core.es5.js:13467)
at Object.debugHandleEvent [as handleEvent] (core.es5.js:13055)
at dispatchEvent (core.es5.js:8614)
at core.es5.js:9228
at HTMLAnchorElement.<anonymous> (platform-browser.es5.js:2648)
at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
检查.component.ts
import {Component, OnInit, Input, ViewChild} from "@angular/core";
import {Headers, Http} from "@angular/http";
import {Router} from "@angular/router";
import {NgModel, FormGroup} from "@angular/forms";
sendMesage(inspection) {
this.inspectionService.sendMesage(inspection);
}
检查服务.ts
import {Headers, Http} from "@angular/http";
import {Observable} from 'rxjs/Observable';
import {Subject} from "rxjs";
import {of} from 'rxjs/observable/of';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Injectable} from "@angular/core";
import {Component, OnInit, ViewChild} from "@angular/core";
import {Router} from "@angular/router";
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
constructor(private http: Http,
private router: Router,
private alertService: AlertService) {
}
sendMesage(inspection: Inspection) {
this.alertService.confirm("Threshold reached for Rank " + inspection.rankName + ". Do you want send message ?",function(){
alert("1...");
const url = '/api/inspection/sendmessage/';
this.http
.post(url, JSON.stringify(inspection), {headers: this.headers})
.toPromise()
.then(response => {
let data = response.json();
return response.json() as Inspection;
})
.catch(error => {
alert("error");
});
},function(){
alert("No clicked");
})
}
应用程序模块.ts
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpModule} from '@angular/http';
import { HttpClientModule } from '@angular/common/http';
import {AppRoutingModule } from './app-routing.module';
import {InspectionService} from './inspection/inspection.service';
import {AlertService} from './alert/alert.service';
import {AppComponent} from './app.component';
import {AlertComponent} from './alert/alert.component';
import {NavigationComponent} from './navigation/navigation.component';
import { InspectionComponent } from './inspection/inspection.component';
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
FormsModule,
HttpModule,
AppRoutingModule
],
providers: [
AlertService,
InspectionService,
],
declarations: [
AppComponent,
AlertComponent,
NavigationComponent,
InspectionComponent,
],
bootstrap: [AppComponent]
})
export class AppModule {
}
alert.service.ts
import {Injectable} from '@angular/core';
import {Router, NavigationStart} from '@angular/router';
import {Observable} from 'rxjs';
import {Subject} from 'rxjs/Subject';
import { AlertComponent } from './alert.component';
@Injectable()
export class AlertService {
private subject = new Subject<any>();
private keepAfterNavigationChange = false;
constructor(private router: Router) {
// clear alert message on route change
router.events.subscribe(event => {
if (event instanceof NavigationStart) {
if (this.keepAfterNavigationChange) {
// only keep for a single location change
this.keepAfterNavigationChange = false;
} else {
// clear alert
this.subject.next();
}
}
});
}
success(message: string, keepAfterNavigationChange = false) {
this.keepAfterNavigationChange = keepAfterNavigationChange;
this.subject.next({type: 'success', text: message});
}
error(message: string, keepAfterNavigationChange = false) {
this.keepAfterNavigationChange = keepAfterNavigationChange;
this.subject.next({type: 'error', text: message});
}
getMessage(): Observable<any> {
return this.subject.asObservable();
}
confirm(message: string,siFn:()=>void,noFn:()=>void){
this.setConfirmation(message,siFn,noFn);
}
setConfirmation(message: string,siFn:()=>void,noFn:()=>void) {
let that = this;
this.subject.next({ type: "confirm",
text: message,
siFn:
function(){
that.subject.next(); //this will close the modal
siFn();
},
noFn:function(){
that.subject.next();
noFn();
}
});
}
}
最佳答案
使用javascript回调函数声明时
var that = this;
然后使用 that.http 作为您的 this.http,如下所示
inspection.service.ts
sendMesage(inspection: Inspection) {
var that = this;
this.alertService.confirm("Threshold reached for Rank " + inspection.rankName + ". Do you want send message ?",function(){
alert("1...");
const url = '/api/inspection/sendmessage/';
that.http
.post(url, JSON.stringify(inspection), {headers: that.headers})
.toPromise()
.then(response => {
let data = response.json();
return response.json() as Inspection;
})
.catch(error => {
alert("error");
});
},function(){
alert("No clicked");
})
}
关于 Angular 4 : Cannot read property 'http' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49882736/
我是一名优秀的程序员,十分优秀!