Javascript 使用路由器时角度加载url

Javascript 使用路由器时角度加载url,javascript,angular,angular-routing,angular-router,Javascript,Angular,Angular Routing,Angular Router,即时通讯使用角7+路由器。例如,我的主页是localhost:4200,我的路由器的url之一是localhost:4200/route,我的API端点是localhost:4200/API/foo 我试图让浏览器从两个位置加载api端点。如果我放置一个href指向API端点的锚点,两个锚点链接都可以完美工作。但是,我需要以编程的方式进行,并且我已经尝试了以下所有方法 window.open("localhost:4200/api/foo","_self") window.location.re

即时通讯使用角7+路由器。例如,我的主页是
localhost:4200
,我的路由器的url之一是
localhost:4200/route
,我的API端点是
localhost:4200/API/foo

我试图让浏览器从两个位置加载api端点。如果我放置一个href指向API端点的锚点,两个锚点链接都可以完美工作。但是,我需要以编程的方式进行,并且我已经尝试了以下所有方法

window.open("localhost:4200/api/foo","_self")
window.location.replace('localhost:4200/api/foo');
window.location.href = 'localhost:4200/api/foo';
它们都在主页上工作,但如果我在路由器页面,这样做会让我进入主页

非常感谢您的帮助

具体来说,我有一个spring引导服务器,它的url模式类似于/api/*。所有其他URL都由angular处理。我希望浏览器加载
localhost:4200/api/foo
,这由服务器中定义的get请求直接处理

演示:

我的导航条是一个组件,无论路由器如何,它都保持不变。 点击按钮背后的代码如下。请注意,我第一次在某个url下单击它时,它会加载主页,而不是
google.com

onLogIn() {
    window.open('https://www.google.com',"_self");
}
Routing.ts文件

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { IndexComponent } from "./index/index.component";
import { MicroserviceComponent } from "./microservice/microservice.component";

const routes: Routes = [
    { path: '', component: IndexComponent},
    { path: 'microservice/:id', component: MicroserviceComponent}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

将pathMatch添加到您的空路由,它将丢失,这就是您重定向到主组件的原因

const routes: Routes = [
 { path: '', component: IndexComponent, pathMatch:'full'},
 { path: 'microservice/:id', component: MicroserviceComponent}
];

你能把你的问题扩大一下吗?我不太明白。您想使用angular router导航到
/api/foo
?这是服务器端问题。如果要在同一个域上混合HTML和API路由,则需要了解Angular Universal以及如何为API添加自定义Express路由。@JamieRees更新了我的question@cgTag这看起来不像是服务器端问题。我在服务器上放了一个断点,但它没有命中,因为它加载了主页面。你能播下你的routing.ts文件吗