首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义组件

自定义组件
EN

Stack Overflow用户
提问于 2016-09-30 14:05:52
回答 1查看 2.3K关注 0票数 2

我按照本指南创建了一个本地文本自定义组件http://moduscreate.com/custom-components-in-nativescript/,但它不适用于我

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

main.html

代码语言:javascript
复制
<StackLayout 
xmlns="http://schemas.nativescript.org/tns.xsd"
xmlns:hello="pages/helllo"
loaded="pageLoaded" >
  <hello:hello/>
</StackLayout>

main.component.ts

代码语言:javascript
复制
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;
 }  
} 

我也有main-common.css,但这并不重要。然后,我在页面中有另一个文件夹,名为hello,其中只有一个文件。

hello.html

代码语言:javascript
复制
<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文件中的这一行hello.html文件更改为这个xmlns:hello="../helllo",但没有得到任何东西,甚至连一个错误都没有。有人能指出我做错了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-30 14:32:44

您所指的在NativeScript核心中是有效的,但在NativeScript +角2中不起作用。

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

让我指导你完成这个过程。

1.)创建自定义组件

using-item-template.component.ts

代码语言:javascript
复制
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 }];
    }
}

using-item-template.component.html

代码语言:javascript
复制
<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>

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

main.ts

代码语言:javascript
复制
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 { }
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39793421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档