一、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. selenium基础操作

    selenium 1.打开和关闭网页 #!/usr/bin/env python # -*- coding:utf-8 -*- from selenium import webdriver drive ...

  2. Python随笔---return与print,全局变量与局部变量

    Return是指返回一个数值,一般在函数中应用较多 Print则是指把结果打印出来,显示在屏幕上 def sum(a,b): total=a+b print('函数内:',total) return ...

  3. 用Open Live Writer写博体验

    感觉还蛮方便的--openlivewriter第一博!

  4. #1369 : 网络流一·Ford-Fulkerson算法 模板题

    http://hihocoder.com/problemset/problem/1369?sid=1108721 别人都说先学网络流再学二分图,但是我先学了二分图的,感觉网络流好高端啊. 首先对于原图 ...

  5. openstack安装newton版本Glance部署(二)

    一.部署Glance 1.Glance 安装 [root@linux-node1 ~]#yum install openstack-glance python-glance python-glance ...

  6. ECharts3.0介绍、入门

    ECharts 特性介绍 ECharts,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,S ...

  7. Appium基础一:Appium概念

    1.Appium介绍: Appium是一款开源跨平台(IOS和Android平台)支持多种开发语言(java.python等)进行测试Native/Web/Hybrid的Android/iOS App ...

  8. C++运算符重载讲解与经典实例

    最近在学C++,找到一篇详细讲解运算符重载的文章,贴在这里分享和收藏. C++中预定义的运算符的操作对象只能是基本数据类型,实际上,对于很多用户自定义类型,也需要有类似的运算操作.例如: class  ...

  9. SharePoint 2013 安装配置(2)

    上一篇中,我们已经安装了Windows Server 2012 R2.现在第二部分,如何在Windows Server 2012 R2中安装Active Directory域控制器.请按照以下步骤. ...

  10. 如何从桌面程序向浏览器传递cookie或自定义header

    类似问题 从c#程序启动ie并传递cookie 打开默认浏览器并传递cookie 打开一个web浏览器使用c#应用程序并添加请求头 猜想 从wpf程序打开默认浏览器并定位到一个url ,并且向这个ur ...