更改Angular2主网址

时间:2017-03-14 17:33:00

标签: javascript html angular routing

我试图从一个基本网址应为http://www.example.com/app而不是http://www.example.com的网站上运行Angular 2应用程序。我已经尝试了几个指南,通过设置基本元素的href(例如这个:Angular 2 router no base href set)来改变它,但这些都不起作用。我仍然只能访问root-url上的页面(因此http://www.example.com/something而不是http://www.example.com/app/something)。这就是index.html文件目前的样子:

<!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>

我做错了什么?路由器中路径的示例如下:

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

更新:

我最终解决了它!构建生产应用程序时,应使用以下命令:ng build --target=production --base-href /app/ --aot true。这解决了一切:)

2 个答案:

答案 0 :(得分:1)

您可以在路由

下进行以下更改
path: '', redirectTo: '/app', pathMatch: 'full'
path: 'app', component:'componentName'

每次发出错误网址时,最终都会将您重定向到/ app

答案 1 :(得分:0)

你试过这个吗?

<base href="/app/">

并在您的组件中

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