Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
角度2 RC5&@angular/router 3.0.0-rc.1配置无效或存在错误?_Angular_Typescript_Webpack_Angular2 Routing_Angular Cli - Fatal编程技术网

角度2 RC5&@angular/router 3.0.0-rc.1配置无效或存在错误?

角度2 RC5&@angular/router 3.0.0-rc.1配置无效或存在错误?,angular,typescript,webpack,angular2-routing,angular-cli,Angular,Typescript,Webpack,Angular2 Routing,Angular Cli,在本例中,我使用Angular 2的RC5和最新路由器遇到了这个问题 我的routes.ts文件如下: import { provideRouter, Routes, RouterModule } from '@angular/router'; import { OverviewComponent, LoginComponent, ProfileComponent } from './shared'; import { AuthGuard } from './auth.g

在本例中,我使用Angular 2的RC5和最新路由器遇到了这个问题

我的routes.ts文件如下:

import {
  provideRouter,
  Routes,
  RouterModule
}
from '@angular/router';
import {
  OverviewComponent,
  LoginComponent,
  ProfileComponent
} from './shared';
import { AuthGuard } from './auth.guard';
import { HasToken } from './common/index';

const routes: Routes = [
  {
    path: '',
    component: OverviewComponent,
    canActivate: [AuthGuard]
  },
  {
    path: 'login',
    component: LoginComponent
  },
  {
    path: 'profile',
    component: ProfileComponent,
    canActivate: [AuthGuard]
  },
  {
    path: '**',
    redirectTo: '/login'
  }
];

export const authProviders = [AuthGuard, HasToken];

export const appRouterProviders = [
  provideRouter(routes),
  authProviders
];

export const routing = RouterModule.forRoot(routes);
我的app.module.ts文件(引导)如下:

最后,我的输入文件(main.ts)是:

因此,当我运行
ng-serve
(这是一个带有网页包的angular cli项目)时,我在控制台中遇到以下错误:

异常:错误:路由“”的配置无效:必须提供以下选项之一(组件或重定向到或子级或loadChildren)

更新代码和新错误

模块“AppModule”声明的未捕获意外值“undefined”

最新更新


看来这些桶有问题。如果我将组件导入到
应用程序模块
中,它会绕过此错误,但会给出另一个错误:

uri.match不是一个函数


当然,我尝试在routes中添加
pathMatch
属性,但没有任何改变。

我认为您的routes设置错误。您需要导入路由

import { Routes,provideRouter, RouterConfig, RouterModule '@angular/router';
而不是创建RouterConfig

const routes: RouterConfig = []
改用路线

const appRoutes: Routes = []

您不需要导入这些

provideRouter, RouterConfig
也不需要这个代码

export const appRouterProviders = [
  provideRouter(routes),
  authProviders
];

而是将路由类型更改为导入所需的路由


就是这样。

我的问题毕竟很简单(试了这么多小时)

解决方案: 不要从桶中导入组件 直接从文件夹中导入它们

这解决了我的问题

更新:


关于
未定义
错误,问题是并非我的所有组件都在app.module的
导入
中声明。

您对AppModule的导入具有重复的RoutingModule声明。此外,不需要导入CommonModule,因为它已经由BrowserModule导出

尝试从以下位置更改导入:

imports: [
    BrowserModule,
    CommonModule,
    // Router
    RouterModule,
    routing,
    // Forms
    FormsModule,
 ],
为此:

imports: [
    BrowserModule,
    routing,
    FormsModule
 ],

对于仍然存在此问题的任何人,如果为组件定义路由,然后在页面底部定义组件,则会抛出此错误

基本上,此错误意味着为路由提供的组件未定义


因此,将组件类移动到路由之前,或者如果您要导入它,请确保它已正确导入。

这样认为,并已尝试过。在这种情况下,我仍然得到:>未捕获的意外值'undefined'由模块'AppModule'声明,我在这里得到了问题,但仍然是相同的问题:/n如果我从我的
app.module
中删除那些声明
OverviewComponent、LoginComponent、ProfileComponent
,那么我会得到第一个错误,否则,我的问题会出现第二个错误,这似乎是一个角度的问题:我不确定到底是什么修正了什么。你能发布一个前后比较吗?是的,我今天晚些时候会发布。我有同样的错误,直接导入组件后,我再也没有这个错误了。我也得到了:错误:模块'AppModule'声明的意外值'undefined',我很有信心我不是从桶中加载的。我注意到,如果删除声明行,代码就会抱怨提供者未定义。我怀疑这与systemjs有关,但不确定如何。当然,使用RC4一切都很好。@user1637302您还没有在appmodule中声明您的组件。添加它们,您就可以开始了;)检查我的答案,那不是问题所在。
appRouterProviders,
imports: [
    BrowserModule,
    CommonModule,
    // Router
    RouterModule,
    routing,
    // Forms
    FormsModule,
 ],
imports: [
    BrowserModule,
    routing,
    FormsModule
 ],