vue中 的传值问题 父传子和子传父

vue中 关于$emit的用法

1、父组件可以使用 props 把数据传给子组件。
2、子组件可以使用 $emit 触发父组件的自定义事件。

vm.$emit( event, arg ) //触发当前实例上的事件

vm.$on( event, fn );//监听event事件后运行 fn;

例如:子组件:

<template>
  <div class="train-city">
    <h3>父组件传给子组件的toCity:{{sendData}}</h3>
    <br/><button @click=‘select(`大连`)‘>点击此处将‘大连’发射给父组件</button>
  </div>
</template>
<script>
  export default {
    name:‘trainCity‘,
    props:[‘sendData‘], // 用来接收父组件传给子组件的数据
    methods:{
      select(val) {
        let data = {
          cityname: val
        };
        this.$emit(‘showCityName‘,data);//select事件触发后,自动触发showCityName事件
      }
    }
  }
</script>

父组件:

<template>
    <div>
        <div>父组件的toCity{{toCity}}</div>
        <train-city @showCityName="updateCity" :sendData="toCity"></train-city>
    </div>
<template>
<script>
  import TrainCity from "./train-city";
  export default {
    name:‘index‘,
    components: {TrainCity},
    data () {
      return {
        toCity:"北京"
      }
    },
    methods:{
      updateCity(data){//触发子组件城市选择-选择城市的事件
        this.toCity = data.cityname;//改变了父组件的值
        console.log(‘toCity:‘+this.toCity)
      }
    }
  }
</script>

结果预览

点击之前:

 点击之后:

原文地址:https://www.cnblogs.com/JonaLin/p/12557227.html

时间: 2024-07-31 03:21:09

vue中 的传值问题 父传子和子传父的相关文章

vue中prop传值时加不加v-bind(冒号:)

前言:有关Vue中父组件通过prop传值给子组件时,是否加v-bind的问题,没弄清楚时感觉很乱,弄清楚之后很简单. 由于结果记起来很容易,所以先给出结果: 只有传递字符串常量时,不采用v-bind形式,其余情况均采用v-bind形式传递. 传入String类型 传入的值title为一个常量(静态prop)时,不加v-bind(或者:) <blog-post title="My journey with Vue"></blog-post> 传入的值title为一

vue父组件给子传参

父: html ....... <child :title="sontitle"></child> ...... js data: sontitle 子: html {{title}} js props:['title'] 就是这么明了

传参-子传父

效果: const App = props => { let alerts = () => { props.alertSs(props.time) } let is = {color:props.color? "red" : "blue"} return <div onClick={alerts} style={is}>{props.time},</div>; } let Apps = ()=>{ let alertSs =

Vue $emit $event 传值(子to父)

事件名 始终使用 kebab-case 的事件名. 通过事件向父组件发送信息 子组件中EnFontsize.vue中$emit <button @click="$emit('enlarge-text')">Enlarge text</button> 父组件中 <template> <div id="app"> <div :style="{ fontSize: postFontSize + 'em' }&

vue中8种组件通信方式

一.props / $emit 下面通过一个例子说明父组件如何向子组件传递数据:在子组件article.vue中如何获取父组件section.vue中的数据articles:['红楼梦', '西游记','三国演义'] // section父组件 <template> <div class="section"> <com-article :articles="articleList"></com-article> <

vue中怎么实现组件通信--自我总结

1.父组件到子组件 父组件 -- > 子组件,使用的props属性传递机制. 在父组件调用子组件时,设置属性 Vue.component('child', { // 声明 props props: ['message'], // 就像 data 一样,prop 可以用在模板内 // 同样也可以在 vm 实例中像 "this.message" 这样使用 template: '<span>{{ message }}</span>' }) 设置message属性

JavaScript(Iframe、window.open、window.showModalDialog)父窗口与子窗口之间的操作

一.Iframe 篇 公共部分 //父对象得到子窗口的值 //ObjectID是窗口标识,ContentID是元素ID function GetValue(ObjectID,ContentID) { var IsIE = (navigator.appName == 'Microsoft Internet Explorer') if(IsIE) {//如果是IE alert(document.frames(ObjectID).document.getElementById(ContentID).i

总结js(Iframe、window.open、window.showModalDialog)父窗口与子窗口之间的操作

http://hi.baidu.com/yashua839/blog/item/131fdb2fe547ef221f3089af.html一.Iframe 篇 //&&&&&&&&&&&&&&&&&&&&公共方法开始&&&&&&&&&&&&&&a

父表、子表 主外键关系

ORACLE官方文档介绍: Concurrency Control, Indexes, and Foreign Keys You almost always index foreign keys. The only exception is when the matching unique or primary key is never updated or deleted.(你总是需要对 外键添加索引! 唯一的例外就是:匹配的主键列 或是 唯一列 从不进行更新操作或者 删除操作) Oracle