zoukankan      html  css  js  c++  java
  • 20180821 Vue父组件向子组件传递事件/调用事件

    今天写Vue父组件向子组件传递事件/调用事件  的时候,

    我怎么都不能成功。最后发先是  this.$refs.child 是个数组。 因为我这个子组件调用了多次


    父组件app.vue

    <template>
          <div id="app">
            <!--父组件-->
            <input v-model="msg">
            <button v-on:click="notify">广播事件</button>
            <!--子组件-->
            <popup ref="child" ></popup>
          </div>
        </template>
        <script>
          import popup from '@/components/popup'
          export default {
            name: 'app',
            data: function () {
              return {
                msg: ''
              }
            },
            components: {
              popup
            },
            methods: {
              notify: function () {
                if (this.msg.trim()) {
                  this.$refs.child.parentMsg(this.msg)
                }
              }
            }
          }
        </script>
        <style>
          #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>
    

      子组件popup.vue

         <template>
             <div>
               <ul>
                 <li v-for="item in messages">父组件输入了:{{item}}</li>
               </ul>
             </div>
         </template>
         <style>
             body {
                 background-color: #ffffff;
             }
         </style>
         <script>
           export default{
             name: 'popup',
             data: function () {
               return {
                 messages: []
               }
             },
             methods: {
               parentMsg: function (msg) {
                 this.messages.push(msg)
               }
             }
           }
         </script>
    

      

     

  • 相关阅读:
    [bzoj3123] [Sdoi2013]森林
    [bzoj2173] 整数的lqp拆分
    Linux
    使用高德地图API
    EF具体用在什么类型的项目上
    出现Data Tools 与VS 不兼容问题
    Entity FramWork
    Entity
    Entity
    BASH
  • 原文地址:https://www.cnblogs.com/beiqi/p/9512824.html
Copyright © 2011-2022 走看看