在vue composition api中:


我有一些定制的产品:

const chair = new Product(...); // lots of information in each product 
const table = new Product(...); // lots of information in each product

以及深层对象中引用产品的订单列表:

let example = reactive({   
  orders: [
    { product: chair, quantity: 2 }, 
    { product: table, quantity: 1 }, 
    { product: chair, quantity: 6 }, 
    { product: table, quantity: 2 },
  ] 
});

我通过example.orders[0].product == chair->false这些是不同的物体.

由于我可以有很多不同的订单和产品包含很多数据,我希望example.orders[].product保持参考原始产品.

我不要求产品本身具有react 性,因为它们是恒定的.(这是一款Electron 应用程序,只要程序运行,内容就会保持不变)

我只想接受订单.

推荐答案

使用markRaw:

import { markRaw, reactive } from 'vue';

const example = reactive({   
  orders: [
    { product: chair, quantity: 2 }, 
    { product: table, quantity: 1 }, 
    { product: chair, quantity: 6 }, 
    { product: table, quantity: 2 },
  ].map(el => ({ 
    ...el,
    product: markRaw(el.product)
  }))
});

请阅读标签上的警告.

Vue.js相关问答推荐

Vue3:如何在功能组件中重用插槽vnode

Vue 3:插槽属性 - 可以通过 props 传递数据吗?

如果似乎没有明确使用输入事件,那么发出输入事件有什么作用?

在 Vuejs 中更新 Chartjs 图表数据集

在 onMounted 之前渲染的 Vue 模板

绑定事件在渲染函数中不起作用

如何解决 importers[path] is not a function for clean Vue 3/Vite/Storybook installation

VeeValidate 4 字段验证状态

为什么我在 Vue npm run serve 的 localhost 中得到无法获取

如何在 vue.js 构建中重命名 index.html?

vuejs中watch方法和计算方法有什么区别

为什么Vue.js 使用单字标签

Polyfill for ie

如何在 Vue 类组件中定义过滤器?

如何从 vue-apollo 中访问 this.$route?

在路由更改之前显示确认对话框

$set 不是函数

带有 Google Auth Signin 的 Vuejs 全局函数

如何订阅store 模块

在 vue.js 应用程序中将静态内容(例如国家代码)存储在哪里?