【问题标题】:NativeScript angular nativeView undefinedNativeScript 角度 nativeView 未定义
【发布时间】:2019-05-13 22:09:53
【问题描述】:

我正在尝试从 NativeScript Angular 获取 StackLayout nativeView,但总是返回 undefined。我试过这样:

html:

<StackLayout id="stackLayout" #stackLayout> </StackLayout>

TS:

ngAfterViewInit() {
    setTimeout(() => {
        let container: StackLayout = this.page.getViewById("stackLayout");
        console.log(container.nativeView); 
        console.log(this.stackLayout.nativeElement.nativeView);
    }, 100)
}

请给我建议。

【问题讨论】:

    标签: nativescript nativescript-angular


    【解决方案1】:

    使用视图本身的loaded 事件确保nativeView 准备就绪。

    HTML

    <StackLayout (loaded)="onLoaded($event)"></StackLayout>
    

    TS

    onLoaded(event: EventData) {
      const stackLayout = <StackLayout>event.object;
      console.log(stackLayout.nativeView);
    }
    

    【讨论】:

    • 感谢您的回复。它似乎有效,但我正在尝试将android.webkit.WebView 附加到该视图。我正在尝试这样:stackLayout.nativeView.addView(this.agent.getWebCreator().getWebView()),但出现错误:JS: ERROR Error: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 。我该怎么做?
    • 我不确定你的目的是什么,但如果你想注入原生视图,你应该使用Placeholder
    • 非常感谢!! Placeholder 解决了我的问题 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 2021-04-20
    • 2015-05-22
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多