Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Error: No base href set." after upgrade to Angular 8

Tags:

angular

After completing an update from Angular 5.x to 8.0.1, my application compiles but I get the following error at runtime (using ng serve):

Error: No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.

I've looked at other posts such as: Angular 2 router no base href set, however, my app already has the HTML base element in it. My index.html is as follows:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>MyApp</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<app-root></app-root>
</html>

I can solve the problem by adding:

{
  provide: APP_BASE_HREF,
  useValue: '/'
}

to my app.modules.ts, but I'm more interested in understanding why this is now needed, when a bare bones app created with ng new works without this provider.

like image 584
CodingGorilla Avatar asked Jun 18 '19 17:06

CodingGorilla


People also ask

What is BASE href in angular?

The <base href="/"> tells the Angular router what is the static part of the URL. The router then only modifies the remaining part of the URL. Without the base href tag you will see errors like this: Angular 2 Router no Base href set.

What is the use of base href in routing in index HTML page?

The href attribute specifies the base URL for all relative URLs on a page.

What is base element in angular?

<meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico"> </head> <body> <app-root></app-root>


1 Answers

Make sure that index.html is a valid HTML file by adding the body element, in which you can insert the Angular root component:

<html lang="en">
<head>
  <base href="/">
  ...
</head>
<body>
  <app-root></app-root>
</body>
</html>
like image 161
ConnorsFan Avatar answered Sep 20 '22 17:09

ConnorsFan