vue2.0组件之间的传值--新入坑,请指教

prop down   emit up

嘿嘿    如果是第一次接触vue2.0组件传值的肯定很疑惑,这是什么意思(大神总结的,我也就是拿来用用)

“down”—>指的是下的意思,即父组件向子组件传值,用props;“up”—>指的是上的意思,即子组件想父组件传值,用emit。

1.子组件向父组件的传值:

Child.vue

<template>  <div class="child">    <h1>子组件</h1>    <button v-on:click="childToParent">想父组件传值</button>  </div></template><script>  export default{    name: ‘child‘,    data(){      return {}    },    methods: {      childToParent(){        this.$emit("childToParentMsg", "子组件向父组件传值");      }    }  }</script>

parent.vue
<template>  <div class="parent">    <h1>父组件</h1>    <Child v-on:childToParentMsg="showChildToParentMsg" ></Child>  </div></template><script>  import Child from ‘./child/Child.vue‘  export default{      name:"parent",    data(){      return {      }    },    methods: {      showChildToParentMsg:function(data){        alert("父组件显示信息:"+data)      }    },    components: {Child}  }</script>

2.父组件向子组件传值

parent.vue

<template>  <div class="parent">    <h1>父组件</h1>    <Child v-bind:parentToChild="parentMsg"></Child>  </div></template><script>  import Child from ‘./child/Child.vue‘  export default{     name:"parent",    data(){      return {        parentMsg:‘父组件向子组件传值‘      }    },    components: {Child}  }</script>

child.vue

<template>  <div class="child">    <h1>子组件</h1>    <span>子组件显示信息:{{parentToChild}}</span><br>  </div></template><script>  export default{    name: ‘child‘,    data(){      return {}    },    props:["parentToChild"]  }</script>

3.采用eventBus.js传值---兄弟组件间的传值

eventBus.js

import Vue from ‘Vue‘

export default new Vue()

App.vue

<template>  <div id="app">    <secondChild></secondChild>    <firstChild></firstChild>  </div></template>

<script>import FirstChild from ‘./components/FirstChild‘import SecondChild from ‘./components/SecondChild‘

export default {  name: ‘app‘,  components: {    FirstChild,    SecondChild,  }}</script>

FirstChild.vue

<template>  <div class="firstChild">    <input type="text" placeholder="请输入文字" v-model="message">    <button @click="showMessage">向组件传值</button>    <br>    <span>{{message}}</span>  </div></template><script>  import bus from ‘../assets/eventBus‘;  export default{    name: ‘firstChild‘,    data () {      return {        message: ‘你好‘      }    },    methods: {      showMessage () {       alert(this.message)        bus.$emit(‘userDefinedEvent‘, this.message);//传值      }    }  }</script>

SecondChild.vue

<template>    <div id="SecondChild">        <h1>{{message}}</h1>    </div></template><script>    import bus from ‘../assets/eventBus‘;    export default{        name:‘SecondChild‘,        data(){            return {                message: ‘‘            }        },        mounted(){            var self = this;            bus.$on(‘userDefinedEvent‘,function(message){                self.message = message;//接值            });        }    }
时间: 2024-08-10 02:11:02

vue2.0组件之间的传值--新入坑,请指教的相关文章

Vue2.0组件之间通信

Vue中组件这个特性让不少前端er非常喜欢,我自己也是其中之一,它让前端的组件式开发更加合理和简单.笔者之前有写过一篇Vue2.0子父组件通信,这次我们就来聊一聊平级组件之间的通信. 首先我们先搭好开发环境,我们先得装好git和npm这两个工具(如果有不清楚的同学请自行百度哦) 环境搭建步骤: 打开git ,运行 npm install --global vue-cli 这是安装vue的命令行 vue init webpack vue-demo 这是vue基于webpack的模板项目 cd vu

vue2.0 组件之间的数据传递

组件间的数据传递// 父组件<template><div class="order"><dialog-addpro v-on:closedialog="close" :proinfo="proinfo"></dialog-addpro></div></template><script>import daddpro from '../../daddpro' expo

通信vue2.0组件

vue2.0组件通信各种情况总结与实例分析 Props在vue组件中各种角色总结 在Vue中组件是实现模块化开发的主要内容,而组件的通信更是vue数据驱动的灵魂,现就四种主要情况总结如下: 使用props传递数据---组件内部 //html <div id="app1"> <i>注意命名规定:仅在html内使用my-message</i> <child my-message="组件内部数据传递"></child&

vue 学习五 深入了解components(父子组件之间的传值)

上一章记录了 如何在父组件中向子组件传值,但在实际应用中,往往子组件也要向父组件中传递数据,那么此时我们应该怎么办呢 1.在父组件内使用v-on监听子组件事件,并在子组件中使用$emit传递数据 //父组件 <template> <div id="home"> <com test='12345' test2='1213232111111' @a_even="a_even"></com> //使用v-on监听子组件事件

Vue2.0组件注册

//全局注册: Vue.component("my-component",{ template:'<div>A custom component!</div>' }); /*交换位置会报错----创建组件必须在根实例化之前*/ //创建根实例: new Vue({ el:"#app" }) --------- //局部注册: new Vue({ el: "#app1", components: { // <my-zu

Vue组件之间的传值方法

在vue当中有两种组件之间的传值方法,分别是 * 父子组件* 之间的传值和* 非父子组件 *之间的传值方法 父子组件之间的传值方法 父子组件之间的传值分为两种 * 父组件给子组件传值 子组件给父组件之间的传值 父组件给子组件传值方法 // 父 <div id = "app"> <my-content></my-content> </div> // 子 <template id="content"> <

事件总线bus解决兄弟组件之间的传值

原文引用https://www.dazhuanlan.com/2019/08/25/5d625951eff92/ 事件总线bus解决兄弟组件之间的传值 实际运用: 封装一个Bus.js 123 import Vue from 'vue'const Bus = new Vue()export default Bus 在组件调用时引入 组件一: 1234567891011121314 import Bus from './Bus' export default { data() { return {

vue中组件之间的传值

按照对象分类: 一.父子组件之间的传值 1.使用props和$emit 2.在引入的子组件中使用ref,通过this.$refs.xxx.方法/值来获取子组件中的方法或者值 3.子组件中使用this.$parent来获取父组件中的值或者方法 4.父组件中使用this.$children来获取子组件中的值或者方法 二.兄弟组件中的传值 1.使用eventBus作为中间件,然后使用$emit去抛出事件,使用$on去监听事件 这里要注意一些事项:a.bus.$emit在beforeDestroy中去触

vue 组件之间的传值

组件之间的传值有三种  1,父组件给子组件传值 ,   2,子组件给父组件传值   3, 非父子组件之间的传值(又叫兄弟组件传值) 1.父组件给子组件传值   (父组件给子组件传值需要通过props) 首先我的页面结构是这样的  child是子组件   father是父组件, 在这里我新建一个父组件  father.vue <template> <div> <div>这是父组件</div> <!-- 使用组件child --> <child