
本文介绍了Angular 2 路由刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在这里实施哈希定位策略,但它不起作用,请指导我.
这是我的 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","引导程序": "^3.3.6",
Main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';从 './app.module' 导入 { AppModule };platformBrowserDynamic().bootstrapModule(AppModule);
app.routes.ts
import { ModuleWithProviders } from '@angular/core';从'@angular/router' 导入 { Routes, RouterModule };从 './home/home.component' 导入 { HomeComponent };从 './users/user-list.component' 导入 { UserListComponent };从 './schedules/schedule-list.component' 导入 { ScheduleListComponent };从 './schedules/schedule-edit.component' 导入 { ScheduleEditComponent };const appRoutes: 路由 = [{ path: 'users', 组件: UserListComponent },{ path: 'schedules', 组件: ScheduleListComponent },{ path: 'schedules/:id/edit', 组件: ScheduleEditComponent },{ 路径:'',组件:HomeComponent }];导出常量路由: ModuleWithProviders = RouterModule.forRoot(appRoutes, { useHash: true });
app.modules.ts
import './rxjs-operators';从'@angular/core' 导入 { NgModule };从'@angular/platform-browser' 导入 { BrowserModule };从'@angular/forms'导入{FormsModule};从'@angular/http'导入{HttpModule};从 '@angular/common' 导入 { LocationStrategy,HashLocationStrategy };从'ng2-bootstrap/ng2-bootstrap'导入{分页模块};从'@angular/router' 导入 { Routes, RouterModule };从'ng2-bootstrap/ng2-bootstrap'导入{DatepickerModule};从 'ng2-bootstrap/ng2-bootstrap' 导入 { Ng2BootstrapModule };从 'ng2-bootstrap/ng2-bootstrap' 导入 { ModalModule };从'ng2-bootstrap/ng2-bootstrap'导入{ProgressbarModule};import { SlimLoadingBarService, SlimLoadingBarComponent } from 'ng2-slim-loading-bar';从'ng2-bootstrap/ng2-bootstrap'导入{TimepickerModule};从 './app.component' 导入 { AppComponent };从 './shared/pipes/date-format.pipe' 导入 { DateFormatPipe };从'./shared/directives/highlight.directive'导入{HighlightDirective};从 './home/home.component' 导入 { HomeComponent };从'./shared/directives/mobile-hide.directive'导入{MobileHideDirective};从 './schedules/schedule-edit.component' 导入 { ScheduleEditComponent };从 './schedules/schedule-list.component' 导入 { ScheduleListComponent };从 './users/user-card.component' 导入 { UserCardComponent };从 './users/user-list.component' 导入 { UserListComponent };从'./app.routes'导入{路由};从'./shared/services/data.service'导入{数据服务};从 './shared/utils/config.service' 导入 { ConfigService };从'./shared/utils/items.service'导入{ItemsService};从'./shared/utils/mapping.service'导入{映射服务};从 './shared/utils/notification.service' 导入 { NotificationService };从@angular/core"导入 { enableProdMode };enableProdMode();@NgModule({进口:[浏览器模块,日期选择器模块,表单模块,Http模块,Ng2BootstrapModule,模态模块,进度条模块,分页模块,路由,时间选择器模块],声明: [应用组件,日期格式管道,高亮指令,主页组件,移动隐藏指令,计划编辑组件,调度列表组件,SlimLoadingBarComponent,用户卡组件,用户列表组件],供应商: [配置服务,数据服务,项目服务,地图服务,通知服务,SlimLoadingBarService,{ 提供: LocationStrategy, useClass: HashLocationStrategy }],引导程序:[AppComponent]})导出类 AppModule { }
app.component.ts
import { Component, OnInit, ViewContainerRef } from '@angular/core';//添加我们在这个应用程序中需要的 RxJS Observable 操作符.导入'./rxjs-operators';@成分({选择器:'调度程序',templateUrl: 'app/app.component.html'})导出类 AppComponent {构造函数(私有 viewContainerRef:ViewContainerRef){//你需要这个小技巧来捕捉应用程序根视图容器引用this.viewContainerRef = viewContainerRef;}}
索引.html
<头><base href="/"><meta charset="utf-8"/><title>调度器</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. 加载库--><!-- 旧浏览器的 Polyfill(s) --><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.配置SystemJS--><script src="systemjs.config.js"></script><脚本>System.import('app').catch(function(err){ console.error(err); });<身体><调度器><div class="loader"></div></调度器></html>
app.component.html
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation"><ng2-slim-loading-bar></ng2-slim-loading-bar><div class="容器"><!-- 品牌和切换被分组以获得更好的移动显示--><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"><span class="sr-only">切换导航</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span><a class="navbar-brand" [routerLink]="['/']"><i class="fa fa-home fa-3x" aria-hidden="true"></i></a>
<!-- 收集导航链接、表单和其他用于切换的内容--><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><ul class="nav navbar-nav"><li><a [routerLink]="['/schedules']"><i class="fa fa-calendar fa-3x" aria-hidden="true"></i></a><li><a [routerLink]="['/customers']"><i class="fa fa-calendar fa-3x" aria-hidden="true"></i></a><li><a [routerLink]="['/customermaintenance']"><i class="fa fa-calendar fa-3x" aria-hidden="true"></i></a><li><a [routerLink]="['/users']"><i class="fa fa-users fa-3x" aria-hidden="true"></i></a><li><a href="" target="_blank"><i class="fa fa-info fa-3x" aria-hidden="true"></i></a><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><a href="" target="_blank"><i class="fa fa-twitter fa-3x" aria-hidden="true"></i></a><li><a href="" target="_blank"><i class="fa fa-github fa-3x" aria-hidden="true"></i></a><li><a target="_blank"><i class="fa fa-rss-square fa-3x" aria-hidden="true"></i></a>
<!--/.navbar-collapse -->
<!--/.container --></nav><br/><!-- 页面内容--><div class="容器"><路由器插座></路由器插座>
<footer class="navbar navbar-fixed-bottom"><div class="text-center"><h4 class="white"><a target="_blank">chsakell 的博客</a><i>任何关于 ASP.NET MVC、Web API、WCF、实体框架和角度
</页脚>
你们都知道在 angular 2 SPA 中,当我们刷新时,我们得到资源未找到或 404 错误,以避免我虽然实施了 hashlocationstrategy 但我没有成功
请分享您的想法并指导我.
谢谢大家.
解决方案
你只需要在app.routes.ts上更改
import { ModuleWithProviders } from '@angular/core';从'@angular/router' 导入 { Routes, RouterModule };从 './home/home.component' 导入 { HomeComponent };从 './users/user-list.component' 导入 { UserListComponent };从 './schedules/schedule-list.component' 导入 { ScheduleListComponent };从 './schedules/schedule-edit.component' 导入 { ScheduleEditComponent };const appRoutes: 路由 = [{ path: 'users', 组件: UserListComponent },{ path: 'schedules', 组件: ScheduleListComponent },{ path: 'schedules/:id/edit', 组件: ScheduleEditComponent },{ 路径:'',组件:HomeComponent }];导出常量路由: ModuleWithProviders = RouterModule.forRoot(appRoutes, {useHash: true });**用下面的代码替换上面的代码**从'@angular/core' 导入 { ModuleWithProviders };从'@angular/router' 导入 { Routes, RouterModule };从 './home/home.component' 导入 { HomeComponent };从 './users/user-list.component' 导入 { UserListComponent };从 './schedules/schedule-list.component' 导入 { ScheduleListComponent };从 './schedules/schedule-edit.component' 导入 { ScheduleEditComponent };const appRoutes: 路由 = [{ path: '', redirectTo: 'users', pathMatch: 'full' },{ path: 'users', 组件: UserListComponent },{ path: 'schedules', 组件: ScheduleListComponent },{ path: 'schedules/:id/edit', 组件: ScheduleEditComponent },];导出 const appRoutingProviders: any[] = [];export const routing = RouterModule.forRoot(appRoutes);
I am trying to Implement Hashlocation Strategy out here it is not working please guide me.
This is My Angular & Router Version
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>
You all know in angular 2 SPA when we refresh we get resource not found or 404 error to avoid i though of implementing the hashlocationstrategy but i am not successfull
kindly share your thoughts and guide me guys.
Thanks for all.
解决方案
You only need to change on 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);
这篇关于Angular 2 路由刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!