应用程序部署在子目录中时设置角度路由

时间:2018-02-22 19:56:08

标签: angular

我有一个Angular应用程序,我正在我的网络服务器上的foobar.com/myapp目录中部署。

我使用的构建命令是ng build -bh /myapp,以确保我的index.html包含

<base href="myapp">

线。所有资产和所需文件加载正常。在主页面上,我有一个路由器插座,我的路由设置如下:

const appRoutes: Routes = [
  { path: 'configure', component: ConfigureComponent },
  { path: 'dashboard',      component: DashboardComponent },
  { path: 'myapp',
    redirectTo: '/configure',
    pathMatch: 'full'
  },
  { path: '',
    redirectTo: '/configure',
    pathMatch: 'full'
  }
];

我遇到的问题是,如果我直接打开网址http://foobar.com/myapp,我会按预期登陆ConfigureComponent,但网址会被重写为http://foobar.com/myapp/myapp/configure。在ConfigureComponent上,我有一个链接:

<a routerLink="/dashboard" class="btn btn-primary">Save and go to Dashboard</a>

当我点击它时,我需要DashboardComponent罚款,但网址又是http://foobar.com/myapp/myapp/dashboard

我尝试使用不同的路由配置,但没有设法将其设置为消除URL中的重复。我不太喜欢这里的应用程序必须在其中硬编码myapp子目录名称,尽管我想让它完全不知道我部署它的子文件夹。 (除此之外,我必须在ng build -bh ...中指定它。

我在这里做错了什么..?

1 个答案:

答案 0 :(得分:3)

Base应该有一个traling斜线。试试ng build -bh /myapp/