【问题标题】:VueJS - How to get data and pass this immediately after create and send to firebase?VueJS - 如何在创建并发送到firebase后立即获取数据并传递它?
【发布时间】:2017-08-19 20:07:24
【问题描述】:

我正在尝试获取数据并在发送到 firebase 后立即显示。但是,我只是在渲染或重新加载页面后才获得已知数据。我想在发送后显示/显示数据,而不必重新加载页面。我尝试使用 mount 和 created 生命周期钩子发出 get 请求,但同样,只显示组件是否被重新渲染或重新加载。

这是我的代码:

<template>
    <div class="container">
        <div class="row">
            <div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
                <h1>Http</h1>

                <div class="form-group">
                    <label for="name">Username:</label>
                    <input type="text" name="name" v-model="user.username">
                </div>

                <div class="form-group">
                    <label for="email">email:</label>
                    <input type="email" name="email" v-model="user.email">
                </div>

                <button class="btn btn-success" @click="sendData">Send data</button>
            </div>

            <div>
                <ul v-for="u in users">
                    <li>{{u.username}}</li>
                    <li>{{u.email}}</li>
                    <hr>
                </ul>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        data(){
            return{
                user:{
                    username:'',
                    email:''
                },
                users:[]
            }
        },
        methods:{
            sendData(){
                this.$http.post('FIREBASE-LINK/data.json', this.user).then(response=>{
                    console.log(response)
                }).catch(err=>{
                    console.log(err)
                })
            }
        },
        mounted(){
            this.$http.get('FIREBASE-LINK/data.json').then(resp=>{
                this.users = resp.body
            }).catch(err=>{
                console.log(err)
            })
        }
    }
</script>

<style>
</style>

【问题讨论】:

    标签: javascript firebase vue.js vuejs2


    【解决方案1】:

    mounted,顾名思义,仅在组件创建期间被调用(Vue.js API 文档说“在刚刚挂载 el 被新创建的 vm.$el 替换的实例后调用”)。由于您希望在将数据发送到数据库后显示数据,因此您的函数调用应该在您的 firebase 代码完成后立即执行。

    methods:{
        sendData(){
            this.$http.post('FIREBASE-LINK/data.json', this.user).then(response=>{
                console.log(response)
                this.getData()
            }).catch(err=>{
                console.log(err)
            })
        },
        getData: function () {
            this.$http.get('FIREBASE-LINK/data.json').then(resp=>{
                this.users = resp.body
            }).catch(err=>{
                console.log(err)
            })
        }
    }
    

    【讨论】:

    • 谢谢。我以前试过这个,但没有什么,因为我没有使用“this.getData()”,我只尝试了“getData()”之类的东西。为什么在这种情况下我们应该使用“this”?
    • 你使用this是因为getData被Vue暴露在组件的对象下。与data 返回的值一样,您可以通过this 访问它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多