一、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. Codeforces 126B(kmp)

    要点 头尾的最长相同只要一个kmp即可得,于是处理中间部分 扫一遍记录一下前缀的每个位置是否存在一个中间串跟它相同,见代码 如果当前没有,接着用Next数组去一找即可 #include <cst ...

  2. NET Core应用中如何记录和查看日志

    NET Core应用中如何记录和查看日志 日志记录不仅对于我们开发的应用,还是对于ASP.NET Core框架功能都是一项非常重要的功能特性.我们知道ASP.NET Core使用的是一个极具扩展性的日 ...

  3. jQuery banner切换插件

    今天学写了一个基于jQuery焦点图切换插件,有不对的地方还请多多指教,不多说下面是代码: 1.引jQuery库 <script src="http://code.jquery.com ...

  4. 牛客网Java刷题知识点之面向对象java的四大特性(抽象、封装、继承、多态)

    不多说,直接上干货! 面向对象java的四大特性之抽象 抽象就是有点模糊的意思,还没确定好的意思. 就比如,要定义一个方法和类.但还没确定怎么去实现它的具体一点的子方法,那我就可以用抽象类或接口.具体 ...

  5. Zookeeper+websocket实现对分布式服务器的实时监控(附源码下载)

    ​ 我就是个封面 Zookeeper简介 Zookeeper是Hadoop的一个子项目,它是分布式系统中的协调系统. 简单来说就是一个Zookeeper注册同步中心,内部结构为一个树形目录,每个节点上 ...

  6. T-SQL查询处理执行顺序(一)

    对于T-SQL编程,用得最广泛的,莫过于查询(Querying).要想写出高质量.高性能的查询语句,必须深入地了解逻辑查询处理. 一.逻辑查询处理的各个阶段 (5)SELECT DISTINCT TO ...

  7. ASPECTJ 注解。。。

    public interface ISomeService { public void doSome(); public String dade(); } public class SomeServi ...

  8. B/S架构 C/S架构 SOA架构

    一.什么是C/S和B/S 第一.什么是C/S结构.C/S (Client/Server)结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配 ...

  9. swift基础-2

    一.基本运算符 let a = 5 var b = 10 b = a if a = b{ swift 中赋值运算符,并不将自身作为一个值进行返回,所以编译不合法,帮开发者避免错误,很人性化的语言 } ...

  10. SpringBoot热部署的两种方式

    SpringBoot热部署方式一共有两种,分别使用两种不同的依赖 SpringBoot 1.3后才拥有SpringBoot devtools热部署 ①:spring-boot-devtools   ② ...