【问题标题】:ListView istead of *ngForListView 而不是 *ngFor
【发布时间】:2021-05-08 07:25:09
【问题描述】:

我在 NativeScript 中使用 ScrollView 作为 Chat-Overview。

到目前为止,我在 Scrollview 中使用了这段代码:

<ScrollView #scrollLayout style="height: 80%;margin-top: -200px">

   <StackLayout class="chat-body">
        <StackLayout *ngFor="let message of messages"  orientation="vertical">

                <Label *ngIf="message.sender == me" horizontalAlignment="right" class="chat-message-me" [text]="message.msg"></Label>
                <Label *ngIf="message.sender == me" horizontalAlignment="right" class="chat-timestamp" [text]="message.time"></Label>
            
                <Label *ngIf="message.sender != me" horizontalAlignment="left" class="chat-message-you" [text]="message.msg"></Label>
                <Label *ngIf="message.sender != me" horizontalAlignment="left" class="chat-timestamp" [text]="message.time"></Label>
        
        </StackLayout> 
    </StackLayout>

</ScrollView>

它起作用了,但是当我向消息数组添加一个新元素时,视图会将完整的数组加载到已经显示的数组之上。

消息数组:

聊天视图:

所以我的解决方案是使用 ListView,如下所示:

<ScrollView #scrollLayout style="height: 80%;margin-top: -200px">
    <GridLayout class="chat-body">
        <ListView [items]="messages" orientation="vertical">
            <ng-template let-item="message">
                
                <Label *ngIf="message.sender == me" horizontalAlignment="right" class="chat-message-me" [text]="message.msg"></Label>
                <Label *ngIf="message.sender == me" horizontalAlignment="right" class="chat-timestamp" [text]="message.time"></Label>
            
                <Label *ngIf="message.sender != me" horizontalAlignment="left" class="chat-message-you" [text]="message.msg"></Label>
                <Label *ngIf="message.sender != me" horizontalAlignment="left" class="chat-timestamp" [text]="message.time"></Label>

                
            </ng-template>
        </ListView>
   </GridLayout>
</ScrollView>

但我得到这个错误:

System.err: An uncaught Exception occurred on "main" thread.
System.err: Calling js method getView failed
System.err: Error: No suitable views found in list template! Nesting level: 0
System.err:

【问题讨论】:

    标签: listview nativescript scrollview


    【解决方案1】:

    ListView 会自动滚动,因此您不需要 ScrollView 父级。

    您看到的错误是因为 ListView 在其中需要一个组件/布局(您有 4 个标签)。

    要修复错误,您可以将标签包装在 ContentView 或类似这样的布局中:

    <ListView [items]="messages" orientation="vertical">
      <ng-template let-message="item">
        <ContentView>
          <Label *ngIf="message.sender == me" horizontalAlignment="right" class="chat-message-me" [text]="message.msg"></Label>
          <Label *ngIf="message.sender == me" horizontalAlignment="right" class="chat-timestamp" [text]="message.time"></Label>      
          <Label *ngIf="message.sender != me" horizontalAlignment="left" class="chat-message-you" [text]="message.msg"></Label>
          <Label *ngIf="message.sender != me" horizontalAlignment="left" class="chat-timestamp" [text]="message.time"></Label>
        </ContentView>
      </ng-template>
    </ListView>
    

    但是,在 ListView 中使用 ngIfs 会对性能产生一些影响。如果可能,请改用多个项目模板。这个blog post 对这两种方法进行了一些深入的比较。

    【讨论】:

    • 非常感谢您的回答,但如果我这样做,标签将无法识别姓名和发件人。此错误:“TypeError:无法读取未定义的属性“名称””和“TypeError:无法读取未定义的属性“发件人””
    • 我认为未定义的错误是因为let-item。尝试将其更改为let-message="item"。 @SwaX
    猜你喜欢
    • 1970-01-01
    • 2013-09-19
    • 2014-04-28
    • 2013-06-15
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    相关资源
    最近更新 更多