VUE / JavaScript变量的奇怪问题

发布于 2025-01-23 08:39:43 字数 1099 浏览 1 评论 0原文

老实说,我什至不知道如何搜索这个问题(要编写什么搜索参数),但无论哪种方式,它都很奇怪,我迫切希望寻求帮助。

因此,我正在尝试做一些简单的事情,事件发送“ form-change” ,当它做到时,我们将“ this.data”的新值设置为“ this.data” 对象。相当简单。我不希望 this.data 我只想更新它。

// Find our data object which we want to update/change
if (form.field.includes('.')) {

    let find = form.field.split('.'), level = this.data;
    for (let index = 0; index < find.length; index++) {

        if (level[find[index]] !== undefined) {

            level = level[find[index]];

        } else {

            level = undefined;

        }

    }

    if (level !== undefined)
        level = setFieldData();

}

这很简单,我们有字段“ ixply.book” 的名称(事件)时,我们只使用dots将dots分为多树并更新“ this.data.insprect。预订“ 新价值。但是它行不通。值不会改变。

实际上来自实际的值。

console.log(level);

this.data[ form.field.split( '.' )[ 0 ] ][ form.field.split( '.' )[ 1 ] ] = setFieldData();

但是 因此,“引用”变量不起作用...这是怎么可能的?看起来像JavaScript中的错误,还是具有VUE/反应性的东西?

有人有更好的主意如何使它工作吗?

I honestly don't even know how to search for this question (what search param to write) but either way its bit weird issue and I am desperate for help.

So I am trying to do something simple, event sends "form-change" and when it does, we set new value in "this.data" object. Fairly simple. I don't expect this.data to be reactive I just want to update it.

// Find our data object which we want to update/change
if (form.field.includes('.')) {

    let find = form.field.split('.'), level = this.data;
    for (let index = 0; index < find.length; index++) {

        if (level[find[index]] !== undefined) {

            level = level[find[index]];

        } else {

            level = undefined;

        }

    }

    if (level !== undefined)
        level = setFieldData();

}

This is fairly simple, we have name of field "inspect.book" and when update comes (Event) we just use dots to split into multi tree and update "this.data.inspect.book" to new value. But it does not work. Value does not change.

But the value from actual this.data.inspect.book comes out just fine using:

console.log(level);

However, if I do this:

this.data[ form.field.split( '.' )[ 0 ] ][ form.field.split( '.' )[ 1 ] ] = setFieldData();

It works fine. So "reference" to variable does not work... how is this possible? Looks like a bug in javascript or is it something with vue/reactivity?

Does anyone have better idea how to get this to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈下烟灰 2025-01-30 08:39:43

因此,您正在尝试使用符号更新表单数据?

我会做这样的事情:

_update(fieldName, value) {
            // We split segments using dot notation (.)
            let segments = fieldName.split(".");

            // We get the last one
            let lastSegment = segments.pop();

            // We start with this.data as root
            let current = this.data

            if(current) {
                // We update current by going down each segment
                segments.forEach((segment) => {
                    current = current[segment];
                });

                // We update the last segment once we are down the correct depth
                current[lastSegment] = val;
            }
        },

如果以您的示例为例:

if (form.field.includes('.')) {

    let find = form.field.split('.'), level = this.data;
    for (let index = 0; index < find.length - 1; index++) {

        if (level[find[index]] !== undefined) {

            level = level[find[index]];

        } else {

            level = undefined;

        }

    }

    if (level !== undefined)
        level[find.pop()] = setFieldData();

}

我替换find.length by find.length -1
并替换level = setFieldData() by lats [find.pop()] = setFieldData()

您应该更新对象的属性,而无需实际覆盖值,
因为如果您覆盖值,则原始值将不会更新。

So you are trying to update form data using to notation ?

i would do something like that :

_update(fieldName, value) {
            // We split segments using dot notation (.)
            let segments = fieldName.split(".");

            // We get the last one
            let lastSegment = segments.pop();

            // We start with this.data as root
            let current = this.data

            if(current) {
                // We update current by going down each segment
                segments.forEach((segment) => {
                    current = current[segment];
                });

                // We update the last segment once we are down the correct depth
                current[lastSegment] = val;
            }
        },

if i take your example :

if (form.field.includes('.')) {

    let find = form.field.split('.'), level = this.data;
    for (let index = 0; index < find.length - 1; index++) {

        if (level[find[index]] !== undefined) {

            level = level[find[index]];

        } else {

            level = undefined;

        }

    }

    if (level !== undefined)
        level[find.pop()] = setFieldData();

}

i replaced find.length by find.length - 1
and replaced level = setFieldData() by level[find.pop()] = setFieldData()

you should update the property of the object, without actually overriding the value,
because if you override the value, the original value will not get updated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文