2016-11-23 3 views
0

У меня проблема при обновлении браузера, он показывает 404 не найдена ошибка.Проблема при обновлении браузера angular2

Я попытался добавить
в index.html, но это бесполезно. , а также LocationStrategy, HashLocationStrategy Но, похоже, не работает.

Я использую VS2015 с 4.5 .NET Framework

Это мой 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/[email protected]/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 { 

    } 

Пожалуйста, помогите решить проблему ue Спасибо.

+0

Что проблема? –

+0

При обновлении отображается ошибка 404 – user3510028

ответ

2

Ok проблема в том, что я импортировал 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 {} 
0

Таким образом, ошибка 404 означает, что ваш веб-сервер (в этом случае сервер разработки, который вы запускаете при создании своего приложения) не может найти страницу. Создается ли ваше приложение без каких-либо ошибок? При запуске консоли должен отображаться журнал запрашиваемых страниц и найден их (http status 200).

0

проблема возникает, потому что маршрут не найден, используйте импорт {ModuleWithProviders} из '@ angular/core'; Ниже приведен пример кода для экспорта маршрутизации:

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 { } 
1

Добавить <base href="/" /> на странице индекса, а не добавлять его динамически из сценария

Смежные вопросы