【发布时间】:2022-05-07 20:43:44
【问题描述】:
我是 Angular 2 的新手,对于发布的类似问题没有找到任何合适的答案。
我在 angular-cli 中的“void”类型上不存在属性“subscribe”。我尝试从 rxjs 导入订阅,但没有找到该库。
请帮助解决这个问题。请在下面找到我正在调用该服务的服务和组件代码。
// *******************************
// login.service.ts
// *******************************
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/throw';
@Injectable()
export class LoginService {
constructor(private http: Http) { }
getLoginData(){
this.http.get('./json/login.json')
.map((result: Response) => result.json())
.catch(this.getError);
}
private getError(error: Response): Observable<any>{
console.log(error);
return Observable.throw(error.json() || 'Server Issue');
}
}
// *******************************
// login.component.ts
// *******************************
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { LoginService } from '../login.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css'],
providers: [LoginService]
})
export class LoginComponent {
usernameModel: string;
passwordModel: string;
validLogin: Boolean;
constructor(private router: Router, private loginService: LoginService) { }
homeNav(){
if(this.usernameModel === 'jay' && this.passwordModel === 'Jay'){
this.validLogin = true;
this.router.navigate(['/home']);
}
else{
this.validLogin = false;
console.log('Login Error');
}
this.loginService.getLoginData()
.subscribe(
data => console.log(data)
);
}
}
【问题讨论】:
标签: angular