tabview中的两个Listview不能按预期工作

时间:2019-05-31 18:25:23

标签: angular2-nativescript

我有一个代码共享Nativescript-Angular项目。我在延迟加载的模块中使用了两个ndigits。延迟加载的模块的结构使得当我从AppModule导航到该模块时。

ListView's

现在每个选项卡项都是一个<TabView id="tabViewContainer"> <page-router-outlet actionBarVisibility="never" *tabItem="{title: 'Players'}" name="playersTab"></page-router-outlet> <page-router-outlet actionBarVisibility="never" *tabItem="{title: 'Event Requests'}" name="eventRequestsTab"></page-router-outlet> </TabView> ,该路由会路由到一个子组件和该子组件 组件具有page-router-outlet作为父元素,如下所示。

ListView

现在我面临的问题是当两个列表视图并排显示时,有时两个列表视图均未显示任何内容,有时仅第二个选项卡显示了数据,而第一个选项卡仅显示了项目分隔符,如图所示下方:-

案例1标签-1 enter image description here

案例1标签2 enter image description here

案例2标签-1 Case 2-picture-1

案例2标签2 Case 2-picture-2

项目始终具有多个元素,但仍然会出现此问题。

我尝试使用功能<ListView class="list-group" [items]="players" (loaded)="onListViewLoaded($event)" #listView > <ng-template let-player="item" let-third="third"> <GridLayout class="list-group-item" rows="*" columns="auto, *"> <Image col="0" [src]="player.photoURL" class="thumb img-circle" ></Image> <StackLayout col="1"> <Label [text]="player.displayName" class="list-group-item-heading" ></Label> <Label [text]="player.uid" class="list-group-item-text" textWrap="true" ></Label> </StackLayout> </GridLayout> </ng-template> </ListView> ,但仍然没有成功。我还使用了listview.refresh()并在收到数据时调用了ChangedDetectionStratergy.OnPush函数。我曾经尝试使用markedForRefresh提供的ObservableArray,但在使用它时感到有些困惑,而且我觉得它不适用于代码共享项目。

从昨天开始,我感到沮丧,它看起来像是Nativescript中的错误。您能否帮助克服它,以便两个选项卡始终显示数据(知道数组始终至少包含一个元素)?

1 个答案:

答案 0 :(得分:0)

例如,两个listview在tabView内部工作:-

app.component.html:-

`<TabView androidTabsPosition="bottom">

<page-router-outlet *tabItem="{title: 'Home', iconSource: getIconSource('home')}"
    name="homeTab">
</page-router-outlet>

<page-router-outlet *tabItem="{title: 'Browse', iconSource: getIconSource('browse')}"
    name="browseTab">
</page-router-outlet>

</TabView>`

app.component.ts:-

`import { Component, OnInit } from "@angular/core";

 import { isAndroid } from "tns-core-modules/platform";

@Component({
selector: "ns-app",

moduleId: module.id,

templateUrl: "app.component.html"
})
export class AppComponent implements OnInit {

constructor() {
    // Use the component constructor to inject providers.
}

ngOnInit(): void {
    // Init your component properties here.
}

getIconSource(icon: string): string {
    const iconPrefix = isAndroid ? "res://" : "res://tabIcons/";

    return iconPrefix + icon;
}
}

` app.module.ts:-

`import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
 import { NativeScriptModule } from "nativescript-angular/nativescript.module";

import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";

@NgModule({
bootstrap: [
    AppComponent
],
imports: [
    NativeScriptModule,
    AppRoutingModule
],
declarations: [
    AppComponent
],
schemas: [
    NO_ERRORS_SCHEMA
]
})
export class AppModule { }

`

app.routing.ts:-

