如果 index.html 包含查询参数,则无法匹配任何路由



当我打开 localhost:4000/index.html 时,我被重定向到HomeComponent,这是正确的行为。

但是通过添加像localhost:4000/index.html?foo=bar这样的查询参数,我得到了以下错误:

错误:无法匹配任何路由。网址细分:"索引.html">

打开本地主机:4000/?foo=酒吧工作很好。

app-routing.module.ts

const routes: Routes = [
{
data: {
title: 'Home'
},
path: '',
pathMatch: 'full',
component: HomeComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: []
})
export class AppRoutingModule {
}

索引.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Angular</title>
<base href="/">
<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>
</body>
</html>

我已经阅读了@LLai关于这个问题的答案,但是有没有办法在索引上添加查询参数.html?

查询参数究竟如何影响 Angular 上的路由行为,因此无法再找到索引.html?

谢谢

我可以在没有索引的情况下运行它.html,但我想了解为什么如果我将查询参数添加到索引中,路由不再起作用.html

然后,您可能希望深入研究angular-cli代码(以查看应用程序的托管方式(和路由模块(以查看路由的解析方式(。

我自己从未真正这样做过,但我有根据的猜测是index.html是一种特例,总是作为基本 href 解决。如果您使用除index.html以外的任何路径(例如添加参数(,它将用于解析您在应用程序中声明的路由。

请注意,在angular.json文件中声明为资产的文件将优先于路由,因此将提供该文件,而不是使用子路径作为路由路径。

如上所述,这是一个有根据的猜测 - 如果您怀疑,首先深入研究代码将是你最好的选择。

最新更新