nativescript vueのprops値に基づいて配列をフィルタリングするにはどうすればよいですか?

Nov 24 2020

私は先週nativescriptvueをピックアップし、課題の一部として、sails jsでWebアプリケーションを転送してモバイルアプリケーションを作成し、nativescriptプレビューでプレビューしています。app.jsで定義された配列であるグローバル変数global.mallsがあり、2番目のタブでそれに基づいてリストビューを作成しています。

                <TabContentItem>
                    <ListView for="mall in malls" @itemTap="onSecondTab"
                        style="height:1250px">
                        <v-template>
                            <StackLayout orientation="vertical" height="350">
                                <Label :text="mall.mall" class="h2" />
                            </StackLayout>
                        </v-template>
                    </ListView>
                </TabContentItem>

アイテムをタップすると、このモール内のショップを一覧表示するはずの2ページ目に移動します。メソッドの使用

    onSecondTab: function(args) {
                    console.log("Item with index: " + args.index + " tapped");
                    console.log("Mall tapped: " + args.item.mall);
    
                    this.$navigateTo(MallShop, {
                        transition: {},
                        props: {
                            tappedProduct: args.item
                        }
                    });
                }

この2ページ目で、これらのショップのリストを作成したいのですが、このtappedProduct.mall(タップされたモール名を表す)に基づいて既存のショップの配列をフィルタリングするのに問題があります。計算を使用し、mountd()ライフサイクルフックを作成して既存の配列をフィルタリングして表示しようとしましたが(エクスポートのデフォルト内)、いずれも機能しません。クーポンは、Webアプリケーションへのフェッチ呼び出しによって埋められる空の配列です。ショップは、クーポンからの結果をフィルタリングするために私が実験していた空の配列でもあります。


    <template>
        <Page>
            <ListView for="shop in shopsPresent" style="height:1250px">
                <v-template>
                    <StackLayout orientation="vertical" height="350">
                        <Label :text="shop.restaurant" class="h2" />
                    </StackLayout>
                </v-template>
            </ListView>
        </Page>
    </template>

    async mounted() {
                var response = await fetch(global.baseUrl + "/shop/json");
    
                if (response.ok) {
                    this.coupons = await response.json();
                    console.log(JSON.stringify(this.coupons));
                } else {
                    console.log(response.statusText);
                }

                this.shops = this.coupons.filter(function (p) {
                    return p.mall == "tappedProduct.mall";
                });

            },

    computed: {
                shopsPresent: function() {
                    return this.coupons.filter(function(p) {
                        return p.mall == "tappedProduct.mall";
                    });
                }
            },

回答

YHStan Nov 25 2020 at 19:47

ここのstackoverflowに関する同様の投稿の1つに基づいて、私が間違っていることを見つけました https://stackoverflow.com/a/53190799/12181863

これが私の新しいコードです。

async mounted() {
        var response = await fetch(global.baseUrl + "/shop/json");

        if (response.ok) {
            this.coupons = await response.json();
            console.log(JSON.stringify(this.coupons));
        } else {
            console.log(response.statusText);
        }

        this.shops = this.coupons.filter(
            function(p) {
                console.log(this.tappedProduct.mall);
                return p.mall == this.tappedProduct.mall;
            }.bind(this)
        );
    },