2016-01-28 31 views
6

Tôi đã thiết lập định tuyến ở góc hoạt động hoàn toàn chính xác trong ứng dụng, tuy nhiên nếu tôi điều hướng đến trang giới thiệu của mình, ví dụ: http://localhost:9000/about và làm mới trang tôi gặp lỗi nói số "Cannot GET /about". nếu tôi mở một tab mới và dán url vào đó và truy cập trang.Không thể nhận/lỗi định tuyến trang góc 2

My boot.ts tập tin định tuyến containint Logic

// -- Typescript typings ------------------------------------------------------- 
/// <reference path="../typings/jquery.d.ts" /> 
/// <reference path="../typings/jqueryui.d.ts" /> 



//Imports ---------------------------------------------------------------------- 
import {Component, enableProdMode} from 'angular2/core'; 
import {bootstrap} from 'angular2/platform/browser'; 
import { 
    ROUTER_DIRECTIVES, 
    ROUTER_PROVIDERS, 
    Router, 
    RouteConfig, 
} from 'angular2/router'; 



// -- Application Imports ------------------------------------------------------ 
import {NavbarComponent} from './components/navbar.component'; 
import {HomePage} from './pages/home.page'; 



// -- Enable production module ------------------------------------------------- 
enableProdMode(); 



// -- Component ---------------------------------------------------------------- 
@Component({ 
    selector: 'main-app', 
    directives: [ ROUTER_DIRECTIVES, NavbarComponent ], 
    template: ` 
     <app-navbar></app-navbar> 
     <router-outlet></router-outlet> 
    ` 
}) 



// -- Routing ------------------------------------------------------------------ 
@RouteConfig([ 
    { path: '/', name: 'root', redirectTo: ['/Home'] }, 
    { path: '/home', name: 'Home', component: HomePage } 
]) 



// -- Class -------------------------------------------------------------------- 
export class MainApp { 
    constructor(public router: Router) {} 
} 



// -- Bootstrap for application ------------------------------------------------ 
bootstrap(MainApp, [ 
    ROUTER_PROVIDERS 
]); 

file index.html

<!doctype html> 
<html lang="en"> 
<head> 
    <base href="/"> 

    <meta charset="UTF-8"> 
    <title>Angular2 starter</title> 

    <!-- Application css --> 
    <link href="dist/libraries/bundle.css" rel="stylesheet"></link> 
    <link href="dist/styles/main.css" rel="stylesheet"></link> 
</head> 

<body> 
    <main-app>Loading...</main-app> 

    <!-- Application js --> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
    <script src="dist/libraries/bundle.js"></script> 
</body> 

<!-- ES6-related imports --> 
<script src="/node_modules/angular2/bundles/angular2-polyfills.js"></script> 
<script src="/node_modules/es6-shim/es6-shim.min.js"></script> 
<script src="/node_modules/systemjs/dist/system.js"></script> 
<script> 
    //configure system loader 
    System.config({defaultJSExtensions: true}); 
</script> 
<script src="/node_modules/rxjs/bundles/Rx.js"></script> 
<script src="/node_modules/angular2/bundles/angular2.min.js"></script> 
<script> 
    //bootstrap the Angular2 application 
    System.import('dist/app/boot').catch(console.log.bind(console)); 
</script> 

</html> 

và dự án kết cấu

dist/ 
    app/ 
    components/ 
     navbar.component.js 
    pages/ 
     home.page.js 
    boot.js 
    assets/ 
    typings/ 
src/ 
    ... original files that are compiled into dist here 
index.html 
+0

Xem: http://stackoverflow.com/questions/34541532/is-angular-2s-router-broken-when-using-html5-routes – Langley

Trả lời

3

Trong boot.ts của bạn, đặt này:

import { bootstrap } from "angular2/platform/browser"; 
import { bind } from "angular2/core"; 
import { ROUTER_PROVIDERS, LocationStrategy, HashLocationStrategy } from "angular2/router"; 

bootstrap(MainApp, [ 
    ROUTER_PROVIDERS, 
    bind(LocationStrategy).toClass(HashLocationStrategy) 
]); 

của URL của bạn sẽ được với #/home

+0

gì xảy ra nếu tôi không cần dấu # này trong URL của tôi? là có bất kỳ thay thế? –

+0

Có, tất nhiên, có PathLocationStrategy trong Angular2, nhưng nó yêu cầu sửa đổi phía máy chủ quá. –

+0

chính xác đó là vấn đề tôi có trong sửa đổi phía máy chủ mã của tôi. cảm ơn –

2

Để bổ sung cho những gì Vlado nói, với chiến lược mặc định bạn cần một cấu hình máy chủ để chuyển hướng tất cả đường dẫn của bạn vào tệp điểm nhập HTML của bạn. Với cách tiếp cận hashbang nó không cần thiết ...

Bạn có thể có một cái nhìn tại những câu hỏi về vấn đề này:

Hy vọng nó giúp bạn, Thierry

1

phép băm với các tuyến đường của bạn

export const routing = RouterModule.forRoot(appRoutes, { useHash: true }); 
Các vấn đề liên quan