简体   繁体   English

Vue.js 中的方法无法更改数据值

[英]method in Vue.js cannot alter data value

I'm using Vue.js 2 and i'm trying to make a component that does a simple discount calculation after user input.我正在使用 Vue.js 2 并且我正在尝试制作一个在用户输入后进行简单折扣计算的组件。

The object i'm using as starting data is an object made like this: it is composed of several building and each of them can have several rooms inside.我使用的 object 作为起始数据是这样制作的 object:它由几个建筑物组成,每个建筑物里面可以有几个房间。 I have an object (i write it as an array, but it's an object) who has我有一个 object (我把它写成一个数组,但它是一个对象)谁有

buildings[<building_id>][<room_id]={room_id: <room_id>,
                                    building_id: <building_id>
                                    name: <name>,
                                    price: <regular_price>
                                    discounted_price: <discounted_price>
                                    discount: discount}

Here is the full code of the component这是组件的完整代码

<template>
    <div>
        {{ buildings }}
        <li v-for="(building, k) in buildings" :key="k">
            <table class="table table-striped table-bordered">
                <thead>
                    <tr><th>Room</th><th>Standard price</th><th>Discounted price</th><th>Discount</th></tr>
                </thead>
                <tbody>
                    <tr v-for="(room, s) in building" :key="s">
                        <td>{{room.name}}</td>
                        <td>{{room.price}} €</td>
                        <td><input type="text" class="form-control" v-model="room.discounted_price" 
                            @input="calcDiscount(room)"></td>
                        <td><input type="text" class="form-control" v-model="room.discount" disabled></td>
                    </tr>
                </tbody>
            </table>
        </li>
    </div>
</template>
<script>
export default {
    data: function() {
        return {
            buildings: {},
        }
    },
    mounted() {
        this.getBuildings();
    },
    methods: {
        getBuildings: function(callback) {
            httpRequest(api_url + "get/buildings").then(onResult => {
                this.buildings = onResult.data.buildings;
            });
        },
        calcDiscount(evt) {
            room.discount = Math.round(
               100 - (100 * room.discounted_price) / room.price
            );
        }
    }
}
</script>

I have two problems with this code.这段代码有两个问题。 First, the v-bind on the input element does not work.首先,输入元素上的 v-bind 不起作用。 As you can see i've printed the buildings object on top and i don't see the values of discounted_price change on the user input.如您所见,我在顶部打印了buildings object 并且我没有看到用户输入的discounted_price的值发生变化。

But apart this, despite the log display the right values, nor the value of discount nor of discounted_price are changed in the object.但除此之外,尽管日志显示正确的值,但 object 中的discountdiscounted_price的值都没有改变。 But the values are taken correctly because they're printed right just one line before.但是这些值是正确的,因为它们之前只打印了一行。 What i'm doing wrong?我做错了什么? Thanks谢谢

EDITED after tao comments陶评论后编辑

FURTHER UPDATE: Since the code tao made here in the playground works, the problem should be something in my environment.进一步更新:由于在操场上制作代码 tao 有效,问题应该出在我的环境中。 Apparently the function calcDiscount works well since if i print the value inside that function it's printed correctly.显然 function calcDiscount工作得很好,因为如果我打印 function 里面的值,它就会正确打印。 But somehow the changes disappear when function exists.但不知何故,当 function 存在时,这些变化消失了。 Could be that the event take place like in a different space?会不会是事件发生在不同的空间?

You need to change @input by v-on:keyup .您需要通过v-on:keyup更改@input input binding just change the value inside but it doesn't make web app recognize that it need to re-render.输入绑定只是更改内部的值,但它不会使 web 应用程序认识到它需要重新渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM