【问题标题】:Cannot load the front end routing in MEAN STACK with Angular 4无法使用 Angular 4 在 MEAN STACK 中加载前端路由
【发布时间】:2018-07-02 01:12:53
【问题描述】:

我正在使用带有 Angular 4 的 MEAN STACK 创建一个应用程序(这项技术的新手)。我正在尝试处理前端路由,即使用 url http://localhost:3000/admin 它应该加载管理组件,http://localhost:3000/registration 它应该加载注册组件。

App-Routing.modeule.ts

import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {RegistrationFormComponent} from './registration-form/registration-form.component';
import {AdminFormComponent} from './admin-form/admin-form.component';


const appRoutes: Routes = [
    {path: 'registration', component: RegistrationFormComponent},
    {path: 'admin', component: AdminFormComponent},
    {path: '', redirectTo: '/admin', pathMatch: 'full'}
    //{path: '*', redirectTo: '/registration', pathMatch: 'full'}
];

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

}

server.js

app.use('/', express.static(path.join(__dirname, '/dist')));
app.get('/registration', api);
app.get('/admin', api);
app.use('/', api);

路由器.js

router.get('/registration', function(req, res){
    //res.send('hello world');
   console.log('registration');
});

我也有一个 router.js 类。路由在我的角度应用程序中工作正常。但是,当使用带有角度的 express 时,我不应该如何处理前端路由。如果需要通过路由器完成。 js 不确定我应该在其中指定什么路径。

我收到错误:

错误:无法获取页面名称

【问题讨论】:

标签: node.js angular mean-stack


【解决方案1】:

设置静态路径

app.use(express.static(path.join(__dirname, 'dist')));

然后

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/index.html'));
});

您不需要在服务器端为 Angular 应用程序编写路由

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 2018-03-08
    • 1970-01-01
    • 2018-11-17
    • 2015-10-05
    • 2018-11-09
    • 2015-07-20
    相关资源
    最近更新 更多