我使用SegmentedBar作为标签。我的 menu.html 文件如下所示:
<StackLayout orientation="vertical" width="310" height="610">
<nav>
<h1 class="title">Hello World</h1>
<SegmentedBar #tabs [items]="items" [selectedIndex]="selectedIndex" ></SegmentedBar>
</nav>
</StackLayout>
这是我的 menu.component.ts 文件:
import {Component, OnInit, OnDestroy, AfterViewInit, ViewChild, ElementRef} from '@angular/core';
import {Page} from 'ui/page';
import {SegmentedBar, SegmentedBarItem, SelectedIndexChangedEventData} from 'ui/segmented-bar';
@Component({
selector: 'tabs',
templateUrl:"./components/menu/menu.html"
})
export class TabsComponent implements OnInit, OnDestroy, AfterViewInit {
selectedIndex: number;
items: Array<any>;
@ViewChild("tabs") tabs: ElementRef; // equal to getViewById() in NativeScript core
constructor(private page: Page) {
this.selectedIndex = 0;
this.items = [{ title: 'Home' }, { title: 'G+' }, { title: 'Sync' }];
}
ngOnInit() {
}
ngAfterViewInit() {
this.tabs.nativeElement.on(SegmentedBar.selectedIndexChangedEvent, (args: SelectedIndexChangedEventData) => {
switch (args.newIndex) {
case 0:
console.log('Home selected')
break;
case 1:
console.log('G+ selected')
break;
case 3:
console.log('Sync selected')
break;
}
})
}
ngOnDestroy() { }
}
答案 0 :(得分:0)
你不能在NativeScript中使用DOM意味着你不能使用浏览器特定的标签,如h1,ul,li,div等。如果你想要自己的标签你可以用integrating third-party components来实现。
分段栏与TabView不同 - 它不提供内容空间,但仅提供分段栏。你应该做的是使用 selectedIndex 来修改你的页面。
这是一个关于如何根据用户选择更改内容的非常简单的实现(它仅用于演示 - 有许多更好的解决方案。例如,使用部分视图也称为自定义组件,并为可见性声明一个布尔变量变化)
<强> app.component.html 强>
<StackLayout orientation="vertical" width="310" height="610">
<SegmentedBar #tabs [items]="items" [selectedIndex]="selectedIndex" ></SegmentedBar>
<GridLayout #firstTabContent width="300" height="500" backgroundColor="orange" fontSize="40">
<Label text="FIRST" textWrap="true"></Label>
</GridLayout>
<GridLayout #secondTabContent width="300" height="500" backgroundColor="red" fontSize="40">
<Label text="SECOND" textWrap="true"></Label>
</GridLayout>
<GridLayout #thirdTabContent width="300" height="500" backgroundColor="green" fontSize="40">
<Label text="THIRD" textWrap="true"></Label>
</GridLayout>
</StackLayout>
<强> app.component.ts 强>
import {Component, OnInit, OnDestroy, AfterViewInit, ViewChild, ElementRef} from '@angular/core';
import {Page} from 'ui/page';
import {SegmentedBar, SegmentedBarItem, SelectedIndexChangedEventData} from 'ui/segmented-bar';
@Component({
selector: 'tabs',
templateUrl: 'app.component.html'
})
export class TabsComponent implements OnInit, OnDestroy, AfterViewInit {
selectedIndex: number;
items: Array<any>;
messages: Array<any>;
@ViewChild("tabs") tabs: ElementRef;
@ViewChild("firstTabContent") firstTabContent: ElementRef;
@ViewChild("secondTabContent") secondTabContent: ElementRef;
@ViewChild("thirdTabContent") thirdTabContent: ElementRef;
constructor(private page: Page) {
this.selectedIndex = 0;
this.items = [{ title: 'First' }, { title: 'Second'}, { title: 'Third'}];
}
ngOnInit() {
// the initial load setup
this.firstTabContent.nativeElement.visibility = "visible";
this.secondTabContent.nativeElement.visibility = "collapsed";
this.thirdTabContent.nativeElement.visibility = "collapsed";
}
ngAfterViewInit() {
this.tabs.nativeElement.on(SegmentedBar.selectedIndexChangedEvent, (args: SelectedIndexChangedEventData) => {
switch (args.newIndex) {
case 0:
console.log('first selected');
this.firstTabContent.nativeElement.visibility = "visible";
this.secondTabContent.nativeElement.visibility = "collapsed";
this.thirdTabContent.nativeElement.visibility = "collapsed";
break;
case 1:
console.log('second selected')
this.firstTabContent.nativeElement.visibility = "collapsed";
this.secondTabContent.nativeElement.visibility = "visible";
this.thirdTabContent.nativeElement.visibility = "collapsed";
break;
case 2:
console.log('third selected')
this.firstTabContent.nativeElement.visibility = "collapsed";
this.secondTabContent.nativeElement.visibility = "collapsed";
this.thirdTabContent.nativeElement.visibility = "visible";
break;
}
})
}
ngOnDestroy() { }
}