Subscribe on changes!

When a custom component is used as the root, attr can override explicitly set prop

avatar
Jan 27th 2022

Version

3.2.29

Reproduction link

sfc.vuejs.org/

Steps to reproduce

The above demo can show

What is expected?

1.attr should not be automatically passed from parent component to child component 2.Under the above premise, if attr and prop have the same name, attr should not overwrite prop 3.When a custom component is used as a root, it should behave as if it were wrapped in a div

What is actually happening?

1.attr(a) on Comp is automatically passed to CompChild, but not on CompA 2.attr (name) in comP is passed to props (name) of CompChild

avatar
Jan 27th 2022

That's working as intended and also worked like this in Vue 2 already.

avatar
Jan 27th 2022

I'll reopen this to re-evaluate one specific thing: attrs from parent overrwriting explicit props in the child, like this: Playground

That doesn't seem to be exactly desirable and would have worked differently in Vue 2 I_think_

avatar
Jan 28th 2022

one can also disable attributes inheritance

inheritAttrs:false

and manually pass attributes to the next child

// age is passthrough
// name is override  
<CompChild name="Set by comp" v-bind:age=$attrs.age></CompChild>

SFC Playground

avatar
Jan 29th 2022

I'll reopen this to re-evaluate one specific thing: attrs from parent overrwriting explicit props in the child

I don't think the fact it's is a prop or an attribute matters.

Since name is merged from App.vue template into Comp.vue, it makes sense for it to override the attribute/prop (going top down).

avatar
Jan 29th 2022

Yeah from that perspective I have to agree.

avatar
Jan 30th 2022

It's an edge-case, but a breaking change that we so far haven't documented.

https://jsfiddle.net/Linusborg/qoxj71f8/6/

avatar
Jan 30th 2022

App.vue > Comp.vue > Inner.vue

there is importance for name being attribute in Comp.vue attributes are expected to be applied to the root element,

which is then applied to <inner> root element, overriding the explicit prop/attribute

documented https://v3.vuejs.org/guide/component-attrs.html#attribute-inheritance image

props don't fall through for root elements.

avatar
Feb 2nd 2022

In the context of Comp.vue, name is a non-prop attribute - Comp.vue doesn't have a prop named name defined.

So the behavior matches the docs. it's just a subtle breaking change compared to Vue 2 that we so fare haven't documented.

avatar
Feb 3rd 2022

I would say this is the more "correct" and expected behavior compared to Vue 2. name is not declared by Comp so it gets passed on to CompChild, and because it's from higher-up, it has higher priority.

Also considering that changing this would technically constitute a breaking change in Vue 3, I think this is a wontfix.