【问题标题】:Issue displaying updated data in Vue component after axios POSTaxios POST 后在 Vue 组件中显示更新数据的问题
【发布时间】:2019-11-23 03:41:29
【问题描述】:

我遇到了一个问题,希望 Javascript Jedi 能帮助我指明正确的方向。

问题范围:

我将 Laravel 集合传递给我的 Vue 组件。在组件内部,我正在遍历集合并通过 axios 提交表单。表单提交,数据在数据库中更新,但是__我不清楚如何在不刷新页面的情况下显示更新的值。__

预期结果:

表单提交后,更新后的数据会反映在Vue模板内的{{ collection.value }}

出了什么问题:

数据库中的数据正在更新,但{{ collection.value }} 保持不变,直到重新加载页面。

Web.php

Route::post('/updateQty', 'MyController@update');

我的控制器

public function update(Request $request)
{
    $product = Product::where('id', $request->productId)
        ->update([ 'qty' => $request->qty ]);

    return response()->json($product);
}


public function index()
{

    $collection = DB::table('products')->get();

    return view('my-blade', [
        'collections' => $collection,
    ]);

}

$collection 存储在数据库中的结构

'qty' => decimal(8,2),
'class' => varchar(255),
'description' => varchar(255),
'value' => decimal(8,2),
'productId' => int(11)

我的刀片

<my-component :collections="{{ $collections }}"></my-component>

MyComponent.vue

<template>
    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <table class="table table-sm">
                    <div v-if="collections.length">
                        <tr v-for="collection in collections" v-bind:key="collection.id">
                            <td>
                                <form @submit="updateQty">
                                <input type="hidden" id="productId" :value="collection.productId" name="productId">
                                    <select class="form-control" name="qty" id="qty" @change="updateQty">
                                        <option :value="collection.qty">{{ collection.qty }}</option>
                                        <option v-for="(x, index) in 200" v-bind:key="index" :value="index">{{ index }}</option> 
                                </select>
                                </form>
                            </td>
                            <td>{{ collection.value }}</td>
                        </tr>
                    </div>
                </table>
            </div>
        </div>
    </div>
</template>


<script>

export default {
    props: ['collections'],

    data() {
        return {
            qty: '',
        }
    }

    mounted() {
        console.log('MyComponent.vue mounted successfully');
    },

    methods: {

        updateQty(e) {
            e.preventDefault();

            let currentObj = this;
            let url = '/updateQty';

            axios.post(url, {
                qty: qty.value,
            })

            .then(function (response) {
                currentObj.value = (response.data);
                let collections = response.data;
            })
        },

    }
}
</script>

App.js

Vue.component('my-component', require('./components/MyComponent.vue'));

我确信这很简单,但对于我的一生,我无法理解它。非常感谢您!

【问题讨论】:

    标签: javascript laravel vue.js


    【解决方案1】:

    你只需要稍微修改一下你的脚本。

    首先,将 collections 属性保存到 data 属性中,否则当您尝试更新它时 Vue 会尖叫。为此,我会将传入的prop 重命名为collections_prop。然后将其保存到collections 数据属性。

    然后在您的更新响应中将let collections = 更改为this.collections =

    编辑:我将 .then 函数更改为 ES6 语法,否则您可能无法访问 this 变量。不需要currentObj 的东西。

    export default {
        props: ['collections_prop'],
    
        mounted() {
            console.log('MyComponent.vue mounted successfully');
        },
        data() {
            return {
                collections: this.collections_prop;
            }
        },
        methods: {
    
            updateQty(e) {
                e.preventDefault();
    
                let url = '/updateQty';
    
                // not sure where qty is coming from 
                // but you said that's all worked out
                // on your end
    
                axios.post(url, {
                    qty: qty.value,
                })
    
                .then(response => {
                    this.collections = response.data;
                })
            },
    
        }
    }
    

    最后,不要忘记更新视图中的道具。

    <my-component :collections_prop="{{ $collections }}"></my-component>
    

    或者如果您想稍后将 prop 类型指定为 JSON:

    <my-component :collections_prop='@json($collections)'></my-component>
    

    【讨论】:

    • 嘿@matticustard!谢谢你帮我解决这个问题。对此,我真的非常感激!为清楚起见,数量肯定会提交并更新数据库中的记录。如果有帮助,我将使用 $collections 数组的内容更新我的问题?
    • 有趣。当从未明确定义时,您如何访问updateQty 中的qty 变量?你能指出这个功能的文档吗?我只是好奇它是怎么做到的。
    • 抱歉!我只是试图从实际中重新编写此示例代码,只是偶然将其遗漏了。我在 data() 下列出了数量 - 我会更新我的答案。不是功能,只是脑放屁
    • 即便如此,我认为您必须使用this.qty 才能访问它。无论如何,如果它有效,那就太好了。我不会再打扰你了。
    • 解决了!非常感谢你帮助我。这里缺少的链接是我需要在我的更新函数中再次返回 $collections 集合。非常感谢您今天抽出宝贵的时间!
    猜你喜欢
    • 2021-10-19
    • 2018-10-31
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 2019-07-26
    • 1970-01-01
    • 2019-05-12
    相关资源
    最近更新 更多