BaseHrefService hinzugefügt, um das Basis-href vom Dokument-Basis-Element abzurufen.
This commit is contained in:
@@ -4,7 +4,18 @@ import { provideRouter } from '@angular/router';
|
|||||||
import { routes } from './app.routes';
|
import { routes } from './app.routes';
|
||||||
import { provideClientHydration } from '@angular/platform-browser';
|
import { provideClientHydration } from '@angular/platform-browser';
|
||||||
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
|
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
|
||||||
|
import { APP_BASE_HREF } from '@angular/common';
|
||||||
|
import { BaseHrefService } from './services/base-href.service';
|
||||||
|
|
||||||
export const appConfig: ApplicationConfig = {
|
export const appConfig: ApplicationConfig = {
|
||||||
providers: [provideRouter(routes), provideClientHydration(), provideAnimationsAsync()]
|
providers: [
|
||||||
|
provideRouter(routes),
|
||||||
|
provideClientHydration(),
|
||||||
|
provideAnimationsAsync(),
|
||||||
|
{
|
||||||
|
provide: APP_BASE_HREF,
|
||||||
|
useFactory: (baseHrefService: BaseHrefService) => baseHrefService.getBaseHref(),
|
||||||
|
deps: [BaseHrefService]
|
||||||
|
}
|
||||||
|
]
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,16 +2,8 @@
|
|||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { RouterModule, Routes } from '@angular/router';
|
import { RouterModule, Routes } from '@angular/router';
|
||||||
import { LoginComponent } from './login/login.component';
|
import { LoginComponent } from './login/login.component';
|
||||||
import { FooComponent } from './foo/foo.component';
|
|
||||||
import { authGuard } from './guards/auth.guard';
|
import { authGuard } from './guards/auth.guard';
|
||||||
|
|
||||||
export const routes: Routes = [
|
export const routes: Routes = [
|
||||||
{ path: 'login', component: LoginComponent },
|
{ path: 'login', component: LoginComponent }
|
||||||
{ path: 'foo', component: FooComponent }
|
|
||||||
];
|
];
|
||||||
|
|
||||||
@NgModule({
|
|
||||||
imports: [RouterModule.forRoot(routes)],
|
|
||||||
exports: [RouterModule]
|
|
||||||
})
|
|
||||||
export class AppRoutingModule {}
|
|
||||||
@@ -11,18 +11,18 @@ export class AuthService {
|
|||||||
|
|
||||||
constructor(private http: HttpClient) {
|
constructor(private http: HttpClient) {
|
||||||
const baseHref = inject(APP_BASE_HREF);
|
const baseHref = inject(APP_BASE_HREF);
|
||||||
this.baseUrl = `${baseHref}api/auth`;
|
this.baseUrl = `${baseHref}auth`;
|
||||||
}
|
}
|
||||||
|
|
||||||
login(credentials: { username: string; password: string }): Observable<any> {
|
login(credentials: { username: string; password: string }): Observable<any> {
|
||||||
return this.http.post(`${this.baseUrl}api/login`, credentials);
|
return this.http.post(`${this.baseUrl}login`, credentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
logout(): Observable<any> {
|
logout(): Observable<any> {
|
||||||
return this.http.post(`${this.baseUrl}api/logout`, {});
|
return this.http.post(`${this.baseUrl}logout`, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
isAuthenticated(): Observable<boolean> {
|
isAuthenticated(): Observable<boolean> {
|
||||||
return this.http.get<boolean>(`${this.baseUrl}api/check`);
|
return this.http.get<boolean>(`${this.baseUrl}check`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { BaseHrefService } from './base-href.service';
|
||||||
|
|
||||||
|
describe('BaseHrefService', () => {
|
||||||
|
let service: BaseHrefService;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({});
|
||||||
|
service = TestBed.inject(BaseHrefService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be created', () => {
|
||||||
|
expect(service).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
// base-href.service.ts
|
||||||
|
import { Injectable, Inject, inject } from '@angular/core';
|
||||||
|
import { DOCUMENT } from '@angular/common';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class BaseHrefService {
|
||||||
|
document: Document;
|
||||||
|
constructor() {
|
||||||
|
this.document = inject(DOCUMENT)
|
||||||
|
}
|
||||||
|
|
||||||
|
getBaseHref(): string {
|
||||||
|
const baseElement = this.document.querySelector('base');
|
||||||
|
return baseElement?.getAttribute('href') || '/';
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user