我正在尝试实施Hashlocation策略,但它不起作用请指导我。
这是My Angular&路由器版本
的package.json
"@angular/common": "2.0.0",
"@angular/compiler": "2.0.0",
"@angular/core": "2.0.0",
"@angular/forms": "2.0.0",
"@angular/http": "2.0.0",
"@angular/platform-browser": "2.0.0",
"@angular/platform-browser-dynamic": "2.0.0",
"@angular/router": "3.0.0",
"@angular/upgrade": "2.0.0",
"bootstrap": "^3.3.6",
Main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module'; platformBrowserDynamic().bootstrapModule(AppModule);
app.routes.ts
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { UserListComponent } from './users/user-list.component';
import { ScheduleListComponent } from './schedules/schedule-list.component';
import { ScheduleEditComponent } from './schedules/schedule-edit.component';
const appRoutes: Routes = [
{ path: 'users', component: UserListComponent },
{ path: 'schedules', component: ScheduleListComponent },
{ path: 'schedules/:id/edit', component: ScheduleEditComponent },
{ path: '', component: HomeComponent }
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes, { useHash: true });
app.modules.ts
import './rxjs-operators';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { LocationStrategy,HashLocationStrategy } from '@angular/common';
import { PaginationModule } from 'ng2-bootstrap/ng2-bootstrap';
import { Routes, RouterModule } from '@angular/router';
import { DatepickerModule } from 'ng2-bootstrap/ng2-bootstrap';
import { Ng2BootstrapModule } from 'ng2-bootstrap/ng2-bootstrap';
import { ModalModule } from 'ng2-bootstrap/ng2-bootstrap';
import { ProgressbarModule } from 'ng2-bootstrap/ng2-bootstrap';
import { SlimLoadingBarService, SlimLoadingBarComponent } from 'ng2-slim-loading-bar';
import { TimepickerModule } from 'ng2-bootstrap/ng2-bootstrap';
import { AppComponent } from './app.component';
import { DateFormatPipe } from './shared/pipes/date-format.pipe';
import { HighlightDirective } from './shared/directives/highlight.directive';
import { HomeComponent } from './home/home.component';
import { MobileHideDirective } from './shared/directives/mobile-hide.directive';
import { ScheduleEditComponent } from './schedules/schedule-edit.component';
import { ScheduleListComponent } from './schedules/schedule-list.component';
import { UserCardComponent } from './users/user-card.component';
import { UserListComponent } from './users/user-list.component';
import { routing } from './app.routes';
import { DataService } from './shared/services/data.service';
import { ConfigService } from './shared/utils/config.service';
import { ItemsService } from './shared/utils/items.service';
import { MappingService } from './shared/utils/mapping.service';
import { NotificationService } from './shared/utils/notification.service';
import { enableProdMode } from '@angular/core';
enableProdMode();
@NgModule({
imports: [
BrowserModule,
DatepickerModule,
FormsModule,
HttpModule,
Ng2BootstrapModule,
ModalModule,
ProgressbarModule,
PaginationModule,
routing,
TimepickerModule
],
declarations: [
AppComponent,
DateFormatPipe,
HighlightDirective,
HomeComponent,
MobileHideDirective,
ScheduleEditComponent,
ScheduleListComponent,
SlimLoadingBarComponent,
UserCardComponent,
UserListComponent
],
providers: [
ConfigService,
DataService,
ItemsService,
MappingService,
NotificationService,
SlimLoadingBarService,
{ provide: LocationStrategy, useClass: HashLocationStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts
import { Component, OnInit, ViewContainerRef } from '@angular/core';
// Add the RxJS Observable operators we need in this app.
import './rxjs-operators';
@Component({
selector: 'scheduler',
templateUrl: 'app/app.component.html'
})
export class AppComponent {
constructor(private viewContainerRef: ViewContainerRef) {
// You need this small hack in order to catch application root view container ref
this.viewContainerRef = viewContainerRef;
}
}
的index.html
<!DOCTYPE html>
<html>
<head>
<base href="/">
<meta charset="utf-8" />
<title>Scheduler</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<link href="bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
<link href="bower_components/alertify.js/themes/alertify.core.css" rel="stylesheet" />
<link href="bower_components/alertify.js/themes/alertify.bootstrap.css" rel="stylesheet" />
<link rel="stylesheet" href="../assets/css/styles.css" />
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="bower_components/alertify.js/lib/alertify.min.js"></script>
<!-- 1. Load libraries -->
<!-- Polyfill(s) 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>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<body>
<scheduler>
<div class="loader"></div>
</scheduler>
</body>
</html>
app.component.html
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<ng2-slim-loading-bar></ng2-slim-loading-bar>
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" [routerLink]="['/']">
<i class="fa fa-home fa-3x" aria-hidden="true"></i>
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="menuitem" *ngFor="let menuItem of MenuItemss" [value]="menuItem">
<a href="{{menuItem.module}}" data-toggle="dropdown" *ngIf="!menuItem.menus">{{menuItem.module}}</a>
<a href="{{menuItem.module}}" data-toggle="dropdown" *ngIf="menuItem.menus">{{menuItem.module}} <span class="caret"></span></a>
<ul class="dropdown-menu" *ngIf="menuItem.menus">
<li *ngFor="let submenu of menuItem.menus" [value]="submenu">
<a href="{{submenu.route}}">{{submenu.description}}</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav">
<li>
<a [routerLink]="['/schedules']"><i class="fa fa-calendar fa-3x" aria-hidden="true"></i></a>
</li>
<li>
<a [routerLink]="['/customers']"><i class="fa fa-calendar fa-3x" aria-hidden="true"></i></a>
</li>
<li>
<a [routerLink]="['/customermaintenance']"><i class="fa fa-calendar fa-3x" aria-hidden="true"></i></a>
</li>
<li>
<a [routerLink]="['/users']"><i class="fa fa-users fa-3x" aria-hidden="true"></i></a>
</li>
<li>
<a href="" target="_blank"><i class="fa fa-info fa-3x" aria-hidden="true"></i></a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="" target="_blank">
<i class="fa fa-facebook fa-3x" aria-hidden="true"></i>
</a>
</li>
<li>
<a href="" target="_blank">
<i class="fa fa-twitter fa-3x" aria-hidden="true"></i>
</a>
</li>
<li>
<a href="" target="_blank">
<i class="fa fa-github fa-3x" aria-hidden="true"></i>
</a>
</li>
<li>
<a target="_blank">
<i class="fa fa-rss-square fa-3x" aria-hidden="true"></i>
</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<br/>
<!-- Page Content -->
<div class="container">
<router-outlet></router-outlet>
</div>
<footer class="navbar navbar-fixed-bottom">
<div class="text-center">
<h4 class="white">
<a target="_blank">chsakell's Blog</a>
<i>Anything around ASP.NET MVC,Web API, WCF, Entity Framework & Angular</i>
</h4>
</div>
</footer>
你知道我们在角度2 SPA时刷新我们得到资源未找到或404错误以避免我虽然实现了hashlocationstrategy但我没有成功
请分享您的想法并指导我们。
谢谢大家。
答案 0 :(得分:0)
您只需要在app.routes.ts上更改
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { UserListComponent } from './users/user-list.component';
import { ScheduleListComponent } from './schedules/schedule-list.component';
import { ScheduleEditComponent } from './schedules/schedule-edit.component';
const appRoutes: Routes = [
{ path: 'users', component: UserListComponent },
{ path: 'schedules', component: ScheduleListComponent },
{ path: 'schedules/:id/edit', component: ScheduleEditComponent },
{ path: '', component: HomeComponent }
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes, {
useHash: true });
**Replace above code with below code**
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { UserListComponent } from './users/user-list.component';
import { ScheduleListComponent } from './schedules/schedule-list.component';
import { ScheduleEditComponent } from './schedules/schedule-edit.component';
const appRoutes: Routes = [
{ path: '', redirectTo: 'users', pathMatch: 'full' },
{ path: 'users', component: UserListComponent },
{ path: 'schedules', component: ScheduleListComponent },
{ path: 'schedules/:id/edit', component: ScheduleEditComponent },
];
export const appRoutingProviders: any[] = [
];
export const routing = RouterModule.forRoot(appRoutes);