Gram z Angular2 i TypeScript i nie idzie dobrze (w AngularJS byłoby to łatwe). Piszę mały eksperyment aplikację, by uporać się z tym wszystkim i mam następujący komponent jako mój główny składnik/najwyższego poziomu ...Angular2 i TypeScript: error TS2322: Typ "Response" nie jest przypisywany do "UserStatus"
import {Component, OnInit} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {UserData} from './services/user-data/UserData';
import {Home} from './components/home/home';
import {UserStatus} from './types/types.ts';
import {Http, Headers, Response} from 'angular2/http';
@Component({
selector: 'app', // <app></app>
providers: [...FORM_PROVIDERS],
directives: [...ROUTER_DIRECTIVES],
template: require('./app.html')
})
@RouteConfig([
{path: '/', component: Home, name: 'Home'},
// more routes here....
])
export class App {
userStatus: UserStatus;
constructor(public http: Http) {
}
ngOnInit() {
// I want to obtain a user Profile as soon as the code is initialised
var headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.get('/restservice/userstatus', {headers: headers})
.subscribe(
(data: Response) => {
data = JSON.parse(data['_body']);
this.userStatus = data;
},
err => console.log(err), // error
() => console.log('getUserStatus Complete') // complete
);
}
}
Teraz, gdy składnik najwyższego poziomu bootstrapped/zainicjowane chcę aby wykonać połączenie do phoney usługi REST (/ restservice/userstatus) założyłem, że zwraca obiekt, który zrobiłem w rodzaju jak tak (to jest od import {UserStatus} from './types/types.ts'
):
export class UserStatus {
constructor (
public appOS?: any , // can be null
public firstName: string,
public formerName?: any, // can be null
public fullPersId: number,
public goldUser: boolean,
public hasProfileImage: boolean,
public hideMoblieNavigationAndFooter: boolean,
public persId: string,
public profileName: string,
public profilePicture: string,
public showAds: boolean,
public siteId: number,
public url: string,
public verified: boolean
) {
}
}
teraz appOS
i formerName
właściwości mogą potencjalnie być null
i podczas wyświetlania odpowiedzi w mojej usłudze REST są one, obiekt JSON wygląda tak:
{
appOS: null,
firstName: "Max",
formerName: null,
fullPersId: 123456789,
goldUser: true,
hasProfileImage: true,
hideMoblieNavigationAndFooter: false,
persId: "4RUDIETMD",
profileName: "Max Dietmountaindew",
profilePicture: "http://myurl.com/images/maxdietmountaindew.jpg",
showAds: true,
siteId: 1,
url: "/profile/maxdietmountaindew",
verified: true
}
więc struktura danych wysłane z mojego phoney usług i spotkania typu obiektu jednak gdy próbuję przypisać dane z Serwisu resztę składnika w klasie 'this.userStatus = data;'
I pojawia się następujący błąd ....
"error TS2322: Type 'Response' is not assignable to type 'UserStatus'.
Property 'appOS' is missing in type 'Response'."
zakładam w moim klasy Type robie coś nie tak z definicji gdzie null są zainteresowane ktoś może zobaczyć, co robię źle lub wyjaśnić, dlaczego ja dostaję błąd. Z góry dziękuję.
Chociaż to wydaje się działać, teraz otrzymuję następujący błąd "(5,16): błąd TS1016: Wymagany parametr nie może być zgodny z opcjonalnym parametrem." –
Czy możesz skopiować tutaj ten wiersz kodu? –
Odnosi się do "public firstName: string" w klasie UserStatus Zakładam, że –