I have carefully read and re-read the Vue docs "Reactivity in Depth" and the API for vm.$set and Vue.set but I am still having a difficult time determining when to use which. It is important for me to be able to distinguish between the two because in my current Laravel project, we are setting a lot of properties on objects dynamically.

文档中的区别似乎在于虚拟机使用的语言$设置为"适用于Vue实例",而Vue.设置为"用于普通数据对象"和Vue.集合是全局的:

然而,有一些方法可以添加一个属性,并使其在之后具有react 性

对于Vue实例,可以使用$set(路径、值)实例方法:

vm.$set('b', 2)
// `vm.b` and `data.b` are now reactive

对于普通数据对象,可以使用全局Vue.设置(对象、键、,

Vue.set(data, 'c', 3)
// `vm.c` and `data.c` are now reactive

最后,我想知道是否可以将执行上述操作的第三个"选项"(即一次添加多个属性)用作上述两个选项中任何一个的等效替代品(只添加一个属性而不是多个)?

Sometimes you may want to assign a number of properties on to an existing object, for example using Object.assign() or _.extend(). However, new properties added to the object will not trigger changes. In such cases, create a fresh object with properties from both the original object and the mixin object:

// instead of `Object.assign(this.someObject, { a: 1, b: 2 })`
this.someObject = Object.assign({}, this.someObject, { a: 1, b: 2 })

推荐答案

Here is the release note that went with the introduction of Vue.set:

VUE不再使用$set和$delete方法扩展Object.Prototype. 这导致依赖于这些库的库出现问题 某些条件判断中的属性(例如,Meteor中的Minimongo). 不使用Object.$set(键,值)和Object.$delete(键),而使用 新的全局方法Vue.set(Object,Key,Value)和Vue.delete(Object, 密钥).

So, .$set used to be available on all objects - it is now only available on a View Model itself. Vue.set is therefore used in those cases now when you have a reference to a reactive object but do not have a reference to the View Model it belongs to.

Laravel相关问答推荐

子目录中具有Vue实例的Laravel不起作用

Laravel 联合和分页

assertSee 由于 html 代码中的空格而失败

Laravel - 将变量从中间件传递到控制器/路由

Laravel 5.4 - 如何为同一个自定义验证规则使用多个错误消息

如何在laravel中将数组转换为字符串?

Laravel Eloquent,仅 Select 存在关系的行

限制 Blade foreach 循环中的结果

如何更改 Laravel5 中的视图文件夹?

升级到 laravel 5.4 后调用未定义的方法

控制器中的 Laravel Auth 用户 ID

设置 Laravel 5.4 以使用 React

如何在 Laravel 中使用旧输入重定向?

如何将对象(模型类型对象)插入到 Laravel 中特定索引号的集合对象中?

Laravel 接口

你如何在 Laravel 中的每个响应上强制一个 JSON 响应?

Laravel - 使用 Eloquent 查询构建器在 Select 中添加自定义列

将参数传递给 Laravel 中的中间件

在 Laravel 中翻译特定语言

Laravel 5.3 - 用户Collection 的单一通知(关注者)