express - Angular2.0 路由器适用于组件而不是模块?

标签 express angular typescript angular2-routing

我正在尝试修改现有的 Angular 应用程序以适应此 Starter Project 的结构.不管怎样,我的应用程序模块有一个子模块(教程)。看起来像这样:

enter image description here 当登陆根域然后使用路由器链接导航到 http://localhost:3000/tutorial/chapter/0 ,一切正常。但是,如果我刷新页面或尝试直接转到该链接,则会出现错误:

Unhandled Promise rejection: Template parse errors:
'my-app' is not a known element:
1. If 'my-app' is an Angular component, then verify that it is part of this module.
2. If 'my-app' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ("

<body>
    [ERROR ->]<my-app>
        <div class="valign-wrapper">
            <div class="preloader-wrapper active valign"): a@30:4 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse

所以我相信这会发生,因为不是链接到应用程序组件的 url,教程子模块作为子模块,它只是链接到 TutorialModule,然后是 <my-app></my-app>。来自 index.html 的标签不被认可。这以前有效,所以我不确定这个新配置的哪个方面打破了这个。

这是我的 app.routes.ts :

import { homeComponent } from './components/home/home.component';
import { pluginsComponent } from './components/plugins/plugins.component';
import { Routes, RouterModule } from '@angular/router';

const appRoutes: Routes = [
  { path: '', component: homeComponent },
  { path: 'tutorial', loadChildren: 'tutorial/tutorial.module', pathMatch: 'prefix'},
  { path: 'plugins', component: pluginsComponent }
];

export const appRoutingProviders: any[] = [];

export const routing = RouterModule.forRoot(appRoutes);

和我的 tutorial.routes.ts :

import { Routes, RouterModule } from '@angular/router';

import { tutorialComponent }    from './tutorial.component';
import { chapterComponent }  from './chapter/chapter.component';

const tutorialRoutes: Routes = [
  {
    path: 'tutorial',
    component: tutorialComponent,
    children: [
      { path: 'chapter/:id',  component: chapterComponent },
      { path: '', redirectTo: 'chapter/0', pathMatch: 'full'},
    ]
  }
];

export const tutorialRouting = RouterModule.forChild(tutorialRoutes);

终于在我的app.ts我在其中定义了我拥有的快速路线:

app.all(/^\/tutorial$/, (req, res) => {
  res.redirect('/tutorial/');
});
app.use('/tutorial/', (req, res) => {
  res.sendFile(resolve(__dirname, '../public/index.html'));
});

为教程组件提供 Angular 索引。

整个 repo 是 here

最佳答案

问题是 index.html 文件,我有 <base href=".">它应该在哪里<base href="/"> .我有一个错误报告 here

关于express - Angular2.0 路由器适用于组件而不是模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39666718/

相关文章:

javascript - 如何返回一个对象而不是数组?

javascript - 使用过滤器或管道将产品类别加载到 Ionic 2 的页面中?

angular - ionic 可选 : error TS1086: An accessor cannot be declared in an ambient context

javascript - typescript 索引获取/设置

javascript - html内容在iframe中呈现,但不在div中呈现

javascript - 如何确定 nodejs/expressjs 函数中可用的方法

Angular 路由器 '**' 通配符作为子路由的包罗万象?使用最新的 2.4.0 和路由器 3.4.1

node.js - typescript 和 Jest : Avoiding type errors on mocked functions

javascript - 单元测试时,无法在 Angular RC 5 中解析路由器 : (?、?、?、?、?、?、?) 的所有参数

javascript - 内部服务器错误(使用 OMDB API 时返回错误)