V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
coolair
V2EX  ›  问与答

Vue 如何知道父组件传递了数据给子组件?

  •  
  •   coolair · Nov 6, 2020 · 1761 views
    This topic created in 2008 days ago, the information mentioned may be changed or developed.

    子组件 Prop 一个参数:

    height: {
        type: Number,
        default: 800,
    }
    

    在父组件中引用子组件

    <child :height=height />
    <child  />
    

    在子组件的 created 方法中有没有办法知道父组件到底有没有传值过来?就是有没有在父组件中有没有写上:height=height ?

    还有一个问题:

    <child :height=height @test="test" />
    

    在这种情况下,有没有什么好的方法保证 height 和 @click 同时设置或者同时不设置?因为我这个 test 是为了修改 height 的值去更新子组件,要是没有设置 height,那这个 test 就没有意义了。

    6 replies    2020-11-06 17:10:47 +08:00
    Mist0
        1
    Mist0  
       Nov 6, 2020
    required:true
    leefly
        2
    leefly  
       Nov 6, 2020
    watch 下这个 height ?
    huijiewei
        3
    huijiewei  
       Nov 6, 2020
    你说的和设置不设置有什么关系么

    只要子组件定义了就好了
    maroon5
        4
    maroon5  
       Nov 6, 2020
    watch 或 computed?
    dinjufen
        5
    dinjufen  
       Nov 6, 2020
    你的 height 有默认值,传过来不等于默认值可约等于有传值?从结果上讲是这样吧。。如果传的刚好等于默认值那结果不还一样吗
    ghjacky
        6
    ghjacky  
       Nov 6, 2020
    watch 一下这个 prop 不就行了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1512 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 16:43 · PVG 00:43 · LAX 09:43 · JFK 12:43
    ♥ Do have faith in what you're doing.