user4424299
user4424299

Reputation:

Change Angular2 main url

I'm trying to run an Angular 2 application from a website where the base url should be http://www.example.com/app instead of http://www.example.com. I've tried several guides to change this by setting the href of the base element (such as this one: Angular 2 router no base href set), but none of these works. I can still only visit the pages on the root-url (thus http://www.example.com/something instead of http://www.example.com/app/something). This is how the index.html-file looks like at the moment:

<!doctype html>
<html>
  <head>
    <base href="/app">
    <title>Test</title>

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

    <!-- Bootstrap stylesheet -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

    <!-- Bootstrap (and jQuery) scripts -->
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>

    <!-- Font Awesome icons! -->
    <script src="https://use.fontawesome.com/9fbdc2c47e.js"></script>
  </head>
  <body>
    <app-root>
      <div class="centered-container">
        <!-- Show animated loading icon -->
        <div id="loading-notification">
          <i class="fa fa-refresh fa-spin fa-5x fa-fw"></i>
        </div>
      </div>
    </app-root>
  </body>
</html>

What have I done wrong? An example of how my paths in the router look like:

RouterModule.forRoot([
      {
        path: "language",
        component: LanguageComponent
      },
      {
        path: "404",
        component: NotFoundComponent
      },
      {
        path: "**",
        component: NotFoundComponent
      }
    ]),

UPDATE:

I eventually fixed it! When building the application for production, the following command should be used: ng build --target=production --base-href /app/ --aot true. That fixes everything :)

Upvotes: 0

Views: 804

Answers (2)

skid
skid

Reputation: 978

you can make the following change under your routing

path: '', redirectTo: '/app', pathMatch: 'full'
path: 'app', component:'componentName'

which will eventually redirect you to the /app everytime when some error url is being given

Upvotes: 1

null canvas
null canvas

Reputation: 10613

Have you tried this

<base href="/app/">

and in your component

@Component({ moduleId: "app", selector:"my-component", ... })

Upvotes: 0

Related Questions