- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这里看到了一些意想不到的奇怪行为,但就纯 JavaScript 而言,它对我来说是直观的。
我有一个表单 Controller ,它累积一个 this.thing
对象,该对象在最终提交时发送到服务器。这是一个多步骤表单,因此每个步骤都会向 this.thing
添加一些数据。
所以 Controller 有:
data() {
return {
thing: {},
};
},
此 Controller 的 DOM 标记有一个子项,例如:
<a-child
:initial-thing="thing"
></a-child>
子进程使用该 prop 来显示其初始状态,因此它接收该 prop 并将其设置为自己的本地状态作为实例数据:
initialThing: {
type: Object,
required: true,
},
...
data() {
return {
thing: this.initialThing,
};
},
然后这个 child 有一个像这样的复选框:
<a-checkbox
v-model="thing.field"
:initial-value="initialThing.field"
></a-checkbox>
这一切都工作正常,只是我刚刚注意到当复选框更改时,它会改变父 Controller thing.field
值。
我提出这个问题是因为我不明白 Vue 如何做到这一点,对我来说唯一有意义的是当 child 做 thing: this.initialThing
时,它是允许子进程调用 this.initialThing
上该字段的 setter 函数。
如果我这样做,它就会停止改变父级的状态:
data() {
return {
thing: { ...this.initialThing },
};
},
在我的实际应用程序中,它更复杂,因为有 2 个中间组件,因此孙子正在改变祖 parent 的状态,这源于我在这里描述的模式。
任何人都可以为这里发生的事情提供教科书式的答案吗?我对是否依赖这种行为犹豫不决,因为驱动它的代码并不明确。它使我的一些 $emit()
事件变得多余,有利于使用这种间接/非显式的向上游发送数据的方式。
还要明确的是,这与 v-model 无关,因为如果我这样做 this.thing.field = 'new value';
也会这样做。我相信这与继承 this.initialThing
上的 getter/setter 密切相关。依赖这种行为安全吗?如果我依赖它,它将使我的代码更加简洁,但是天真的个人可能很难理解数据如何将其放入祖 parent 组件中。
最佳答案
这是一个浅拷贝,因此您无法阻止突变的孙子。
data() {
return {
thing: { ...this.initialThing },
};
},
解决方案如下:
data() {
return {
thing: JSON.parse(JSON.stringify(this.initialThing)),
};
},
const initialThing = {
age: 23,
name: {
first: "David",
last: "Collins",
}
}
const shallowCopy = { ...initialThing };
shallowCopy.age = 10;
shallowCopy.name.first = "Antonio"; // will mutate initialThing
console.log("init:", initialThing);
console.log("shallow:", shallowCopy);
const deepCopy = JSON.parse(JSON.stringify(initialThing));
deepCopy.age = 30;
shallowCopy.first = "Nicholas"; // will not mutate initialThing
console.log("------Deep Copy------");
console.log("init:", initialThing);
console.log("deep:", deepCopy);
工作原理:
JSON.stringify(this.initialThing)
这会将 JSON Object
转换为 String
类型。这意味着它永远不会再让 child 发生变异。然后 JSON.parse
会将 String
转换为 Object
类型。
But, using stringify and parse will be expensive in performance. :D
更新:如果你使用的是lodash或者可以添加外部库,你可以使用_.cloneDeep .
_.cloneDeep(value); // deep clone
_.clone(value); // shallow clone
关于javascript - Vue 组件的 prop 设置为实例数据会改变上游值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57839858/
我正在使用 rebase 主题分支工作流 http://www.golden-gryphon.com/software/misc/packaging.html 但由于本地测试人员和管理员不喜欢一次性发
在我的工作中,我们为我们的一些产品使用了开源软件,并在主线项目提供的功能之上开发了一些额外的特性和功能。开源项目正在积极开发中,我打算将其中一些更改贡献回主线开源项目。 不过,我还添加了一些我们产品所
我正在尝试将 proxy_pass 与 nginx 一起使用,其中与上游服务器的连接是加密的。上游服务器的证书已由 letsencrypt certbot 创建。 # upstream server:
我一直沿着实际河流的路线思考上游和下游,其中信息的流动就像水一样。因此,上游是水/数据的来源(例如 HTTP 请求),下游是水/数据的去向(例如为请求提供服务的底层系统)。 我最近一直在研究 API
我正在努力使用 nginx 代理到 SSL 上游。我意识到代理到 HTTPS 是浪费,但这是我的设置,有时直接访问 API,其他时候我使用 nginx 来服务 JS 应用程序,它也是 API、CORS
我有两个 channel ,上游和下游。我的目标是从上游读取数据并将其传递给下游。但是,当取消上下文时,我希望在没有死锁的情况下正常退出。 我试图变得“聪明”,并做了类似以下的事情。 func mai
我已经在新安装的 cpanel vps 上安装了 nginxadmin。当我尝试登录域时出现 504 超时。这是我的日志和配置。 日志-------------------- 2015/03/06 1
无法理解为什么我的上游/CORS 配置失败。这阻碍了一些本地开发和测试。 从 local.mysite.com:8081 发出 API 请求时,我收到请求的资源上不存在“Access-Control-
我收到这个错误: Error frontend: 502 Bad gateway 99.110.244:443 2017/09/28 13:03:51 [error] 34080#34080: *10
只是一个免责声明:在使用 git 几个月后,我仍然完全不知道我在使用它时在做什么。只有当我严格按照我在网络上的各个页面上找到的说明进行操作时,事情才会奏效。 我想做的是更新我的存储库,它是从另一个项目
我在上游有 1.11.57 标签,我用 master 分支代码推送了它。我正在使用 bitbucket 和 git bash 我 fork 了上面的存储库,并在本地使用 fork 存储库作为我的本地主
我想使用 nginx 作为 websocket 连接的反向代理。 考虑 echo.websocket.org 成为我的后端 websocket 服务。作为测试客户端,我使用 wscat 来自 http
假设您有一个返回 postgresQL 数据库中定义的 json 的函数。 CREATE OR REPLACE FUNCTION test() RETURNS JSON AS $$ SELECT
在 ubuntu 18.04 上,我正在运行 nginx upstream vault { server 127.0.0.1:8001; } server { listen 80;
我有一些关于 nginx 绕过 http 和 https 的问题,在上游 block 中 上游 block : upstream bypass{ server 192.168.99.1:8
我们有一个由大约 10 名开发人员组成的团队,我们经常遇到某人的更改被意外还原的情况。我们的工作流程非常简单。开发人员进行本地提交,从上游 pull ,然后推送到上游(简而言之,这是我们的工作流程,但
我正在开发一个由 3 个微服务组成的应用程序。每个微服务都是一个 Symfony 4 应用程序。 为了将我发出的所有请求路由到此应用程序,我正在使用 Nginx。 目前有三种 url 模式,每个微服务
真正知道这个问题答案的人是那些为 Linux 发行版中包含的程序或库开发的人,这些程序或库依赖于 Linux 发行版中包含的其他库。他们只是告诉我他们在我描述的开发环境方面做了什么会很有帮助。 问题基
我是一名优秀的程序员,十分优秀!