[英]How to pass USER ID from template to function to get values in angular 5
[英]how to get values from model function and pass the value on url in angular 6
Login.model.ts:
export class Login{
access_token : string;
token_type : string;
expires_in : string;
refresh_token : string;
FirstName : string;
LastName : string;
Email : string;
AccountNo : string;
client_Id : string;
}
这是登录模型。用户登录后,我要使用AccountNo
Checkout.service.ts:
Login1 : Login[];
async getAddress() {
const address = await this.httpClient.get<Customer[]>('http://localhost:49422/api/customer/' +'{{login1.AccountNo}}' +'/profile/', { withCredentials: true })
.toPromise();
return address;
}
我想使用该URL中没有从登录模型获得的帐户来获取相应的客户详细信息。登录和结帐是完全不同的组件。如何在角度6中实现
您需要创建一个新服务以在组件和服务之间共享资源,这是通过称为dependency injection
的属性完成的。 您可以在此处了解更多信息。
步骤1:创建一个非常基本的loginDetail服务,并导入您的Login
模型,并创建一个非常基本的setData和getData函数来接受和返回登录数据。
import { Injectable } from '@angular/core';
import { Login } from './path/to/login.model.ts';
@Injectable({
providedIn: 'root'
})
export class LoginDetailService {
loginData: Login;
setData: void(data: Login) {
this.loginData = data;
}
getData: Login() {
if (this.loginData) return this.loginData;
else return {
error: 'no login data present'
}
}
constructor() { }
}
第2步:将此服务导入您将获得所有令牌和登录数据的位置。 导入登录模型和此服务。
例如,您正在使用登录服务来执行所需的工作
import { LoginDetailService } from './path/to/logindetail.service.ts';
import { Login } from './path/to/login.model.ts';
@Injectable({
providedIn: 'root'
})
export class LoginService {
loginData: Login;
login(email, pass): Login {
//perform login functions here and then i assume you will get the login
//data and store it in some loginData variable which u need to
//save in LoginDetailService
this.loginDetailService.setData(loginData);
}
constructor(private loginDetailService: LoginDetailService) { }
}
现在,您已成功将数据保存在loginDetailService中,可以从导入并调用函数getData
任何组件或服务中检索该数据。
步骤3:将此服务导入Checkout.service.ts
并调用函数getData
//assuming that u have imported the service and declared it in the constructor
Login1 : Login = this.loginDetailService.getData();
async getAddress() {
const address = await this.httpClient.get<Customer[]>('http://localhost:49422/api/customer/' +Login1.AccountNo+'/profile/', { withCredentials: true })
.toPromise();
return address;
}
希望这可以帮助。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.