获取body - 角度 4



>我正在实现一个登录方法。当用户登录时,后端会生成一个 JWT,并将令牌发送到 HTML 的本地存储中。我从未使用过TS/Angular 4,在AngularJS中我没有问题,看到我的功能,告诉我它不起作用?

login(){
this.auth.authenticate(this.creds)
.subscribe(response => {
console.log("response: ", response.body.token)
this.auth.successfulLogin(response.headers.get('Authorization'))
this.navCtrl.setRoot('DetalhesViagemPage')
},
error => {})
}

console.log("response: ", response.body)返回

response:  {"nome":"Frederico","_id":"5b21a4332a5e3333cc64a12f","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyIkX18iOnsic3RyaWN0TW9kZSI6dHJ1ZSwic2VsZWN0"}

我修改了我的问题,使其更清楚。

如果你想获取 name 属性,你只需要像这样访问它:

response.body.name

只需像获取 body 属性一样使用属性。

最新更新