我是Angular 5的新手并尝试放置路由following Angular 5 tutorial。 我的差异很小:
当我在应用程序中导航(使用routerLinks)时,路由正常工作,但是当我刷新页面或直接在浏览器中写入URL时,我有404。
由于我无法配置虚拟目录的路由,如何配置Angular以正确管理路由?
我已经回答了问题,但没有找到一个好的解决方案:
的index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>XXX</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="dist/assets/css/XXX.css">
<link rel="stylesheet" href="dist/assets/vendor/css/bootstrap.min.css">
</head>
<body>
<!-- Application -->
<div class="container">
<app>Dev loading...</app>
</div>
<!-- Scripts -->
<script type="text/javascript" src="dist/runtime.js"></script>
<script type="text/javascript" src="dist/polyfills.js"></script>
<script type="text/javascript" src="dist/styles.js"></script>
<script type="text/javascript" src="dist/vendor.js"></script>
<script type="text/javascript" src="dist/main.js"></script>
<script type="text/javascript" src="dist/assets/vendor/js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="dist/assets/vendor/js/bootstrap.min.js"></script>
</body>
</html>
菜单组件(带有routerLinks):
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" routerLink="/">Dev menu</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Login</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="nav-link" routerLink="/login">Login</a>
<a class="nav-link" routerLink="/login/C0b0ll">Login as C0b0ll</a>
</div>
</li>
</ul>
</div>
</nav>
app.component.html(带路由器插座):
<devMenu></devMenu>
<router-outlet></router-outlet>
App.module.ts:
import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
// Application components.
import { AppComponent } from "./app.component";
import { AppRoutingModule } from "./app-routing.module";
import { DevMenuComponent } from "./devMenu/devMenu.component";
// Game components.
import { LoginComponent } from "./login/login.component";
@NgModule({
imports: [
BrowserModule,
AppRoutingModule
],
declarations: [
AppComponent,
DevMenuComponent,
LoginComponent
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
APP-routing.module.ts
import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
import { LoginComponent } from "./login/login.component";
const routes: Routes = [
{ path: "login", component: LoginComponent },
{ path: "login/:login", component: LoginComponent },
{ path: "", redirectTo: "/login", pathMatch: "full" }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
答案 0 :(得分:2)
根据您用于托管应用程序的内容,您需要设置URL重写规则。
Apache:
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
NGINX:
try_files $uri $uri/ /index.html;
IIS:
<system.webServer>
<rewrite>
<rules>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
火力地堡:
"rewrites": [ {
"source": "**",
"destination": "/index.html"
} ]