一、React

(一)父组件向子组件传数据

  1. 简单的向下传递参数
/* Parent */
class App extends Component {
render() {
return (
<div className="App">
<Child msg="来自父元素的问候"/>
</div>
);
}
} /* Child */
class Child extends Component {
render() {
return <div>{this.props.msg}</div>;
}
}

在CodeSandbox中打开

  1. 向更下一级传递参数
/* Child1 */
class Child1 extends Component {
render() {
return (
<div>
<h3>Child1</h3>
<p>{this.props.msg}</p>
<Child1_Child1 {...this.props} />
</div>
);
}
} /* Child1_Child1 */
class Child1_Child1 extends Component {
render() {
return (
<div>
<h3>Child1_Child1</h3>
<p>{this.props.msg}</p>
</div>
);
}
}

在CodeSandbox中打开

(二)子组件向父组件传递参数

/* Parent */
class App extends Component {
constructor() {
super();
this.state = {
msg: "this is parent msg"
};
} changeMsg(msg) {
this.setState({ msg });
} render() {
return (
<div className="App">
<h3>parent</h3>
<p>{this.state.msg}</p>
<Child1
changeMsg={msg => {
this.changeMsg(msg);
}}
msg={this.state.msg}
/>
</div>
);
}
} /* Child1 */
class Child1 extends Component {
componentDidMount() {
setTimeout(() => {
this.props.changeMsg("This child change msg");
}, 1000);
} render() {
return (
<div>
<h3>Child1</h3>
<p>{this.props.msg}</p>
</div>
);
}
}

在CodeSandbox中打开

(三)兄弟组件传递参数

/* Parent */
class App extends Component {
constructor() {
super();
this.state = {
msg: "this is parent msg"
};
} changeMsg(msg) {
this.setState({ msg });
} render() {
return (
<div className="App">
<h3>parent</h3>
<p>{this.state.msg}</p>
<Child1
changeMsg={msg => {
this.changeMsg(msg);
}}
msg={this.state.msg}
/>
<Child1
msg={this.state.msg}
/>
</div>
);
}
} /* Child1 */
class Child1 extends Component {
componentDidMount() {
setTimeout(() => {
this.props.changeMsg("This child change msg");
}, 1000);
} render() {
return (
<div>
<h3>Child1</h3>
<p>{this.props.msg}</p>
</div>
);
}
} /* Child2 */
class Child2 extends Component {
render() {
return (
<div>
<h3>Child2</h3>
<p>{this.props.msg}</p>
</div>
);
}
}

二、Vue

(一)父组件向子组件传数据

  1. 简单的向下传递参数
/* Parent */
<div id="app">
<Child msg='ni daye'/>
</div> /* Child1 */
<template>
<div class="hello">
<p>{{ msg }}</p>
</div>
</template>
<script>
export default {
name: "HelloWorld",
props: {
msg: String
}
// somecomde
};
</script>

在CodeSandbox中打开

  1. 向更下一级传递参数
/* Child1 */
<template>
<div class="hello">
<p>{{ msg }}</p>
</div>
</template>
<script>
export default {
name: "HelloWorld",
props: {
msg: String
}
// some code
};
</script> /* Child1Child1 */
<template>
<div class="hello">
<p>{{ msg }}123123</p>
</div>
</template>
<script>
export default {
name: "Child1Child1",
props: {
msg: String
}
// some code
};
</script>

在CodeSandbox中打开

(二)子组件向父组件传递参数

/* Parent */
<template>
<div id="app">
<h3>parent</h3>
<Child2 @changParent='dealFromChild2'/>
</div>
</template>
<script>
import Child2 from "./components/Child2"; export default {
name: "App",
components: {
Child2
},
data () {
return {
fromChild2: ''
}
},
methods: {
dealFromChild2 (val) {
this.fromChild2 = val;
}
}
};
</script> /* Child2 */
<script>
export default {
name: "Child2",
data() {
return {};
},
mounted () {
setTimeout(() =>{
this.$emit('changParent', 'come from Child2')
}, 1000)
}
};
</script>

在CodeSandbox中打开

(三)兄弟组件传递参数

/* Parent */
<template>
<div id="app">
<h3>parent</h3>
<Child2 @changParent='dealFromChild2'/>
<Child1 :fromChild2='fromChild2'>
</div>
</template>
<script>
import Child2 from "./components/Child2";
import Child1 from "./components/Child1"; export default {
name: "App",
components: {
Child2
},
data () {
return {
fromChild2: ''
}
},
methods: {
dealFromChild2 (val) {
this.fromChild2 = val;
}
}
};
</script> /* Child2 */
<script>
export default {
name: "Child2",
data() {
return {};
},
mounted () {
setTimeout(() =>{
this.$emit('changParent', 'come from Child2')
}, 1000)
}
};
</script> /* Child1 */
<template>
<div class="hello">
<p>{{ fromChild2 }}</p>
</div>
</template>
export default {
name: "HelloWorld",
props: {
fromChild2: String
}
// some code
};

在CodeSandbox中打开

在github上编辑此页

原文地址:https://segmentfault.com/a/1190000016784633

React和Vue组件间数据传递demo的更多相关文章

  1. vue 组件间数据传递

    父组件向子组件传值 方法一: 子组件想要使用父组件的数据,需要通过子组件的 props 选项来获得父组件传过来的数据. 1.父组件parent.vue中代码: <template> < ...

  2. VUE组件间数据方法的传递,初步了解

    父组件的数据传递到子组件: 子组件:(其中fMsg是要从父组件传递过来的数据,注意fMsg要在子组件props里先定义) 父组件:(使用v-bind,将自身数据绑定给中转属性fMsg,从而通过 子组件 ...

  3. Vue2.0组件间数据传递

    Vue1.0组件间传递 使用$on()监听事件: 使用$emit()在它上面触发事件: 使用$dispatch()派发事件,事件沿着父链冒泡: 使用$broadcast()广播事件,事件向下传导给所有 ...

  4. 开始使用 Vuejs 2.0 --- 组件间数据传递

    Vue1.0组件间传递 使用$on()监听事件: 使用$emit()在它上面触发事件: 使用$dispatch()派发事件,事件沿着父链冒泡: 使用$broadcast()广播事件,事件向下传导给所有 ...

  5. 13. VUE 组件之间数据传递

    组件数据传递: 父组件向内传递属性---动态属性 子组件向外发布事件 solt 插槽传递模板---具名solt 1. 父组件向子组件传递数据 子组件在父组件的并作为标签引入,通过设置标签的属性传递数据 ...

  6. vue 组件中数据传递

    //有种形式的传递:从父到子,从子到父,平行级别的传递//首先第一种:从父到子,用props属性绑定 //父级数据: new vue({ "el":"#app" ...

  7. vue 组件之间数据传递(七)

    1.props:父组件 -->传值到子组件 app.vue是父组件 ,其它组件是子组件,把父组件值传递给子组件需要使用 =>props 在父组件(App.vue)定义一个属性(变量)sex ...

  8. VUE组件2数据传递

    传递数据 prop验证 除了传递数组,也可以传递对象 Vue.component('test',{ props:{ price:Number, unit: String } }) 如果price不是数 ...

  9. vue组件间的数据传递

    父组件向子组件传递数据 在 Vue 中,可以使用 props 向子组件传递数据.   App.vue HelloWorld.vue 在子组件部分: 如果需要从父组件获取 logo 的值,就需要使用 p ...

随机推荐

  1. 详解环境搭建SSM

    1.概述: SSM即为spring 4 +spring mvc +mybatis 3.4.6 推荐使用maven或者gradle 来配置 下面给出maven配置方式 2.项目结构: 新建web项目 这 ...

  2. slf4j日志整合框架的一些问题

    概述: 今天遇到一个问题,现在流程的形式,记录一下解法问题的一些过程, 你将收获解决常用包日志系统冲突的解决办法: Code 由于要使用log4j所以引入以下依赖 <!-- https://mv ...

  3. [HNOI2010] 弾飞绵羊

    题目链接: 传送门 题目分析: 题外话: 我即使是死了,钉在棺材里了,也要在墓里,用这腐朽的声带喊出: 根号算法牛逼!!! 显然,这是一道LCT裸题,然而在下并不会LCT于是采用了分块瞎搞 对于每个点 ...

  4. 【手撸一个ORM】第八步、查询工具类

    一.实体查询 using MyOrm.Commons; using MyOrm.DbParameters; using MyOrm.Expressions; using MyOrm.Mappers; ...

  5. NET高性能IO

    System.IO.Pipelines: .NET高性能IO https://www.cnblogs.com/xxfy1/p/9290235.html System.IO.Pipelines是一个新的 ...

  6. 043 Multiply Strings 字符串相乘

    给定两个以字符串表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积.注意:    num1 和 num2 的长度均小于110.    num1 和 num2 均只包含数字 0 ...

  7. Ubuntu搭建WordPress-MySQL-Apache

    目标 技术博客www.xifarm.com有5年时间了. 原来在虚拟机/VPS上搭建,不过都是Windows系统下的. 最近突发奇想,试试迁移到Linux的Unbuntu下.说干就干,抽空用了大约3天 ...

  8. P4876 近似排列计数50

    时间限制:1s 内存限制:256MB [问题描述] 对于一个1-n的排列,如果满足第i个数|ai-i|<=k,则称该排列为K-近似排列. 现在排列的若干位置已经确定,你需要计算剩下的数有多少种排 ...

  9. SpringBoot 2.0中SpringWebContext 找不到无法使用的问题解决

    为了应对在SpringBoot中的高并发及优化访问速度,我们一般会把页面上的数据查询出来,然后放到redis中进行缓存.减少数据库的压力. 在SpringBoot中一般使用 thymeleafView ...

  10. Cannot load JDBC driver class 'com.mysql.jdbc.Driver解决方法

    “Cannot load JDBC driver class 'com.mysql.jdbc.Driver ” 表示没有JDBC连接MySql的驱动包,因此需要手动添加驱动包到WEB-INF目录下的l ...