首页 文章

“错误:Root应该是UIViewController或UIView”Nativescript

提问于
浏览
0

所以我正在尝试在新模块中创建一个组件,然后在'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 { }

然后我的sampleComponent:

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>

我一直在四处寻找,但我一直在发现错误,人们忘记了导出模块本身 . 我已经这样做了,所以我不确定我做错了什么 .

所以问题是如何在app中使用在不同模块中声明的组件?

先谢谢你们!!

1 回答

  • 1

    因为它是一个自定义组件,当您将其作为根元素放置时,它将使用ContentView / StackLayout进行包装 .

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

相关问题