Member-only story

How to extract the base HREF in Angular?

Georgi Parlakov
ITNEXT

Using the location strategy one might get the base HREF passed in the ng build command.

It turns out there is the APP_BASE_HREF injection token provided out of the box with Angular but if you try and inject it:

You get an error, claiming no one provided it!

NullInjectorError: No provider for InjectionToken appBaseHref!

When we build our app we can specify the base HREF for it:

For example:ng build --base-href /admin will build our app and instruct it to use base HREF of /admin That could be useful if the administration part of the app as a separately deployed app under the same domain as our main app.

In order to use that in our app’s logic we can:

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Written by Georgi Parlakov

Angular and DotNet dev. RxJs explorer. Testing proponent. A dad. Educative.io course author. https://gparlakov.github.io/

Responses (1)

What are your thoughts?

"This class should not be used directly by an application developer. Instead, use Location."

Recommended from Medium

Lists

See more recommendations