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

Error when using addChild twice in one function in 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>@diary/diary-page.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);
});

任何人都可以帮助解决这种奇怪的行为吗?谢谢

您的 getViewById 函数的代码是什么?看起来它没有 return DOM 元素,这就是为什么你不能在 scrollView.

上调用 addChild 的原因

在这里找到原因:How to nest elements in dynamically created ScrollView?

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

scrollview.content = stacklayout

注意:Only只能设置单个内容视图。这就是没有 addChild 方法的原因,因为它表明支持多个 children。"