【问题标题】:Angular 2 Routing not working, no matchAngular 2路由不起作用,不匹配
【发布时间】:2018-12-24 23:46:16
【问题描述】:

我目前正在开发一个需要路由的 Angular 2+ 应用程序。 应用的整个配色方案需要随着 URL 参数的输入而改变。

我目前在我的app.module.ts 中使用它:

const appRoutes: Routes = [
  { path: 'main/:key', component: MainComponent }
];

@NgModule({
  imports: [
    RouterModule.forRoot(appRoutes)
  ]
})

但是当我访问时: http://localhost:4200/main/someKey 要么 http://localhost:4200/main?key=someKey 路径无法匹配,应用程序正在生成 404。

这似乎是 URL 参数的最基本应用。 任何想法为什么它不起作用?

【问题讨论】:

  • 你有<router-outlet></router-outlet>吗?
  • 是的,我的app.component.html中有它
  • 您能否创建一个stackblitz.com 来重现您的问题。
  • 你能告诉我们你的 MainComponent 的上下文吗?
  • 问题是<base href="/">,设置为./。我将其更改为 / 现在可以使用了

标签: angular typescript routing


【解决方案1】:

我通过更改我的基本 href 来解决这个问题。它设置为<base href="./">,因为它在上传到服务器上的子目录时不起作用,但现在我将它改回<base href="/">,这解决了url不匹配的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-05
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 2019-03-28
    • 2017-04-06
    • 2017-01-01
    相关资源
    最近更新 更多