React和Vue组件间数据传递demo
一、React
(一)父组件向子组件传数据
- 简单的向下传递参数
/* 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>;
}
}
- 向更下一级传递参数
/* 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>
);
}
}
(二)子组件向父组件传递参数
/* 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>
);
}
}
(三)兄弟组件传递参数
/* 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
(一)父组件向子组件传数据
- 简单的向下传递参数
/* 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>
- 向更下一级传递参数
/* 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>
(二)子组件向父组件传递参数
/* 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>
(三)兄弟组件传递参数
/* 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
};
原文地址:https://segmentfault.com/a/1190000016784633
React和Vue组件间数据传递demo的更多相关文章
- vue 组件间数据传递
父组件向子组件传值 方法一: 子组件想要使用父组件的数据,需要通过子组件的 props 选项来获得父组件传过来的数据. 1.父组件parent.vue中代码: <template> < ...
- VUE组件间数据方法的传递,初步了解
父组件的数据传递到子组件: 子组件:(其中fMsg是要从父组件传递过来的数据,注意fMsg要在子组件props里先定义) 父组件:(使用v-bind,将自身数据绑定给中转属性fMsg,从而通过 子组件 ...
- Vue2.0组件间数据传递
Vue1.0组件间传递 使用$on()监听事件: 使用$emit()在它上面触发事件: 使用$dispatch()派发事件,事件沿着父链冒泡: 使用$broadcast()广播事件,事件向下传导给所有 ...
- 开始使用 Vuejs 2.0 --- 组件间数据传递
Vue1.0组件间传递 使用$on()监听事件: 使用$emit()在它上面触发事件: 使用$dispatch()派发事件,事件沿着父链冒泡: 使用$broadcast()广播事件,事件向下传导给所有 ...
- 13. VUE 组件之间数据传递
组件数据传递: 父组件向内传递属性---动态属性 子组件向外发布事件 solt 插槽传递模板---具名solt 1. 父组件向子组件传递数据 子组件在父组件的并作为标签引入,通过设置标签的属性传递数据 ...
- vue 组件中数据传递
//有种形式的传递:从父到子,从子到父,平行级别的传递//首先第一种:从父到子,用props属性绑定 //父级数据: new vue({ "el":"#app" ...
- vue 组件之间数据传递(七)
1.props:父组件 -->传值到子组件 app.vue是父组件 ,其它组件是子组件,把父组件值传递给子组件需要使用 =>props 在父组件(App.vue)定义一个属性(变量)sex ...
- VUE组件2数据传递
传递数据 prop验证 除了传递数组,也可以传递对象 Vue.component('test',{ props:{ price:Number, unit: String } }) 如果price不是数 ...
- vue组件间的数据传递
父组件向子组件传递数据 在 Vue 中,可以使用 props 向子组件传递数据. App.vue HelloWorld.vue 在子组件部分: 如果需要从父组件获取 logo 的值,就需要使用 p ...
随机推荐
- Codeforces Round #527-B. Teams Forming(贪心)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- NSwag生成客户端调用代码
NetCore2.1 WebAPI 根据swagger.json自动生成客户端代码 https://www.cnblogs.com/hunanzp/p/9297361.html 前言 上一篇博客中我们 ...
- hihoCoder 1383 : The Book List 北京网络赛
http://hihocoder.com/problemset/problem/1383?sid=950389 #1383 : The Book List 时间限制:1000ms 单点时限:1000m ...
- DevExpress GridControl 控件二表连动
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- android应用开发全程实录-你有多熟悉listview?
今天给大家带来<android应用开发全程实录>中关于listview和adatper中的部分.包括listview的基本使用,listview的优化等. 我们经常会在应用程序中使用列表的 ...
- jsp-简单的猜数小游戏
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- ECShop怎么首页调用文章列表
举例如首页调用方法:1.先打开index.php文件找到以下代码:$smarty->assign('new_articles', index_get_new_articles()); // 最新 ...
- 如何快速构建CMBD系统-glpi
一.CMBD系统构建步骤 起初,开发这套CMBD系统是为了帮助朋友公司简化设备统计操作,以代替人工入库方式.举个例子,单位发放笔记本,或者设备更换了硬盘,都需要人工签到,手动输入统计,安装了CMBD系 ...
- Linux系统常用命令大全
一.系统信息操作(备注:红色标记为常用命令,以下类推,不再赘述) arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r ...
- Autoit3脚本编写举例
以任务管理器为例 1.首先打开任务管理器 2.点击结束任务操作 第一步打开任务管理器 run("C:\WINDOWS\system32\taskmgr.exe"); 第二步点击结束 ...