我在刷新浏览器时遇到问题,显示404找不到错误。
我试过添加
在index.html但它没用。
并且
LocationStrategy,HashLocationStrategy
但似乎行不通。
我正在使用带有4.5 .net框架的VS2015
这是我的app.routes.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { JobBookingComponent } from './component/jobbooking';
import { JobDetailsComponent } from './component/jobbookingdetailscomponent';
import { JPTemplateComponent } from './component/jptemplate.component';
const routes: Routes = [
{ path: '', redirectTo: '/templatejp', pathMatch: 'full' },
{ path: 'template', component: JobBookingComponent },
{ path: 'templatejp', component: JPTemplateComponent },
{ path: 'detail/:templateid', component: JobDetailsComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
的index.html
<html>
<head>
<script>document.write('<base href="/" />');</script>
<title>Demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" type="text/css" href="node_modules/primeng/resources/themes/omega/theme.css" />
<link rel="stylesheet" type="text/css" href="node_modules/primeng/resources/primeng.min.css" />
<!--<link rel="stylesheet" type="text/css" href="app/resources/icons/css/font-awesome.min.css" />-->
<!--<script src="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"></script>-->
<link href="font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<!-- 1. Load libraries -->
<!-- Polyfill for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<link rel="stylesheet"
href="https://unpkg.com/bootstrap@3.3.7/dist/css/bootstrap.min.css">
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
app.ts
import './rxjs-operator';
import { Component, ViewContainerRef } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<nav>
<a routerLink="/template" routerLinkActive= "active" > Template </a>
<a routerLink="/templatejp" routerLinkActive= "active" > Templatejp </a>
</nav>
<router-outlet></router-outlet>`
})
export class AppComponent {
}
请帮助解决问题 感谢。
答案 0 :(得分:2)
好的问题是我导入的PathLocationStrategy需要使用HashLocationStrategy 更新(app.module.ts) 通过导入HashLocationStrategy和locationstrategy并添加提供程序。
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HashLocationStrategy, LocationStrategy } from '@angular/common';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule],
providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}],
bootstrap: [AppComponent],
})
export class AppModule {}
答案 1 :(得分:1)
在索引页面上添加<base href="/" />
,而不是从脚本
答案 2 :(得分:0)
因此,404错误意味着您的Web服务器(在本例中是您在构建应用程序时运行的开发服务器)无法找到该页面。您的应用程序是否构建没有任何错运行控制台时,应显示所请求页面的日志以及是否找到它们(http状态200)。
答案 3 :(得分:0)
问题正在发生,因为无法找到路线,请使用来自&#39; @ angular / core&#39 ;;的导入{ModuleWithProviders};以下是路由导出的示例代码:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { JobBookingComponent } from './component/jobbooking';
import { JobDetailsComponent } from './component/jobbookingdetailscomponent';
import { JPTemplateComponent } from './component/jptemplate.component';
const routes: Routes = [
{ path: '', redirectTo: 'templatejp', pathMatch: 'full' },
{ path: 'template', component: JobBookingComponent },
{ path: 'templatejp', component: JPTemplateComponent },
{ path: 'detail/:templateid', component: JobDetailsComponent }
];
@NgModule({
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes, { useHash: true });
})
export class AppRoutingModule { }