简体   繁体   English

Angular2:ContentChildren + QueryList错误

[英]Angular2 : ContentChildren+QueryList error

I want use @ContentChildren in Angular2 to navigate in a wizard. 我想在Angular2中使用@ContentChildren在向导中导航。 But when i use it, i have this error : "zone.js?fad3:158 Uncaught Error: Can't construct a query for the property "steps" of "ExtendedWizard" since the query selector wasn't defined." 但是当我使用它时,我有这个错误:“zone.js?fad3:158 Uncaught Error:无法为”ExtendedWizard“的属性”steps“构建查询,因为未定义查询选择器。”

wizard.component.ts wizard.component.ts

import { Component, ContentChildren, QueryList } from '@angular/core';

@Component({
    selector: 'extended-wizard',
    template: `
            <ul>
                <li *ngFor="let step of steps">
                    {{step.title}}
                </li>
            </ul>
            <ng-content></ng-content>
    `
})
export class ExtendedWizard {
    @ContentChildren(ExtendedStep) steps: QueryList<ExtendedStep>;
}

@Component({
    selector: 'extended-step',
    template: `
        <fieldset>
            {{title}}
            <ng-content></ng-content>
        </fieldset>
    `
})
export class ExtendedStep {
    title: string;
}

app.component.html app.component.html

<extended-wizard>
    <extended-step title="Step 1"></extended-step>
    <extended-step title="Step 2"></extended-step>
</extended-wizard>

Thanks for your help 谢谢你的帮助

You need to move the class declaration export class ExtendedStep above it's first use, otherwise it will not be recognized. 您需要将类声明export class ExtendedStep移动到它首次使用的上方,否则将无法识别。 (only if you have several classes in the same file). (仅当您在同一文件中有多个类时)。

If you move 如果你搬家

export class ExtendedStep {
    title: string;
}

just below the import s, it should work. import s下面,它应该工作。

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

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