简体   繁体   English

Angular 2 Router错误,Route配置应该只包含一个“component”,“loader”或“redirectTo”属性

[英]Angular 2 Router error, Route config should contain exactly one “component”, “loader”, or “redirectTo” property

This is my setup: 这是我的设置:

import {bootstrap} from 'angular2/platform/browser';
import {Component} from 'angular2/core';
import {LocationStrategy, APP_BASE_HREF, HashLocationStrategy, ROUTER_DIRECTIVES, Router, RouteConfig, ROUTER_PROVIDERS} from 'angular2/router';
import {HomeComponent} from "../components/HomeComponent";
import {provide} from "angular2/core";

@Component({
    selector: 'app',
    template: `<a [routerLink]="['/Home']">Home</a>
              <router-outlet></router-outlet>`,
    directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
    { path: '/', component: HomeComponent, name: 'HomeComponent' }
])
class RootComponent {
    constructor(router:Router) {

    }
}

bootstrap(RootComponent, [ROUTER_PROVIDERS, provide(LocationStrategy, {useClass: HashLocationStrategy})
    , provide(APP_BASE_HREF, {useValue: '/'})]);

and I keep getting this error, any help is appreciated 我一直得到这个错误,任何帮助表示赞赏

在此输入图像描述

I am using latest Beta.0 我正在使用最新的Beta.0

regards 问候

Sean 肖恩

I had met same issue,but I resolved by using the same component name used in the component definition and when import the component,eg: 我遇到了同样的问题,但我通过使用组件定义中使用的相同组件名称以及导入组件时解决了问题,例如:

definition 定义

import {Component} from 'angular2/core';

@Component({
    selector: 'register-page',
    template: `
        <h4>Register</h4>

    `
})
export class RegisterPageComponent {
   //public hero: Hero;
}

import 进口

import {RegisterPageComponent} from './register.component';

@RouteConfig([
   {path: '/register',   name: 'Register',component: RegisterPageComponent}
])

I met the same issue when I tried to change the component name on import eg: issue 当我尝试在导入时更改组件名称时遇到了同样的问题,例如:issue

import {RegisterComponent} from './register.component'; 

/*remove`Page` from the name ,it should be RegisterPageComponent*/

route 路线

@RouteConfig([
    {path: '/register',name:'Register',component:RegisterComponent}
])

I had the same issue. 我遇到过同样的问题。 My import was not surrounded by curly braces, so it was undefined. 我的导入没有被花括号括起来,所以它是未定义的。 The problem is the cryptic error message, which should state that the component you provided is undefined for the @RouteConfig . 问题是隐秘的错误消息,它应该声明您提供的组件undefined@RouteConfig

I had the same error but for an other reason. 我有同样的错误,但出于其他原因。 It's true that the error reporting is still very poor in ng2. 确实,ng2中的错误报告仍然很差。 The error: Route config should contain exactly one “component”, “loader”, or “redirectTo” property, really just means that what you have put in your routeConfig is not correct. 错误:Route config应该只包含一个“component”,“loader”或“redirectTo”属性,实际上只是意味着你在routeConfig中放置的内容不正确。 In my case, my components were undefined. 就我而言,我的组件未定义。 This due to a stupid order mistake. 这是由于一个愚蠢的订单错误。

I had this (component.ts) 我有这个(component.ts)

export * from './trackerpanel.component';
export * from './login.component';
export * from './oauthcallback.component';
export * from './tracker.component';
export * from './settings.component';

And an import like this (trackerpanel.component) 像这样的导入(trackerpanel.component)

import {TrackerComponent, SettingsComponent} from './component';

And I was trying to use the components tracker and settings in routes defined in trackerpanel. 我试图在trackerpanel中定义的路径中使用组件跟踪器和设置。 This gave me undefined components, and the aforementioned error. 这给了我未定义的组件,以及上述错误。

@RouteConfig([
  {path: '/', name: 'Tracker', component: TrackerComponent, useAsDefault: true },
  {path: '/settings', name: 'Settings', component: SettingsComponent }
])

I changed it to 我改成了

export * from './tracker.component';
export * from './settings.component';
export * from './trackerpanel.component';
export * from './login.component';
export * from './oauthcallback.component';

And this fixed my routing problem. 这解决了我的路由问题。

Same error i had faced but i have solved this as below: 我面临同样的错误,但我已经解决了以下问题:

when i write the code like this 当我写这样的代码

{ path: '/formDemo', Component: FormDemo, name: "FormDemo"},

it shows throws the same error you faced but after searching i have found error is in my Component attribute of routeConfig ie angular2 thinks we have write a Component annotation in the routeConfig but it accept only exactly one loader,component,redirectto (URL) property. 它显示抛出你遇到的相同错误但是在搜索之后我发现错误是在我的RouteConfig的Component属性中,即angular2认为我们在routeConfig中编写了一个Component注释,但它只接受一个loader,component,redirectto(URL)属性。 but we have write something else , so when i changed Component with component code is working fine. 但我们已经写了别的东西,所以当我用component代码更改Component时工作正常。

{ path: '/formDemo', component: FormDemo, name: "FormDemo"},

above line make my code working. 上面的行使我的代码工作。 It may not help to solve your problem because you have already write component insted of Component but i have posted this for others may be it will help to someone else thanks 它可能没有帮助解决您的问题,因为您已经编写组件的组件,但我已经发布了其他人可能会有助于其他人谢谢

In my case the problem was incorrectly imported child component. 在我的情况下,问题是错误导入的子组件。 The component export was default: 组件导出是默认的:

export default class HomeComponent {}

So i replaced its import from 所以我替换了它的导入

import { HomeComponent } from './homeComponent';

to

import HomeComponent from './homeComponent';

and it works! 它的工作原理!

Interesting that webpack which i use to bundle the app does not provide any errors/warnings. 有趣的是,我用来捆绑应用程序的webpack不提供任何错误/警告。

In my case it was the mismatch of the component name in import {TestComponent} and "export class TestingComponent" 在我的例子中,导入{TestComponent}和“export class TestingComponent”中的组件名称不匹配

TestingComponent should be TestComponent. TestingComponent应该是TestComponent。

I had the same issue just now. 我刚才有同样的问题。 This had to do with me creating a component, and Webpack didn't recognize it. 这与我创建一个组件有关,而Webpack没有认出它。

After restarting Webpack the error went away.. 重新启动Webpack后错误消失了..

Your NgModule should be changed from 你的NgModule应该改变

provide(LocationStrategy, {useClass: HashLocationStrategy})
    , provide(APP_BASE_HREF, {useValue: '/'})]);

To

providers: [LocationStrategy, {useClass: HashLocationStrategy}), {provide: APP_BASE_HREF, useValue: '/'}]

https://github.com/angular/angular/issues/12295 https://github.com/angular/angular/issues/12295

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM