2016-12-14 69 views
1

你好,我想知道是否有可能創建一個類,在那裏你實現一個接口,並從那裏你發送從.get服務獲得的數據來創建一個新的對象。事情是這樣的Angular 2:從類創建對象

import { Component, OnInit } from '@angular/core'; 
import { User} from '../interfaces/user'; 
import {UserService} from '../services/user.service'; 
import { UserClass } from '../classes/user-class' 

@Component({ 
    selector: 'up-pros', 
    templateUrl: './pros.component.html', 
    providers: [UserService] 
}) 
export class ProsComponent implements OnInit { 
    public users :User[]; 
    public term: string; 
    constructor(private _httpService: UserService) { } 

    ngOnInit() { 
    console.log(UserClass) 
    this.term= 'INSTRUCTOR'; 
    this._httpService.searchUsers(this.term) 
     .subscribe(
      data => {this.users = new UserClass(data), console.log(data)}, 
      error => alert(error + ' Error Get') 
     ); 
    } 
} 

在我的UserClass的代碼是一樣的東西下一個

import { User } from '../interfaces/user'; 
import { Address } from "../interfaces/address"; 

export class UserClass implements User { 

public id:   number 
public name:   string 
public password:  string 
public lastNameA: string 
public lastNameB: string 
public photo:  string 
public telephone: string 
public email:  string 
public userType:  string 
public active:  string 
public score:  number 
public createdAt: string 
public updatedAt: string 
public Address:  Address 

constructor (id:   number, 
       password:  string, 
       name:   string, 
       lastNameA: string, 
       lastNameB: string, 
       photo:  string, 
       telephone: string, 
       email:  string, 
       userType:  string, 
       active:  string, 
       score:  number, 
       createdAt: string, 
       updatedAt: string, 
       Address:  Address) { 
       this.name  = name 
       this.password = password 
       this.lastNameA = lastNameA 
       this.lastNameB = lastNameB 
       this.photo  = photo 
       this.telephone = telephone 
       this.email  = email 
       this.userType = userType 
       this.active  = active 
       this.score  = score 
       this.createdAt = createdAt 
       this.updatedAt = updatedAt 
       this.Address = Address 
       } 

} 

,並通過最後,接口:

import { Address } from "./address" 

export interface User { 
    name:  string; 
    password: string; 
    lastNameA: string; 
    lastNameB: string; 
    photo:  string; 
    telephone: string; 
    email:  string; 
    userType: string; 
    active:  string; 
    score:  number; 
    createdAt: string; 
    updatedAt: string; 
    Address: Address; 
} 

這可能嗎?因爲如果我嘗試這樣做即時得到的利弊,component.ts下一個錯誤:

Supplied parameters do not match any signature of call target. 
[default] Checking finished with 1 errors 

我的服務:

import {Injectable} from '@angular/core'; 
import {Http, Headers} from '@angular/http'; 
import 'rxjs/add/operator/map'; 
import { User } from '../interfaces/user'; 


@Injectable() 
export class UserService { 
    url= 'http://localhostapi/users'; 

    constructor(private _http: Http){} 


    getUsers(){ 
    return this._http.get(this.url) 
     .map(res => res.json()); 
    } 

    searchUsers(term : string){ 
    return this._http.get('http://localhostapi/listas?user='+term) 
     .map(res => res.json()); 

    } 
    searchUser(term : string){ 
    return this._http.get('http://localhostapi/users/'+term) 
     .map(res => res.json()); 

    } 

    postUsers(user: User){ 

    var headers = new Headers(); 
    headers.append('Content-Type','application/json'); 
    return this._http.post(this.url, user, {headers: headers}) 
    .map(res => res.json()); 
    } 

    updateUsers(user: User, term: string){ 

    var headers = new Headers(); 
    headers.append('Content-Type','application/json'); 
    return this._http.put(this.url+"/"+term, user, {headers: headers}) 
    .map(res => res.json()); 
    } 
} 
+0

不要寫一個構造函數與十幾個參數。 – 2016-12-14 16:44:56

+0

因此,我不知道我在做什麼或只是在構造函數中打了十幾個參數? –

+0

這可能有所幫助:http://stackoverflow.com/questions/37520578/javascript-constructor-use-an-object/37520993#37520993 – 2016-12-14 17:03:51

回答

2

如果data結構UserClass匹配列表,你可以簡單地做

this._httpService.searchUsers(this.term) 
     .subscribe(
      data => { 
         this.users = data as User[]; 
         console.log(data) 
        }, 
      error => alert(error + ' Error Get') 
     ); 
+0

謝謝。讓我嘗試一下。 –

+0

非常感謝,它工作。你能解釋爲什麼我不能像傳統模式那樣創建一個類的實例嗎? –

+0

當然,我們可以創建像「傳統模式」的實例,但在這種特殊情況下,UserClass中沒有任何構造函數,它將'User'的列表作爲參數,'data'具有' User'。 ** TypeScript **是一個結構類型系統。 http://www.typescriptlang.org/docs/handbook/classes.html – TheKojuEffect