无法创建新对象并在angular2中使用导出类

时间:2018-01-06 09:45:00

标签: angular typescript

下面是类UserDetails ::

export class UserDetails {
name:string;
phoneNumber:string;
email:string;
password: string;
 }

下面是使用userDetails ::

的服务
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { userDetails } from './_models/index';
import 'rxjs/add/operator/map'

@Injectable()
export class AuthenticationService {
private loginUserUrl="http://localhost:8082/TodoApp/login";

constructor(private http: HttpClient) {
//let user = new UserDetails();
}

login(email: string, password: string) {
    console.log("in login service");

    let user = new User();
   user.email=email;
    user.password=password;

    return this.http.post(this.loginUserUrl,user);
    .map(user => {
            // login successful if there's a jwt token in the 
             response
            if (user && user.token) {
                // store user details and jwt token in local storage to keep user logged in between page refreshes
                localStorage.setItem('currentUser', JSON.stringify(user));
            }

            return user;
        });
      }

}

error :::: eferenceError:未定义UserDetails     在AuthenticationService.login(webpack-internal:///../../../../../src/app/_services/authentication.service.ts:25)

3 个答案:

答案 0 :(得分:0)

试试这个:

import { UserDetails as User } from './_models/index';
//...
let user = new User();

答案 1 :(得分:0)

您的导入错误,您正在执行 userDetails 而不是 UserDetails ,将其更改为

import { UserDetails } from './_models/index';

并且对象应该创建为,

 let user = new UserDetails();

答案 2 :(得分:0)

您需要在构造函数中初始化模型变量UserDetails,如此

constructor(private http: HttpClient, private user:UserDetails) {

}

这将创建user类型的对象UserDetails,您可以像使用http一样使用它。像这样,

this.user.email=email;
this.user.password=password;