Javascript 在NativeScript中的一个函数中使用addChild两次时出错

Javascript 在NativeScript中的一个函数中使用addChild两次时出错,javascript,nativescript,Javascript,Nativescript,我正在尝试动态创建stackLayouts并将其添加到现有scrollView,但出现错误: JS ERROR TypeError: scrollView.addChild is not a function. (In 'scrollView.addChild(stackLayout)', 'scrollView.addChild' is undefined) 但通过使用console.log,它可以同时看到这两种情况: StackLayout(10) ScrollView<dates&

我正在尝试动态创建stackLayouts并将其添加到现有scrollView,但出现错误:

JS ERROR TypeError: scrollView.addChild is not a function. (In 'scrollView.addChild(stackLayout)', 'scrollView.addChild' is undefined)
但通过使用console.log,它可以同时看到这两种情况:

StackLayout(10) ScrollView<dates>@diary/diary-page.xml:4:7;

有人能帮我对付这种奇怪的行为吗?谢谢

您的
getViewById
函数的代码是什么?它看起来没有返回DOM元素,这就是为什么不能在
scrollView
上调用
addChild
函数的代码是什么?它看起来没有返回DOM元素,这就是为什么不能在
scrollView
上调用
addChild
的原因

“ScrollView实际上是ContentView的植入,因此设置其内容的正确方法是:

scrollview.content=stacklayout

注意:只能设置一个内容视图。这就是没有addChild方法的原因,因为它建议支持多个子对象。“

在此处找到原因:

“ScrollView实际上是ContentView的植入,因此设置其内容的正确方法是:

scrollview.content=stacklayout


注意:只能设置一个内容视图。这就是为什么没有addChild方法,因为它建议支持多个子元素。”

Hi@luser是在NativeScript中获取元素的标准函数,当我执行console.log时,它会看到它ScrollView@diary/xml:4:7;Hi@luser是在NativeScript中获取元素的标准函数,当我执行console.log时,它会看到它ScrollView@diary/xml:4:7;
input.forEach(function(entry) {
    const scrollView = page.getViewById("dates");
    var stackLayout = new StackLayout();
    stackLayout.className = 'date';
    var label = new Label();
    label.text = entry.Date;
    stackLayout.addChild(label);
    console.log(stackLayout + ' ' + scrollView);
    scrollView.addChild(stackLayout);
});