【问题标题】:Angular2 Routing 3.0.0x Get Errors for style.css when using parametersAngular2 Routing 3.0.0x 使用参数时获取 style.css 的错误
【发布时间】:2016-06-30 17:44:49
【问题描述】:

我在 Angular2 中使用最新的路由器包(3.0.0.8?)

当我向其中一条路由添加参数时,页面无法再找到所有必需的样式和应用文件。

当我有这个时:

export const routes: RouterConfig = [
    {path: '', component: LandingPageComponent },
    { path: 'buyers', component: BuyerFormComponent },
    {path: 'links', component: GenerateLinksComponent},
    {path: 'cc/:bid', component: SupplierFormComponent}
];

当我导航到“http://localhost:3000/cc/b4”时 我收到一堆错误:

获取http://localhost:3000/cc/styles.css
获取http://localhost:3000/cc/app

我的其他没有参数的路由工作正常,如果我从路由中删除参数:

export const routes: RouterConfig = [
    {path: '', component: LandingPageComponent },
    { path: 'buyers', component: BuyerFormComponent },
    {path: 'links', component: GenerateLinksComponent},
    {path: 'cc', component: SupplierFormComponent}
];

并且访问不带参数的路由(localhost3000/cc),页面加载正常。

我有 href="."在我的 index.html 中:

<html>
  <head>

    <base href=".">

有什么建议吗?谢谢!

【问题讨论】:

    标签: parameters angular routing


    【解决方案1】:

    新路由器需要

    <base href="/">
    

    空路径路由通常应该设置terminal: true

    {path: '', component: LandingPageComponent, terminal: true },
    

    在 beta.9 中,terminal: true 将被替换为

    {path: '', component: LandingPageComponent, pathMatch: full },
    

    https://github.com/angular/angular/blob/master/modules/@angular/router/src/config.ts#L258

    【讨论】:

    • 这已解决,谢谢! (显然我接受不了8分钟的答案……)
    • 我使用的是我认为是最新的指南:angular.io/docs/ts/latest/guide/router.html#!#base-href,但它仍然需要“。”是否有更新的指南,您能否指出有关“终端:真实”的文档?再次感谢您。
    • GitHub问题中提到需要更新。对于terminal 检查angular.io/docs/ts/latest/guide/router.html 我最近看到了一些更广泛的解释,但不记得在哪里。它基本上说它应该只在整个路径为'' 时匹配,否则它将匹配前缀''',这将是每条路由。 terminal 已被弃用。我会更新答案。
    猜你喜欢
    • 1970-01-01
    • 2016-06-01
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 2019-04-17
    相关资源
    最近更新 更多