美文网首页
5.vue组件间的数值传递(父子)

5.vue组件间的数值传递(父子)

作者: 悟空你又瘦了 | 来源:发表于2018-04-09 15:32 被阅读0次

VUE父子组件的关系可以总结为 props down, events up 。

//父组件 newslist 
<template >
 <div class="tmp1">  
         <newsshow :parentMessage="parentMessage"  @childEvent="parentMethod"  ></newsshow>
    ----  :parentMessage="parentMessage"  就是  v-bind:parentMessage="parentMessage"    
      然后子组件props: ['parentMessage'],就可以拿到父组件的parentMessage变量了

    ----  @childEvent="parentMethod"  就是  v-on:childEvent="parentMethod"  
      然后父组件定义一个  parentMethod  函数来处理子组件传递来内容
     this.$emit('childEvent', { name: 'wsh', age:  10 }); ---通过$emit给父组件传递内容
  </div>    
</template>

<script>
    import newsshow from './newsshow';
    export default{             
        data(){
             return {
                parentMessage:'我是来自父组件的消',      
           }
        },

       methods:{        
        parentMethod({ name, age }) {
           console.log( name, age); ---打印出子组件传递来内容
         }
       },
        components:{
            newsshow
        },   

    }

</script>

<style scoped>
    .mui-table-view span{
        position: absolute;
        top: 27px;
        right: 0;
    }
    .mui-table-view .mui-ellipsis{
        margin-top: 10px;
    }
</style>
//子组件 newsshow
<template> 
            <div> 
               <h3>我是子组件一</h3> 
                <span>{{parentMessage}}</span>   ----渲染父组件传递过来的内容
                <textarea ref="commentContent" placeholder="请输入要评论的内容..">111</textarea>
            </div>

</template>

 <script>
     export default{ 
        props: ['parentMessage'],    -----管道,获取父组件的内容 
        mounted() {
          this.$emit('childEvent', { name: 'wsh', age:  10 }); ---通过$emit传递给父组件内容
          var txt = this.$refs.commentContent.value; ---获取textarea 文本框的内容,打印出来
          console.log(txt)          
       },

     }; 

 </script>

  <style scoped>

   </style>

https://www.jianshu.com/p/649929d88699

相关文章

  • 5.vue组件间的数值传递(父子)

    VUE父子组件的关系可以总结为 props down, events up 。 https://www.jians...

  • 5.vue组件间的数值传递(兄弟)

    点击购物车(goodsshows)改变底部购物(App)车徽章数字 核心内容 goodsshow.vue App.vue

  • Vue中组件间传值总结 ------ 2020-05-17

    父子组件间传递数据的方式 1、父组件向子组件传递数据 2、子组件向父组件传递数据 3、父子组件相互传递同一数据的两...

  • Vue组件传值及页面缓存问题

    一、父子组件传值 基本概念在Vue中,父子组件间的数据流向可以总结为prop向下传递,事件向上传递,即父组件通过p...

  • Vue2 组件通信写法总结

    组件通讯包括:父子组件间的通信和兄弟组件间的通信。具体有以下几种情况。 父组件传递数据给子组件 父组件使用 Pro...

  • Vue组件之间的数据传递

    1、父子组件传递父组件传给子组件通过props方法接收参数子组件传给父组件:$emit方法传递参数2、非父子组件创...

  • vue.js系列三:组件间通信

    1.组件间通信基本原则 2.vue 组件间通信方式 2.1组件间通信 1: props(适用于父子组件传递属性) ...

  • Vue常见问题

    1. 父子组件间通信 父组件传递数据给子组件(通过props属性来实现) 子组件通过props来接收数据 子组件与...

  • Vue 2.0 组件间数据传递

    父子组件数据传递 父组件单向传递给子组件 使用v-bind传递 父组件: 子组件: 子组件单向传递给父组件 使用 ...

  • React-组件间通信

    父子间组件通信 父结点数据传递给子组件 通过 props进行传递,子组件只能用于展示或者判断,但不能进行更新当子组...

网友评论

      本文标题:5.vue组件间的数值传递(父子)

      本文链接:https://www.haomeiwen.com/subject/pqgkhftx.html