Angular2路由.在url末尾斜杠

Angular2 Routing. Slash in the end of url

本文关键字:url 路由 Angular2      更新时间:2023-09-26

我在路由方面遇到问题。

当我在url"myapp.com/route"中添加一个斜杠,就像"myapp.com/sroute/"一样,我的资源从"myapp.microsoft.com/route/…"加载。

例如:我有一个库必须从"myapp.com/starwars.js"加载,但使用斜杠,它将从"myapps.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 { }

谢谢你的回答!

我不知道如何包含starwars.js,但它可能是相对于当前url的。这就是为什么当你的地址中有斜杠时,它会被解析为错误的地址。

您可以尝试将基本url添加到您的页面:

<base href="/">

这样,您将确保所有相对地址都将根据"/"进行解析。

有关更多信息,请参阅Angular 2文档。