How can I get the complete current URL in angular 7 without using window.location.href
?
For example, let's say my current URL is http://localhost:8080/p/drinks?q=cold&price=200
How can I get the complete URL rather than only /p/drinks?q=cold&price=200
?
I have tried with this.router.url
this gives only /p/drinks?q=cold&price=200
not with the complete hostname.
The reason that I do not want to use window.location.href
is that it causes an issue with rendering in ng-toolkit/universal
I tried to follow this solution which seems to be a same problem as me and also updated
How to get domain name for service in Angular2
And here is my code
windowProvider.js
import { InjectionToken, FactoryProvider } from '@angular/core';
export const WINDOW = new InjectionToken<Window>('window');
const windowProvider: FactoryProvider = {
provide: WINDOW,
useFactory: () => window
};
export const WINDOW_PROVIDERS = [
windowProvider
];
In app.module.ts
@NgModule({
.......
providers:[
.......
windowProvider
]
})
And in my required page :
import {WINDOW as WindowFactory} from '../../factory/windowProvider';
......
constructor(private router: Router, private helper: Helpers,
@Inject(WindowFactory) private windowFactory: any,
) {
console.warn('HELLO I M WINDOW FACTORY', this.windowFactory.location.hostname);
}
Buy this gives error at the time of compilation
ERROR in ./src/app/factory/windowProvider.js 5:20 Module parse failed: Unexpected token (5:20) You may need an appropriate loader to handle this file type. | export const WINDOW = new InjectionToken('window'); |
const windowProvider: FactoryProvider = { | provide: WINDOW, | useFactory: () => window ℹ 「wdm」: Failed to compile.
Can anyone please tell how I can get this resolved. Thanks,
More links that I followed: