子组件props接受父组件传递的值 能修改吗?
vue2.0 子组件props接受父组件传递的值,能不能修改的问题整理
父组件代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<!-- --><template> <div class=''> <el-link type="danger">传值为对象:</el-link> <div> 父组件中显示fatherData的值:{{fatherData}} <l0705components :fatherData="fatherData"></l0705components> </div> <br><br><br> <el-link type="danger">传值为字符串,使用v-model传值:</el-link> <div> 父组件中显示fatherData2的值:{{fatherData2}} <l0705components v-model="fatherData2"></l0705components> </div> <br><br> <el-link type="danger">传值为字符串:</el-link> <div> 父组件中显示fatherData3的值:{{fatherData3}} <l0705components :fatherData3="fatherData3"></l0705components> </div> </div></template><script> import l0705components from './views/l0705components' export default { name: "L0705L", components: { l0705components }, data() { // 这里存放数据 return { fatherData:{ name:"李四", age:"14" }, fatherData2:'父组件的数据2', fatherData3: '父组件的数据3' } } }</script> |
子组件代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<!-- --><template> <div class=''> <div v-if="fatherData"> 子组件中显示fatherData的值:{{fatherData}} <el-button type="danger" @click="changeFather"> 点击修改父组件fartherData的值-姓名改为“王五” </el-button> </div> <div v-if="value!==''"> <input v-model="value"> </div> <div v-if="fatherData3!==''"> 子组件中显示fatherData3的值:{{fatherData3}} <el-button type="danger" @click="changeFather3"> 点击修改父组件fartherData3的值,改为“哈哈哈哈哈” </el-button> </div> </div></template><script> export default { props:{ fatherData:{ type:Object }, value: { type: String, default: '' }, fatherData3: { type: String, default: '' } }, name: "l0705components", data() { // 这里存放数据 return { } }, // 方法集合 methods: { changeFather(){ this.fatherData.name = '王五' }, changeFather3() { this.fatherData3 = '哈哈哈哈哈' } } }</script> |
页面展示:

测试结果说明:
1.父组件传递一个对象,子组件接受,子组件中,直接修改接受到的对象里面的值,可以修改,父子组件的值都会随之改变

2.使用v-model传值,修改input里面的值,会报错

意思就是props传递的值不能进行修改
3.点击修改第三个值,在子组件中的值会修改,但是父组件中不能修改,报错

总结:
父子组件传值时,父组件传递的参数,数组和对象,子组件接受之后可以直接进行修改,并且会传递给父组件相应的值也会修改。
如果传递的值是字符串,直接修改会报错。
不推荐子组件直接修改父组件中的参数,避免这个参数多个子组件引用,无法找到造成数据不正常的原因
子组件props接受父组件传递的值 能修改吗?的更多相关文章
- vue2.0 子组件props接受父组件传递的值,能不能修改的问题整理
父组件代码: <!-- --> <template> <div class=''> <el-link type="danger">传 ...
- 子组件props接受父组件传递的值,能不能修改的问题
参考链接:https://www.cnblogs.com/pangchunlei/p/11139356.html
- 九、React中的组件、父子组件、React props父组件给子组件传值、子组件给父组件传值、父组件中通过refs获取子组件属性和方法
一.概述 React中的组件: 解决html 标签构建应用的不足. 使用组件的好处:把公共的功能单独抽离成一个文件作为一个组件,哪里里使用哪里引入. [父子组件]:组件的相互调用中,我们把调用者称为父 ...
- vue父组件调用子组件方法、父组件向子组件传值、子组件向父组件传值
一.父组件调用子组件方法 父组件代码 parent.vue <template> <div> <button @click="parentFun" ...
- Vue中子组件数据跟着父组件改变和父组件数据跟着子组件改变的方法
一,子组件数据跟着父组件改变 父组件的代码 <template> <div class="home"> <img alt="Vue logo ...
- React子组件怎么改变父组件的state
React子组件怎么改变父组件的state 1.父组件 class Father extends React.Component { construtor(props){ super(props); ...
- vue 父组件主动获取子组件的数据和方法 子组件主动获取父组件的数据和方法
Header.vue <template> <div> <h2>我是头部组件</h2> <button @click="getParen ...
- vue 父组件给子组件传值 Vue父组件给子组件传方法 Vue父组件把整个实例传给子组件
Home.vue <template> <!-- 所有的内容要被根节点包含起来 --> <div id="home"> <v-header ...
- vue中子组件的拆分 父组件与子组件之间的传值
vue是组件式开发,尽量独立出子组件 prop():父组件传值给子组件 $emit():子组件传值给父组件 子组件中的设置: 使用bind <template> : default-che ...
随机推荐
- 使用axios对安卓或者ios低版本兼容性处理
原因:不支持ES6,无法使用promise 解决办法: 1.安装 es6-promise cnpm install es6-promise --save-dev 2.引入 es6-promise im ...
- 使用session在jsp页面之间传递多维数组,用于实现全局变量的效果
使用session在jsp页面之间传递多维数组:发送数据的jsp页面:int [][] form_number=new int[4][4]; session.setAttribute("se ...
- HTML 入门第一课
HTML 简单认识 HTML(HyperText Markup Language)即超文本标记语言,是一种用来制作超文本文档的简单标记语言,也是制作网页的最基本的语言,它可以直接由浏览器执行. 1.H ...
- HDU3173 Dominos
单向并查集,问至少给几个点可以遍历全图,连通块数量n,入度为0的点的数量m,取max(n,m)~ #include<cstdio> #include<algorithm> #i ...
- Qt5基于smtp服务发送电子邮件
1.设置邮箱 先登录163邮箱,然后在邮箱界面找到设置,在里面开通smtp服务. 这一步比较关键,要开通smtp服务,在开通的过程中会让你输入一个邮箱客户端授权码,这个才是你后面要用到的密码,而不是你 ...
- CentOS上安装elasticsearch
1.安装docker yum install docker 2.启动docker systemctl start docker 查看docker版本: docker -v 之后只要启动机器就会自动开启 ...
- day04-MyBatis的注解开发
单表的CRUD注解开发: User实体类: package com.zyb.pojo; import java.io.Serializable; import java.util.Date; publ ...
- 虚拟机下修改ip配置
// centos ip 配置vim /etc/sysconfig/network-scripts/ifcfg-eth0 // 虚拟机下删除里面的内容vim /etc/udev/rules.d/70- ...
- idna与utf-8编码漏洞
来自Black hat 2019 原理什么是IDN?国际化域名(Internationalized Domain Name,IDN)又名特殊字符域名,是指部分或完全使用特殊文字或字母组成的互联网域名, ...
- SpringBoot与Mybatis整合,插件生成dao、mapper、pojo
一.创建SpringBoot项目,引入相关依赖包 <?xml version="1.0" encoding="UTF-8"?> <projec ...