StackLayout - addChild不是一个函数

时间:2017-12-01 18:14:15

标签: android nativescript angular2-nativescript

这在NativeScript + Angular应用程序中发生。

我正在尝试使用方法StackLayoutStackLayout.addChild视图中动态添加标签,如documentation所示。

我的观点如下:

<MainActionBar > </MainActionBar>
    <ScrollView orientation="vertical" #scroll>

    <StackLayout class="full-page settings-list pad-bottom" #stack >
            <Label class="text-color" text="Cadastro genérico"></Label>
    </StackLayout>

    <StackLayout class="full-page settings-list pad-bottom">
            <Button class="btn btn-primary btn-active" id="createInput" text="Cria input" (tap)="criaInput()"></Button>
            <Button class="btn btn-primary btn-active" id="createSelect" text="Cria select" (tap)="criaSelect()"></Button>
    </StackLayout>

</ScrollView>

Typescript类:

    import { FormControl, Validators } from '@angular/forms';
import {
    ViewChild, ChangeDetectorRef, OnInit, AfterViewInit,
    ChangeDetectionStrategy, Component
} from '@angular/core';

import {NavigationEnd} from '@angular/router';

import sideDrawerModule = require('nativescript-telerik-ui/sidedrawer');
import {RadSideDrawerComponent} from "nativescript-telerik-ui/sidedrawer/angular";
import {DrawerTransitionBase, PushTransition} from "nativescript-telerik-ui/sidedrawer";
import {Page} from "ui/page";
import {Frame} from "ui/frame";
import {RouterExtensions} from "nativescript-angular";
import {DrawerService} from "../../services/drawer.service";
import {ActionBarUtil} from "../../utils/actionbar.util";
import {Observable} from "data/observable";

import { Label } from 'ui/label';

import { StackLayout } from 'ui/layouts/stack-layout';


@Component({
    moduleId: module.id,
    selector: 'cadastro-generico',
    templateUrl: 'cadastro-generico.component.html',
    styleUrls: ['cadastro-generico.component.scss'],
    changeDetection: ChangeDetectionStrategy.Default
})
export class CadastroGenericoComponent extends Observable implements OnInit, AfterViewInit {


    @ViewChild('stack') stack : StackLayout;

    constructor(private routerExtensions: RouterExtensions,
        private changeDetectionRef: ChangeDetectorRef,
        public drawerService: DrawerService)  {

        super();
            console.log(`RelatorioComponent constructor`);
        }

        ngOnInit() {
            console.log(`RelatorioComponent ngOnInit`);
        }

        ngAfterViewInit() {
            console.log("RelatorioComponent ngAfterViewInit - this.drawerComponent.sideDrawer=", this.drawerService.drawer);
            this.changeDetectionRef.detectChanges();
        }

        navigateTo(pathToNavigate:string) {
            console.log("navigating to:", pathToNavigate);
            this.routerExtensions.navigate([pathToNavigate], {clearHistory: true});
        }

        criaInput(){
            console.log("clicou cria input")
            let lbl = new Label()
            lbl.text = "Share This!"
            this.stack.addChild(lbl)
        }

        criaSelect(){
            console.log("clicou cria select");
        }
}

方法criaInput()应该在屏幕上添加标签。 Altought,当我点击按钮时,会发生以下错误:

System.err: com.tns.NativeScriptException:

System.err: Calling js method onClick failed

System.err: TypeError: this.stack.addChild is not a function

有关如何解决这个问题的想法吗?

1 个答案:

答案 0 :(得分:1)

将其更改为:

@ViewChild('stack') stack : ElementRef; // import { ElementRef } from "@angular/core";

// and later:
const myStack: StackLayout = this.stack.nativeElement; // import { StackLayout } from "tns-core-modules/ui/layouts/stack-layout";
myStack.addChild(.....);