【问题标题】:Nativescript Custom ComponentsNativescript 自定义组件
【发布时间】:2016-09-30 14:05:52
【问题描述】:

我正在按照本指南创建一个 nativescript 自定义组件 http://moduscreate.com/custom-components-in-nativescript/,但它对我不起作用

我有一个文件夹 pages,里面有一个名为 ma​​in 的文件夹。 ma​​in 有几个文件

ma​​in.html

<StackLayout 
xmlns="http://schemas.nativescript.org/tns.xsd"
xmlns:hello="pages/helllo"
loaded="pageLoaded" >
  <hello:hello/>
</StackLayout>

ma​​in.component.ts

import { Component, ElementRef, OnInit, ViewChild} from "@angular/core";
import { Page } from "ui/page";
import colorModule = require("color");
var Color = colorModule.Color;
@Component({
selector: "my-app",
templateUrl: "pages/main/main.html",
styleUrls: ["pages/main/main-common.css"]
})    
export class MainComponent implements OnInit{
      constructor(private page: Page) {
  }    

  ngOnInit() {
    this.page.actionBarHidden = true;
 }  
} 

我也有 ma​​in-common.css 但这并不重要。然后我在 pages 中有另一个名为 hello 的文件夹,其中只有一个文件

hello.html

<StackLayout width="100%" height="100%" backgroundColorr="red">
  <Label class ="h1" text="h1 hello world" color="black"></Label>
  <Label class ="h1" text="h1 hello world" color="black"></Label>
  <Label class ="h1" text="h1 hello world" color="black"></Label>
  <Label class ="h1" text="h1 hello world" color="black"></Label>
  <Label class ="h1" text="h1 hello world" color="black"></Label>
  <Label class ="h1" text="h1 hello world" color="black"></Label>
  <Label class ="h1" text="h1 hello world" color="black"></Label>
  <Label class ="h1" text="h1 hello world" color="black"></Label>
  <Label class ="h1" text="h1 hello world" color="black"></Label>
  <Label class ="h1" text="h1 hello world" color="black"></Label>
  <Label class ="h1" text="h1 hello world" color="black"></Label>
</StackLayout>

但是,无论我做什么,hello 组件都没有显示,我只是得到一个空屏幕。我还尝试将 hello.html 文件中的这一行 xmlns:hello="pages/helllo" 更改为 xmlns:hello="../helllo",但我什至没有得到任何错误消息。有人能指出我做错了什么吗?

【问题讨论】:

  • 自定义组件仅在原版 NS 中起作用,您需要将它作为 Angular 共享的东西,不确定它叫什么,因为我不使用 Angular
  • 使用 vanilla NS 和 angular 哪个更好?
  • 每个都有优点和缺点,我使用香草,因为我有同样的问题和更多的事情,当我开始使用 NS 时会导致错误,但如果你熟悉 ng2 或需要重用它,它会更好网站逻辑

标签: android angular typescript nativescript


【解决方案1】:

您所指的内容在 NativeScript Core 中有效,但在 NativeScript + Angular-2 中不起作用。

您需要以 Angular-2 方式创建自定义组件。 为了演示,我们可以参考this sample创建自定义item组件的地方。该示例也在documentation 中进行了描述,它还将向您展示如何使用该组件的@Input 指令绑定数据。

让我指导您完成整个过程。

1.) 创建您的自定义组件

using-item-template.component.ts

import { Component, ChangeDetectionStrategy, Input }  from "@angular/core";

@Component({
    selector: 'item-component',
    styleUrls: ["listview/using-item-template/using-item-template.component.css"],
    template: `
        <StackLayout *ngFor="let element of data.list" class="model">
            <Label [text]="element.model" class="name"></Label>
            <Label [text]="element.speed +'mph'" class="speed"></Label>
        </StackLayout>
    `
})
export class ItemComponent {
    @Input() data: any; // this way we "pass data" to our item-component
}

@Component({
    selector: 'using-item-template',
    styleUrls: ["listview/using-item-template/using-item-template.component.css"],
    templateUrl: "listview/using-item-template/using-item-template.component.html",
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class UsingItemTemplateComponent {
    public manufacturers: Array<any>;

    constructor() {
        var bugatti = [{ "model": "Bugatti Chiron", "speed": "261" }, { "model": "Bugatti Veyron Super Sport", "speed": "268" }];
        var mclaren = [{ "model": "McLaren P1", "speed": "211" }, { "model": "McLaren F1", "speed": "242" }];
        var jaguar = [{ "model": "Jaguar XJ220", "speed": 217 }];
        this.manufacturers = [{ "list": bugatti }, { "list": mclaren }, { "list": jaguar }];
    }
}

使用-item-template.component.html

<StackLayout exampleTitle toggleNavButton>
    <GridLayout rows="50, *" class="example-container">
        <Label text="Top Cars" row="0" class="title" textWrap="true" horizontalAlignment="center"></Label>
        <ListView [items]="manufacturers" row="1">
            <template let-item="item">
                <item-component [data]="item" ></item-component>
            </template>
        </ListView>
    </GridLayout>
</StackLayout>

最后也是关键的部分是不要忘记在 NgModule 中声明您的 ItemComponent!

main.ts

import { ItemComponent } from "./listview/using-item-template/using-item-template.component";

@NgModule({
    declarations: [
        ItemComponent, // declare the item component
        // the other components in your app
    ],
    bootstrap: [AppComponent],
    imports: [
        .....
    ],
})
class AppComponentModule { }

【讨论】:

  • 非常感谢,这非常有效,但我想知道如果我想将 ItemComponent 放在一个单独的文件中,而不是 using-item-template.component.ts 怎么办?
  • 没关系,我想通了 :D 回答并赞成 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多