我有一个使用Angular2路由器和表达的问题,但没有一个以前的问题似乎有我需要的解决方案。我正在使用angular2-cli的最新版本。Angular2路由器和快速集成
当我将浏览器指向localhost:3000时,我得到了angular2-cli生成的通用“app works”消息。当我尝试导航到其中一个子路由时,例如localhost:3000/auth/login,它会将我重定向到相同的“应用程序作品”页面,而不是向我显示一个页面,其中显示“登录作品” 。
看着堆栈交换其他问题,我想这个问题是在这里:
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
当我更换*有/,而不是重定向我回到我的目标页面,它给我的无法获取错误。
总之,我的问题是我的angular2路由集成到我的快速应用程序的URL导航无法正常工作。
我在server.js快递代码如下:
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var path = require("path");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static(path.join(__dirname, 'dist')));
// Catch all other routes and return the index file
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
app.listen(3000,() => {
console.log("Server has started");
});
我的应用程序的路由模块如下(APP-routing.module.ts):
import {Route, RouterModule} from '@angular/router';
import {NgModule} from '@angular/core';
import {LoginComponent} from './login/login.component';
import {RegisterComponent} from './register/register.component';
import {FeedComponent} from './feed/feed.component';
import {FindComponent} from './find/find.component';
import {SubmitComponent} from './submit/submit.component';
import {ProfileComponent} from './profile/profile.component';
export const routeConfig = [
{
path: "auth",
children: [
{
path: "login",
component: LoginComponent
},
{
path: "register",
component: RegisterComponent
},
{
path: "",
redirectTo: "/login",
pathMatch: "full"
}
]
},
{
path: "app",
children: [
{
path: "feed",
component: FeedComponent
},
{
path: "find",
component: FindComponent
},
{
path: "submit",
component: SubmitComponent
},
{
path: "profile",
component: ProfileComponent
},
{
path: "",
redirectTo: "/feed",
pathMatch: "full"
}
]
}
];
@NgModule({
imports: [RouterModule.forRoot(routeConfig)],
exports: [RouterModule]
})
export class AppRoutingModule{
}
我的应用程序模块如下所示:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import {AppRoutingModule} from './app-routing.module';
import { AppComponent } from './app.component';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { FeedComponent } from './feed/feed.component';
import { FindComponent } from './find/find.component';
import { SubmitComponent } from './submit/submit.component';
import { ProfileComponent } from './profile/profile.component';
@NgModule({
declarations: [
AppComponent,
LoginComponent,
RegisterComponent,
FeedComponent,
FindComponent,
SubmitComponent,
ProfileComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
所有组件已在其各自的文件中定义,并且是准系统由angular2-cli生成的默认模板。
我不知道该怎么做。我甚至尝试多次重新启动并重写这个项目,看看我是否在某处出错(这是我的第三次尝试)。
我非常愚蠢的错误 - 我忘了将添加到app.component.html文件。 问题解决。 –
user7486352
在任何不起作用的东西上检查浏览器控制台总是一个好主意,即使它看起来没有道理 - 在这种情况下,它会显示没有提供路由器插座的错误。很高兴你想出来了。 – glitchbane