vue组件值传递之父组件向子组件传递(props)

<template>
    <div class="hello">
        <h1>{{ msg }}</h1>
        <ul>
            <li v-bind="message" v-for="item in message">{{item}}
            </li>
        </ul>
        <test v-bind:child_message="message"></test>
    </div>
</template>

<script>
  export default {
    name: 'Hi',
    data :function() {
      return {
        msg: 'HI',
        message:[11,22,33,44,55,66]
      }
    },
    components:{
      "test":{
        template:`<div><li v-for='item in child_message '>传递到子组件的值:{{item}}</li></div>`,
        props:['child_message']
      }

    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

另:props也可以做值验证 ,和设置默认值

        props:{
          child_message:{
            type:Array,
            default:function(){
              return [1,2,3,4,5,6,74];
            }
          }
        }

猜你喜欢

转载自www.cnblogs.com/fps2tao/p/9668760.html
今日推荐