javascript - 在 app.module.ts 中获取 Angular 应用程序的动态值
问题描述
我angularx-social-login
在我的应用程序中使用该包进行社交登录。根据文档,我必须在app.module.ts
. 但这就是我的问题开始的地方,因为我需要保持动态并从 cookie 中获取 id,而不是将客户端 id 作为静态字符串传递。为了使其动态化,我尝试制作服务并导出功能,app.module
但似乎没有任何效果。在创建导出函数时,我无法在其中传递 cookie,因为没有构造函数来初始化 cookie,并且当通过服务传递函数时,我不能在 app.module 中专门调用它,因为我需要首先创建服务的实例调用服务方法。已经坚持了好几天了。任何想法都会受到赞赏。
app.module.ts
let config = new AuthServiceConfig([
{
id: GoogleLoginProvider.PROVIDER_ID,
provider: new GoogleLoginProvider("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") //need to make this dynamic
},
{
id: FacebookLoginProvider.PROVIDER_ID,
provider: new FacebookLoginProvider("xxxxxxxxxx") //need to make this dynamic
}
]);
export function provideConfig() {
return config;
}
获取动态数据的服务
import { Injectable,Inject} from '@angular/core';
import { SocialLoginModule, AuthServiceConfig } from "angularx-social-login";
import { GoogleLoginProvider, FacebookLoginProvider } from "angularx-social-login";
import { CookieService } from 'ngx-cookie';
@Injectable({ providedIn: 'root' })
export class AppSocialLoginService {
public google;
public facebook;
public tempCookie;
constructor(private _cookiesService:CookieService) {
this.tempCookie = this._cookiesService.getObject('globalData');
}
getGoogle() {
if (this.tempCookie && this.tempCookie.hasOwnProperty('google_login_client_id')) {
this.google = this.tempCookie['google_login_client_id'];
return this.google;
}
}
getFacebook() {
if (this.tempCookie && this.tempCookie.hasOwnProperty('fb_login_id')) {
this.facebook = this.tempCookie['fb_login_id'];
return this.facebook;
}
}
provideConfig() {
let config = new AuthServiceConfig([
{
id: GoogleLoginProvider.PROVIDER_ID,
provider: new GoogleLoginProvider(this.getGoogle())
},
{
id: FacebookLoginProvider.PROVIDER_ID,
provider: new FacebookLoginProvider(this.getFacebook())
}
]);
return config;
} // cannot export this function in app.module since it's already in a class
}
试图创建一个函数并将其导出到 app.module 但问题是我无法初始化构造函数并让 cookie 在函数内部传递
export function provideConfig() {
let config = new AuthServiceConfig([
{
id: GoogleLoginProvider.PROVIDER_ID,
provider: new GoogleLoginProvider("xxxxxxxxxxxxxxxxxxxxxxxxxx") //can't receive cookies in this
},
{
id: FacebookLoginProvider.PROVIDER_ID,
provider: new FacebookLoginProvider("xxxxxxxxxxxxxxxxxxx") //can't receive cookies in this
}
]);
return config;
}
解决方案
推荐阅读
- java - Spring Boot中的一个类的多个配置树
- python - 如何将 dict 中的列表项与 Python 中的另一个列表相乘
- sql - 一个SQL问题,一个订单有多个产品
- firebase - Firebase 中的 IVR 支持
- r - ggplot polar_coord 更好地分隔文本
- java - 如何使用 Vaadin 实现 OAuth2 - Facebook
- javascript - 从 JS 中的现有数组中创建一个具有相应总数的对象数组
- git - Git Reset master / 将代码从分支复制到 master
- reactjs - 反应钩子没有设置状态
- sql - 在不丢失任何信息的情况下连接表