【问题标题】:"Error: Root should be either UIViewController or UIView" Nativescript“错误:根应该是 UIViewController 或 UIView” Nativescript
【发布时间】:2019-05-10 08:41:05
【问题描述】:

所以我试图在一个新模块中创建一个组件,然后在“app”中使用它,但是我收到了这个错误:

Error: Root should be either UIViewController or UIView

这是我的 sample.module.ts:

import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
import { NativeScriptCommonModule } from 'nativescript-angular/common';
import { SampleComponent } from './sample/sample.component';

@NgModule({
  declarations: [SampleComponent], // <= this is the component I want to use
  exports: [SampleComponent],
  imports: [
    NativeScriptCommonModule
  ],
  schemas: [NO_ERRORS_SCHEMA]
})
export class SampleModule { }

然后是我的示例组件:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'ns-sample',
  templateUrl: './sample.component.html',
  styleUrls: ['./sample.component.css'],
  moduleId: module.id,
})
export class SampleComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

然后在我的应用程序中,我正在像这样导入模块:

imports: [
    // ... other modules
    SampleModule,
],

当我尝试像这样在 app.component.html 中使用 SampleComponent 选择器时,会触发错误消息:

<ns-sample></ns-sample>

我一直在环顾四周,但我不断发现人们忘记导出模块本身的错误。我已经这样做了,所以我不确定我做错了什么。

所以问题是如何使用在应用程序内部的不同模块中声明的组件?

提前谢谢大家!!

【问题讨论】:

    标签: angular nativescript angular-components angular-module


    【解决方案1】:

    因为它是一个自定义组件,所以当您将它作为根元素放置时,用 ContentView / StackLayout 包装它。

    <StackLayout>
      <ns-sample></ns-sample>
    </StackLayout>
    

    【讨论】:

    • 天哪!那行得通!非常感谢。我要疯了,想弄清楚
    • 我也在关注一个教程,但它有点过时了。你知道有什么有用的吗?或者有什么工具可以帮助我开始使用 nativescript?
    猜你喜欢
    • 2013-04-05
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 2012-05-23
    • 2011-12-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多