“错误:根应该是 UIViewController 或 UIView";本机脚本 [英] "Error: Root should be either UIViewController or UIView" Nativescript

查看:19
本文介绍了“错误:根应该是 UIViewController 或 UIView";本机脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm trying to create a component inside of a new module, and then use it inside 'app', but I'm getting this error:

Error: Root should be either UIViewController or UIView

这是我的 sample.module.ts:

This is my 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() {
  }

}

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

Then inside of my app I'm importing the module like this:

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

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

The error messages fires when I try to use my SampleComponent selector inside of app.component.html like this:

<ns-sample></ns-sample>

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

I've been looking around, but I keep finding mistakes where people forgot to export the module itself. I'm already doing that, so I'm not sure what am I doing wrong.

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

So the question is how do I use a component declared in a different module inside of app?

提前谢谢各位!!

推荐答案

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

Since it's a custom component wrap it with a ContentView / StackLayout when you are placing it as root element.

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

这篇关于“错误:根应该是 UIViewController 或 UIView";本机脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