vue父子组件通信

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  </head>

  <body>
    <div id="app">
      <!-- v-bind -->
      <comp :cmessage="message" :cfruit="fruit"> </comp>
    </div>

    <template id="comp">
      <div id="">
        <p>{{cmessage}}</p>
        <ul>
          <li v-for="item in cfruit">{{item}}</li>
        </ul>
      </div>
    </template>

    <script>
      // 父传子 props
      const comp = Vue.extend({
        template: '#comp',
        props: ['cmessage', 'cfruit']
      })

      // root
      const app = new Vue({
        el: '#app',
        data: {
          message: 'Hello Vue.js!',
          fruit: ['苹果', '桃', '香蕉']
        },
        components: {
          comp,
        }
      })
    </script>
  </body>
</html>

posted @ 2021-07-05 23:44  thomas_blog  阅读(30)  评论(0编辑  收藏  举报