vue父传子props,怎么设置默认值呢,我的default为什么不行?

vue父传子props,怎么设置默认值呢,我的default为什么不行呢?


父组件 one


        <two :message="ac" :acacacac="aaa"></two>

        

         import two from "./two";

            

        data(){

        return{

            aaa:"",

            ac:"",

        }

    },

    components:{

        two

    },

父组件 two


    <div>

     <div>我是子组件</div>

      {{message}}

      {{acacacac}}

  </div>



 props:{

        message:{

            default:"我是默认值"

        },

        acacacac:{

            default:"我是下一个默认值"

        }

    },

为什么没有效果呢?


有只小跳蛙
浏览 5390回答 2
2回答

喵喔喔

题主可以试试ac: undefined有效而aaa: ""无效,表面上看起来似乎有点智障父组件:&nbsp;data() {&nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; ac: undefined,//有效&nbsp; &nbsp; &nbsp; aaa: ""//无效&nbsp; &nbsp; };&nbsp; }&nbsp;&nbsp;子组件:export default {&nbsp; name: "two",&nbsp; props: {&nbsp; &nbsp; message: {&nbsp; &nbsp; &nbsp; type: String,&nbsp; &nbsp; &nbsp; default: "我是默认值"&nbsp; &nbsp; },&nbsp; &nbsp; acacacac: {&nbsp; &nbsp; &nbsp; type: String,&nbsp; &nbsp; &nbsp; default: "我是下一个默认值"&nbsp; &nbsp; }&nbsp; }};结果:

慕娘9325324

两种写法。&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp;<div>我是子组件</div>&nbsp; &nbsp; &nbsp; {{message}}&nbsp; &nbsp; &nbsp; {{acacacac}}&nbsp; </div>&nbsp;props:{&nbsp; &nbsp; &nbsp; &nbsp; message:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: String,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:"我是默认值"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; acacacac:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: String,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:()=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return '默认值'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript