【问题标题】:Direct loading routes in Angular 2Angular 2中的直接加载路线
【发布时间】:2017-12-14 23:42:17
【问题描述】:

我有一个导航到两条路线的 Angular 应用程序:

http://localhost:8080/ /* Landing page */
http://localhost:8080/details/:id /* Result page */

每当我导航到 http://localhost:8080/details/4235 时。我遇到了错误:Cannot GET /details/4235

这是我设置路线的方式,在我的:

app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LandingPage } from './components/landingpage/landingpage.component';
import { ResultPage } from './components/resultpage/resultpage.component';

import { TitleService } from './services/title.service';

const routes: Routes = [
  { path: '', component: LandingPage },
  {
    path: 'details/:id', component: ResultPage, pathMatch: 'full'
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
  providers: [TitleService]
})
export class AppRoutingModule { }

我导入到我的:

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms'; // <-- NgModel lives here
import { RouterModule, Routes } from '@angular/router';
import { AppComponent }         from './app.component';
import { LandingPage } from './components/landingpage/landingpage.component';
import { ResultPage } from './components/resultpage/resultpage.component';
import { AppRoutingModule } from './app-routing.module';

@NgModule({
  declarations: [
    AppComponent,
    LandingPage,
    ResultPage
  ],
  imports: [
    BrowserModule,
    FormsModule, // <-- import the FormsModule before binding with [(ngModel)]
    HttpModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

我无法找到一种有效的 Angular 2 处理直接链接到 url 的方法。

如何在 Angular 2 中设置直接链接,这样当我加载 http://localhost:8080/details/4235 时,它会加载我的 ResultPage 组件。

编辑:

加载 http://localhost:8080/#/details/4235 是有效的,但它会重新加载 LandingPage 组件。当有扩展 url 时,我正在尝试加载 ResultPage 组件。

【问题讨论】:

  • 我猜你错过了其中的#
  • @Aravind 。感谢您的回复!当我尝试这个 URL 时,它会加载 LandingPage 组件,而不是 ResultPage 组件。我正在尝试让它加载 ResultPage。
  • 你有什么错误吗?
  • 没有错误,它只是加载我的登陆页面

标签: javascript angular angular-routing


【解决方案1】:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LandingPage } from 
'./components/landingpage/landingpage.component';
import { ResultPage } from 
'./components/resultpage/resultpage.component';

import { TitleService } from './services/title.service';

const routes: Routes = [
  { path: '', component: LandingPage },
{
path: 'details/:id', component: ResultPage
}
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [TitleService]
})
export class AppRoutingModule { }

在这里,您首先加载空路径。然后将加载其他路径。更新空路径路由。最后将其保留在层次结构中。

更新代码:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LandingPage } from ' 
./components/landingpage/landingpage.component';
import { ResultPage } from 
'./components/resultpage/resultpage.component';

import { TitleService } from './services/title.service';

const routes: Routes = [
{
path: 'details/:id', component: ResultPage
},
{ path: '', component: LandingPage } //<-- here  
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [TitleService]
})
export class AppRoutingModule { }

编辑: 也可能存在您的浏览器不支持 HTML5 pushState 的问题。我指的是一些可能会有所帮助的 SO 链接。

Configure history.pushState for Angular 2

enter link description here

Angular 2.0 router not working on reloading the browser

【讨论】:

  • 感谢您的回复!不幸的是,localhost:8080/#/details/246 在您更改后仍会重定向到我的登录页面组件。 localhost:8080/details/246 仍然返回 Cannot GET /details/246
  • 尝试删除空路径路由,然后在尝试 localhost:8080/details/246 时查看错误。
  • 我按照你说的试过了,除了Cannot GET /details/246没有错误。这是i.imgur.com/6LGTWbu.png 发生的图像 **注意左侧的页眉和页脚已加载到我的根组件中
  • 您在左侧看到的页眉和页脚来自我的根组件,它加载到所有组件上。实际的 ResultPage 永远不会加载,html 应该是空白的地方
  • 我们终于做到了。 ;)
猜你喜欢
  • 1970-01-01
  • 2017-07-28
  • 2016-12-22
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 2019-03-04
  • 1970-01-01
  • 2021-12-31
相关资源
最近更新 更多