【发布时间】: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