简体   繁体   中英

In Angular 8 /9, can I determine my URL inside of app.module.ts and use it when declaring modules?

I have a third party module in my app.module.ts that needs to know whether a user logs in with or without a "www" in their url to correctly redirect them after authentication. So, setup looks like:

app.module.ts

...
@NgModule({
    declarations: [
        AppComponent
    ],
    imports     : [
        ...
        ThirdPartyAuthModule.initAuth(environment.authConfig),
        ...
    ],
    bootstrap   : [
        AppComponent
    ]
})
export class AppModule
{
}

I want to get somehow get the url into that authConfig variable. Is this possible with Angular?

You could always use window.location.href

https://stackblitz.com/edit/angular-href-1

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { environment } from '../environments/environment';

const authConfig = environment.authConfig;
// Add param below to your auth config
const href = window.location.href;
const isHttp = href.includes('http://');
const isHttps = href.includes('https://');
const hasWWW = href.includes('www');;

@NgModule({
  imports:      [
    BrowserModule,
    FormsModule,
    // ThirdPartyAuthModule.initAuth(authConfig),
    ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM