Nativescript 是否可以在自定义组件中获取子级?

Nativescript 是否可以在自定义组件中获取子级?,nativescript,Nativescript,是否可以在自定义组件中获取子级 <myCustomComponent> <Label #id1 text="ID 1"></Label> <Label #id2 text="ID 2"></Label> </myCustomComponent> 有没有办法获取这两个标签的引用以将它们定位在我的自定义组件中的网格布局中?视图类有一个用于此目的的方法 <myCustomComponent>

是否可以在自定义组件中获取子级

<myCustomComponent>
    <Label #id1 text="ID 1"></Label>
    <Label #id2 text="ID 2"></Label>
</myCustomComponent>

有没有办法获取这两个
标签的引用
以将它们定位在我的自定义组件中的
网格布局
中?

视图类有一个用于此目的的方法

<myCustomComponent>
    <Label #id1 text="ID 1"></Label>
    <Label #id2 text="ID 2"></Label>
</myCustomComponent>
在my-component.xml中:

<StackLayout loaded="onLoaded">
    <Label id="label1" text="ID 1" mycustomattribute="Hi there."></Label>
    <Label id="label2" text="ID 2"></Label>
</StackLayout>

除了getViewById()选项外,还有几个选项可以查看布局的子项或其索引,还可以添加、删除、插入和重置子项

e、 g。 page.xml


page.ts

let page=args.object;
让stack=page.getViewById(“st”);
让label=page.getViewById(“lbl标题”);
console.log(“page.content:+page.content);//第页。内容:StackLayout@file:///app/main page.xml:19:5;
console.log(“stack.getChildrenCount():”+stack.getChildrenCount())//3
console.log(“stack.getChildAt(1):”+stack.getChildAt(1));//stack.getChildAt(1):按钮(5)@file:///app/main-page.xml:21:9;
console.log(“stack.getChildIndex(标签):”+stack.getChildIndex(标签));//0
console.log(“stack.parent:+stack.parent);//stack.parent:第(1)页@file:///app/main-page.xml:7:1;
var newlabel=新标签();
newlabel.text=“新标签”;
stack.addChild(newlabel);
有关所有支持的方法的详细信息

<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo">
    <StackLayout id="st" class="p-20">
        <Label id="lbl-title" text="Tap the button" class="h1 text-center"/>
        <Button text="TAP" tap="{{ onTap }}" class="btn btn-primary btn-active"/>
        <Label text="{{ message }}" class="h2 text-center" textWrap="true"/>
    </StackLayout>
</Page>
let page = <Page>args.object;
let stack = <StackLayout>page.getViewById("st");
let label = <Label>page.getViewById("lbl-title");

console.log("page.content: " + page.content); // page.content: StackLayout<st>@file:///app/main-page.xml:19:5;

console.log("stack.getChildrenCount(): " + stack.getChildrenCount()) // 3
console.log("stack.getChildAt(1): " + stack.getChildAt(1)); // stack.getChildAt(1): Button(5)@file:///app/main-page.xml:21:9;
console.log("stack.getChildIndex(label): " + stack.getChildIndex(label)); // 0

console.log("stack.parent: " + stack.parent); // stack.parent: Page(1)@file:///app/main-page.xml:7:1;

var newlabel = new Label();
newlabel.text = "new Label";
stack.addChild(newlabel);