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" :ctitle='title'> </comp>
    </div>

    <template id="comp">
      <div id="">
        <p>{{cmessage}}</p>
        <ul>
          <li v-for="item in cfruit">{{item}}</li>
        </ul>
        <p v-for="(value, key) in ctitle">{{key}}: {{value}}</p>
      </div>
    </template>

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

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

posted @ 2021-07-06 10:38  thomas_blog  阅读(78)  评论(0编辑  收藏  举报