zl程序教程

您现在的位置是:首页 >  前端

当前栏目

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

Vue组件 传递 Props
2023-09-11 14:19:38 时间

 

 

<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];
            }
          }
        }