`import { NgModule } from "@angular/core";

import { Routes } from "@angular/router";

import { NSEmptyOutletComponent } from "nativescript-angular";

import { NativeScriptRouterModule } from "nativescript-angular/router";

const routes: Routes = [
{
    path: "",

    redirectTo: "/(homeTab:home/default//browseTab:browse/default)",

    pathMatch: "full"
},

{
    path: "home",

    component: NSEmptyOutletComponent,

    loadChildren: "~/app/home/home.module#HomeModule",

    outlet: "homeTab"
},
{
    path: "browse",

    component: NSEmptyOutletComponent,

    loadChildren: "~/app/browse/browse.module#BrowseModule",

    outlet: "browseTab"
}
];

@NgModule({

imports: [NativeScriptRouterModule.forRoot(routes)],

exports: [NativeScriptRouterModule]
})

export class AppRoutingModule { }

`

home.component.html:-

`<GridLayout class="page page-content">

<ListView [items]="items" class="list-group">

    <ng-template let-item="item">

        <ScrollView height="150">

            <Label [text]="item.name" class="list-group-item"></Label>

        </ScrollView>

    </ng-template>

</ListView>

</GridLayout>`

home.component.ts:-

`import { Component, OnInit } from "@angular/core";
import { DataService, IDataItem } from "../shared/data.service";

@Component({
selector: "Home",
moduleId: module.id,
templateUrl: "./home.component.html"
})
export class HomeComponent implements OnInit {

items: Array<IDataItem>;

constructor(private _itemService: DataService) { }

ngOnInit(): void {
    this.items = this._itemService.getItems();
}
}` 

home.module.ts:-

`import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";

import { NativeScriptCommonModule } from "nativescript-angular/common";

import { HomeRoutingModule } from "./home-routing.module";

import { HomeComponent } from "./home.component";

import { ItemDetailComponent } from "./item-detail/item-detail.component";

@NgModule({

imports: [
    NativeScriptCommonModule,
    HomeRoutingModule
],

declarations: [
    HomeComponent,
    ItemDetailComponent
],

schemas: [
    NO_ERRORS_SCHEMA
]

})

export class HomeModule { }`

home-routing.module.ts:-

`import { NgModule } from "@angular/core";

import { Routes } from "@angular/router";

import { NativeScriptRouterModule } from "nativescript-angular/router";

import { HomeComponent } from "./home.component";

import { ItemDetailComponent } from "./item-detail/item-detail.component";

const routes: Routes = [

{ path: "default", component: HomeComponent },

{ path: "item/:id", component: ItemDetailComponent }

];

@NgModule({

imports: [NativeScriptRouterModule.forChild(routes)],

exports: [NativeScriptRouterModule]

})

export class HomeRoutingModule { }`

browse.component.html:-

`<GridLayout class="page page-content">

<ListView [items]="items" class="list-group">

    <ng-template let-item="item">

        <ScrollView height="150">

            <Label [text]="item.name" class="list-group-item"></Label>

        </ScrollView>

    </ng-template>

</ListView>

</GridLayout>`

browse.component.ts:-

`import { Component, OnInit } from "@angular/core";
import { DataService, IDataItem } from "../shared/data.service";

@Component({
selector: "Browse",
moduleId: module.id,
templateUrl: "./browse.component.html"
})
export class BrowseComponent implements OnInit {

items: Array<IDataItem>;

constructor(private _itemService: DataService) { }

ngOnInit(): void {
    this.items = this._itemService.getItems();
}
}

`

browse.module.ts:-

`import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";

import { NativeScriptCommonModule } from "nativescript-angular/common";

import { BrowseRoutingModule } from "./browse-routing.module";

import { BrowseComponent } from "./browse.component";

import { DataService } from "../shared/data.service";

@NgModule({
imports: [
    NativeScriptCommonModule,
    BrowseRoutingModule
],
declarations: [
    BrowseComponent
],
schemas: [
    NO_ERRORS_SCHEMA
],
providers:[
    DataService
]
})
export class BrowseModule { }`

browse-routing.module.ts:-

`import { NgModule } from "@angular/core";

import { Routes } from "@angular/router";

import { NativeScriptRouterModule } from "nativescript-angular/router";

import { BrowseComponent } from "./browse.component";

const routes: Routes = [

{ path: "default", component: BrowseComponent }

];

@NgModule({

imports: [NativeScriptRouterModule.forChild(routes)],

exports: [NativeScriptRouterModule]

})

export class BrowseRoutingModule { }

`

示例在这里可用:- Nativescript playground url