2015-12-21 53 views
3

我有路由問題。Angular2路由。在網址結尾的斜線

當我在URL「myapp.com/route」像從「myapp.com/route/」我的資源裝載結束多了一個斜線加上「myapp.com/route/......」。

例如:我必須從'myapp.com/starwars.js'加載庫,但使用斜槓將從'myapp.com/route/starwars.js'加載。 http://prntscr.com/9gpeen

但它是不正常的斜線 http://prntscr.com/9gpepv

代碼

import {Component} from 'angular2/core'; 
import {Route, RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router' 
    import {NotFound} from './notfound/notfound'; 
import {TimerComponent} from './timer/timer.component' 

    @Component({ 
    selector: 'my-app', 
    template:` 
     <a [routerLink]="['Timer']">Timer</a> 
     <router-outlet></router-outlet> 
     `, 
    directives: [ROUTER_DIRECTIVES] 
    }) 
    @RouteConfig([ 

    { path: 'timer/', name: 'Timer', component: TimerComponent}, 
    new Route({path: '/**', component: NotFound}) 
    ]) 
    export class AppComponent { } 

謝謝您的解答!

回答

0

我不知道你怎麼包括您starwars.js但相對於當前的URL可能。這就是爲什麼當你的地址有斜線時,它被解析爲錯誤的地址。

你可以嘗試添加基本URL到您的網頁:

<base href="/"> 

有了這個,你將確保所有的相對地址去反對`/``得到解決。

欲瞭解更多信息,請參閱Angular 2 doc here

+0

這是問題對我來說,我的標籤是在錯誤的地方,哎呦!謝謝! – trickpatty