在Angular 2中直接加载路线

时间:2017-07-11 04:46:42

标签: javascript angular angular-routing

我有一个Angular应用程序导航到两个路径:

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

每当我导航到http://localhost:8080/details/4235时。我遇到了错误:无法获取/详细信息/ 4235

以下是我在以下方式设置路线的方法:

应用-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方法。

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

修改

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

1 个答案:

答案 0 :(得分:0)

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