【问题标题】:Cannot pass the fetch method information to the instance data无法将 fetch 方法信息传递给实例数据
【发布时间】:2019-03-11 22:36:59
【问题描述】:

当我运行以下代码时,currencie 变量内的值不会显示。

我无法将 fetch 方法信息传递给实例数据。我想获取数据并将其显示在屏幕上。如果我使用console.log,我可以看到预期的数据。

 <template>
        <Page class="page">
            <ActionBar title="Tela N°1" class="action-bar" />
            <ScrollView>
                <StackLayout class="home-panel">
                    <!--Add your page content here-->
                    <Label textWrap="true" text="Primeira tela criada usando NativeScript"
                        class="h2 description-label" />
                    <Button text="Segunda Tela" @tap="onButtonTap" />
                    <Button text="Terceira Tela" @tap="onButton" />
                    <ListView class="list-group" for="currencie in currenciess"
                        style="height:1250px">
                        <v-template>
                            <FlexboxLayout flexDirection="row" class="list-group-item">
                                <Label :text="currencie.name" class="list-group-item-heading" style="width: 60%" />
                                <Label :text="currencie.buy" class="list-group-item-heading"
                                    style="width: 60%" />
                            </FlexboxLayout>
                        </v-template>
                    </ListView>
    </StackLayout>
            </ScrollView>
        </Page>
    </template>

    <script>
        import Second from "./Second";
        import Third from "./Third";
        const http = require("tns-core-modules/http");
        const url = "https://api.hgbrasil.com/finance?key=c5239f9c";
        export default {
            data() {
                return {
                    currenciess: []
                };
            },                    
    mounted() {
                fetch(url)
                    .then(response => response.json())
                    .then(results => {
                        this.currenciess = results.results.currencies.USD;
                    });
            },
        };
    </script>

【问题讨论】:

    标签: vue.js nativescript


    【解决方案1】:

    更改您的挂载方法。 currenciess 是一个数组,而您的 results.results.currencies["USD"] 是一个来自 API 的对象。我为你创建了一个游乐场here

    mounted() {
                console.log("mounted");
                fetch(url)
                    .then(response => response.json())
                    .then(results => {
                        this.currenciess.push(results.results.currencies["USD"]);
                    });
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      • 2020-07-29
      相关资源
      最近更新 更多