简体   繁体   English

Angular2路由。 在URL的末尾斜杠

[英]Angular2 Routing. Slash in the end of url

I have trouble with routing. 我在路由方面遇到了麻烦。

When I add in url 'myapp.com/route' one more slash in the end like that 'myapp.com/route/' my resources loading from 'myapp.com/route/...'. 当我添加url'myapp.com/route'时,最后再添加一个斜杠,比如'myapp.com/route/'从'myapp.com/route / ...'加载我的资源。

For example: I had library that have to be loaded from 'myapp.com/starwars.js', but with slash it will be loaded from 'myapp.com/route/starwars.js'. 例如:我必须从'myapp.com/starwars.js'加载库,但是使用斜杠它将从'myapp.com/route/starwars.js'加载。 http://prntscr.com/9gpeen http://prntscr.com/9gpeen

But it is normal without slash http://prntscr.com/9gpepv 但是没有斜线这是正常的http://prntscr.com/9gpepv

Code

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

Thank you for your answers! 谢谢您的回答!

I don't know how do you include your starwars.js but it is probably relative to current url. 我不知道你怎么包括你的starwars.js但它可能与当前网址相关。 That's why it is resolved to wrong address when you have a slash in your address. 这就是为什么当你的地址中有斜线时,它被解决为错误的地址。

You can try adding base url to your page: 您可以尝试在网页中添加基本网址

<base href="/">

With this, you will ensure that, all relative addresses are going to be resolved against `/``. 有了这个,你将确保所有相对地址将被解析为`/``。

For more information please see Angular 2 doc here . 有关更多信息,请参阅Angular 2 doc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM