Nativescript自定义组件

Oma*_*sam 2 android typescript nativescript angular

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

我有一个文件夹页面,里面有一个名为main的文件夹. main有几个文件

main.html中

<StackLayout 
xmlns="http://schemas.nativescript.org/tns.xsd"
xmlns:hello="pages/helllo"
loaded="pageLoaded" >
  <hello:hello/>
</StackLayout>
Run Code Online (Sandbox Code Playgroud)

main.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;
 }  
} 
Run Code Online (Sandbox Code Playgroud)

而且我也有main-common.css,但这并不重要.然后我在页面中有另一个名为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>
Run Code Online (Sandbox Code Playgroud)

然而,无论我做什么,你好组件都没有显示我只是得到一个空屏幕.我也试图改变这条线xmlns:hello="pages/helllo"hello.html的文件,这xmlns:hello="../helllo"可是我没有得到任何东西,甚至不是一个错误.谁能指出我做错了什么?

Nic*_*iev 10

您在NativeScript Core中所指的有效但在NativeScript + Angular-2无效.

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

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

1.)创建自定义组件

利用项目,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 }];
    }
}
Run Code Online (Sandbox Code Playgroud)

利用项目,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>
Run Code Online (Sandbox Code Playgroud)

最后也是至关重要的部分是不要忘记在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 { }
Run Code Online (Sandbox Code Playgroud)