2016-09-30 3 views
1

나는 A A nativescript 사용자 지정 구성 요소 http://moduscreate.com/custom-components-in-nativescript/을 만들려면이 가이드를 따라했지만 내가 폴더를이 주요라는 내부 폴더와 페이지이 나Nativescript 사용자 정의 구성 요소

을 위해 작동하지 않습니다. 주요 파일

main.html의 몇 가지를 가지고

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

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; 
} 
} 

나는 또한 메인 common.css이 그러나 그것은 중요하지 않습니다. 그때 나는 내가 무엇을

<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.html

그 안에 하나의 파일로 안녕하세요라는 페이지 안에 다른 폴더가 빈 화면 만 나타납니다. 나는 또한이 라인을 변경하려고 시도했다 xmlns:hello="pages/helllo"hello.html이 파일에 xmlns:hello="../helllo"하지만 아무것도 얻지 못했습니다 심지어 오류. 누군가 내가 잘못하고있는 것을 지적 할 수 있습니까?

+0

사용자 정의 구성 요소 당신이 그것을 같은 각도 공유 일 필요 각도 만 바닐라 NS에서 작동하지 않도록 뭐죠 난 바닐라 NS 또는 각도를 사용하는 것이 좋습니다 어떤 각도 –

+0

를 사용하지 않는로 불리는이? –

+0

각각 장단점이 있습니다. 동일한 문제가있는 바닐라를 사용하고 있습니다. NS로 시작했지만 ng2가 익숙하거나 웹 사이트의 로직을 재사용해야하는 경우 오류가 발생하는 더 많은 것들이 있습니다. –

답변

5

NativeScript 코어에서는 유효하지만, 에서는 작동하지 않습니다. NativeScript + Angular-2.

대신 사용자 정의 구성 요소 인 각도 2를 만드는 것이 필요합니다. 사용자 지정 항목 구성 요소가 생성되는 this sample을 참조 할 수 있음을 보여줍니다. 이 예제는 documentation에도 설명되어 있으며이 구성 요소의 @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 }]; 
    } 
} 

사용하여 항목-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를 선언하는 것을 잊지 않는 것입니다!

메인.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 { } 
+0

soo를 완벽하게 작동시켜 주셔서 감사합니다.하지만 item-item을 item-template.component.ts를 사용하는 별도의 파일에 넣으려면 어떻게해야할까요? –

+0

나는 그것을 알아 냈다. D는 대답했다. upvoted :) –

관련 문제