• vue子父组件的通信


    Element使用的是Vue2.0版本,众所周知在Vue 1.0升级到2.0中去除了$broadcast和$dispatch方法。

    1、父组件向子组件传值

    a、app.vue父组件

    <template>
      <div id="app">
        <h1>{{title}}</h1>
        <h1 v-text="title"></h1>
        <h1 v-html="title"></h1>
        <input type="text" v-model="newItem" v-on:keyup.enter="addNew">
        <ul>
            <li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click="toggleFinish(item)">
            {{item.label}}
            </li>
        </ul>
      <componentA msgfromfather='hongMaJu'></componentA>
       <component-a></component-a>
      </div>
    </template>
    
    <script>
    import Store from './Store'
    import componentA from './components/componentA'
    //console.log(Store)
    export default {
      
       data () {
        return {
          title: '<span>?</span>this is a todolist',
          items:Store.fetch(),
          // items:[
          //     {
          //       label:'coding',
          //       isFinished:false
      
          //     },
          //     {
          //       label:'walking',
          //       isFinished:true
      
          //     }
          // ],
          newItem:''
        }
      },
      components:{componentA},
      watch:{
        items:{
          handler:function(items){
            // console.log(val,oldVal)
             // console.log(items);
    
            Store.save(items);
             // console.log(items);
    
          }
        },
        deep:true
      },
      methods:{
        toggleFinish:function(item){
          // console.log(item);
          item.isFinished=!item.isFinished;
          console.log(this.items);
        },
        addNew:function(){
          // this.newItem;
          // console.log(this.newItem);
          this.items.push({
            label:this.newItem,
            isFinished:false
          })
          this.newItem='';
        }
      }
    }
    </script>
    
    <style>
    .finished{
      text-decoration:underline;
    }
    #app {
      font-family: 'Avenir', Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
    }
    </style>

    b、componentA.vue子组件

     <<template>    
    <div class="hello">
        <h1>{{msg}}</h1>
        <button v-on:click="onClickMe">click me!</button>
        <h1>{{msgfromfather}}</h1>
    </div>
     </template>
     <script>
         export    default{
             data:function(){
                 return{
                     msg:'hello from component a'
                 }
             },
             props:['msgfromfather'],
             methods:{
                 onClickMe:function(){
    console.log(this.msgfromfather);
                 }
             }
         }
    
     </script>
    
     <style>
    h1{
        color: #42b983;
    }
     </style>

    c、效果

    2、子组件向父组件传值

    a、app.vue父组件

    <template>
      <div id="app">
        <h1>{{title}}</h1>
        <h1 v-text="title"></h1>
        <h1 v-html="title"></h1>
        <input type="text" v-model="newItem" v-on:keyup.enter="addNew">
        <ul>
            <li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click="toggleFinish(item)">
            {{item.label}}
            </li>
        </ul>
        <p>child tells me:{{childwords}}</p>
      <componentA msgfromfather='hongMaJu' v-on:child-tell-me-something='listenToMyBoy'></componentA>
     <!--   <component-a></component-a> -->
      </div>
    </template>
    
    <script>
    import Store from './Store'
    import componentA from './components/componentA'
    //console.log(Store)
    export default {
      
       data () {
        return {
          title: '<span>?</span>this is a todolist',
          items:Store.fetch(),
          // items:[
          //     {
          //       label:'coding',
          //       isFinished:false
      
          //     },
          //     {
          //       label:'walking',
          //       isFinished:true
      
          //     }
          // ],
          newItem:'',
          childwords:''
        }
      },
      components:{componentA},
      watch:{
        items:{
          handler:function(items){
            // console.log(val,oldVal)
             // console.log(items);
    
            Store.save(items);
             // console.log(items);
    
          }
        },
        deep:true
      },
      methods:{
        toggleFinish:function(item){
          // console.log(item);
          item.isFinished=!item.isFinished;
          console.log(this.items);
        },
        addNew:function(){
          // this.newItem;
          // console.log(this.newItem);
          this.items.push({
            label:this.newItem,
            isFinished:false
          })
          this.newItem='';
        },
        listenToMyBoy:function(msg){
          this.childwords=msg;
        }
      }
    }
    </script>
    
    <style>
    .finished{
      text-decoration:underline;
    }
    #app {
      font-family: 'Avenir', Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
    }
    </style>

    b、componentA.vue子组件

     <<template>    
    <div class="hello">
        <h1>{{msg}}</h1>
        <button v-on:click="onClickMe">click me!</button>
        <h1>{{msgfromfather}}</h1>
    </div>
     </template>
     <script>
         export    default{
             data:function(){
                 return{
                     msg:'hello from component a'
                 }
             },
             props:['msgfromfather'],
             methods:{
                 onClickMe:function(){
                // console.log(this.msgfromfather);
                this.$emit('child-tell-me-something',this.msg)
                 }
             }
         }
    
     </script>
    
     <style>
    h1{
        color: #42b983;
    }
     </style>

    c、效果

     

  • 相关阅读:
    【NIPS 2018】完整论文下载链接
    【今日CV 视觉论文速览】30 Nov 2018
    【超好玩的在线AI项目】浏览器里玩AI------持续更新
    hdu 4035 Maze 概率DP
    hdu 4089 Activation 概率DP
    hdu 4405 Aeroplane chess 概率DP
    zoj 3329 One Person Game 概率DP
    poj 2096 Collecting Bugs 概率DP
    poj 3744 Scout YYF I
    2013 Multi-University Training Contest 5 Partition
  • 原文地址:https://www.cnblogs.com/hongmaju/p/6847494.html
Copyright © 2020-2023  润新知