首页 > 解决方案 > 在 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;
 }

标签: javascriptangulartypescriptcookiesangular-social-login

解决方案


推荐阅读