5

I have tried three approaches.

1st : index.html

<base href="/customer">

2nd : app.module.ts

@NgModule({
  providers: [{provide: APP_BASE_HREF, useValue: '/customer'}]
})

3rd : app-routing.module.ts

const routes: Routes = [
  { path: "", redirectTo: "/customer", pathMatch: "full" },
  {
    path: "customer",
    component: CustomerComponent,
    data: { animation: "HomePage" }
  }
];

All the above approaches work well for URL routing and I get desired URL.

http://localhost:4200/customer

However, static files(js & images) are still loading with the base path 'http://localhost:4200/'. I need it to have like http://localhost:4200/customer/main.js.

Hence, I am trying to make it http://localhost:4200/customer/main.js instead of http://localhost:4200/main.js for some secure validation reason.

Same can be seen in below screenshot.

enter image description here

3 Answers 3

11

You can use the --baseHref command line flag with ng serve and ng build this means that you no longer have to prefix the routes in app-routing.module.ts

ng serve --baseHref=/customer/

build with

ng build --baseHref=/customer/
Sign up to request clarification or add additional context in comments.

4 Comments

The application failed to load due to "localhost:4200/customer/runtime.js". It is not routing but it sets the relative path.
ng serve --baseHref=/customer/
Works for me when setting up a "Your First App" angular 7 project then used ng serve --baseHref=/customer/. The url localhost:4200/customer works without any error
It only works for me when I write the flag in the following way: ng build --base-href=/customer/
1

You can use --deploy-url parameter in ng build command.

Example: ng build --deploy-url /my/assets

More detail on https://angular.io/guide/deployment.

2 Comments

The tooling says that this approach is now deprecated.
@Gleno no, it does not says this
0

I got exactly same bug when I migrate from angular 5 to 7. It is probably related to the way that angular attaching the build js files. If you already added <base href="/"> into index.html but the scripts are still loading from the relative path, it might because there is no <body> tag. Try add <body> tag into index.html file and see if it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.