【发布时间】:2022-06-15 22:27:30
【问题描述】:
我想在我的 Vue 应用程序中使用 structuredClone() 函数。我想用它来创建一个深度克隆(而不是使用像 stringify 和 parse 或外部库这样的变通方法)。在我的设置函数中,以下代码很好
const a = {
foo: {
bar: "+"
}
};
const b = structuredClone(a);
console.log(b);
但我不可能在 ref 变量的值上使用它。本示例代码
import { ref } from "vue";
const a = ref({ foo: { bar: "+" } });
const b = structuredClone(a.value);
抛出错误
Uncaught DOMException: Failed to execute 'structuredClone' on 'Window': # could not be clone.
引用数组中的项目也是如此
import { ref } from "vue";
const a = ref([{ foo: { bar: "+" } }]);
for (const b of a.value) {
const c = structuredClone(b);
}
如何解决这个问题?
【问题讨论】:
标签: javascript vue.js