vuejs / rfcs

RFCs for substantial changes / feature additions to Vue core
4.87k stars 546 forks source link

Components registration with props #143

Closed yzx9 closed 4 years ago

yzx9 commented 4 years ago

Provide default props on component registration when define component.

const Panel = {
  components: {
    Card: [Card, { shadow: 'hover' }]
  },
  template: `<Card />` // Is equivalent to: <Card shadow="hover">
}

Rendered

posva commented 4 years ago

The motivation part is, to me, way too short given the small improvement this brings.

Even though, this can just be a helper function:

import { resolveComponent, h } from 'vue'

function fixedProps(component, propsData) {
  // this example is missing how things should be properly passed, but it doesn't change much
  return () => h(typeof component === 'string' ? resolveComponent(component) : component, {...propData}, children)
}

and then use that:

components: {
  Card: fixedPropd(Card, { shadow: 'hover' })
}

So I don't think there is a need to add another api for components

jacekkarczmarczyk commented 4 years ago

@posva how'd you do that with components that are already registered (i.e. coming from some lib) and you want to set default props globally and not just within one components object?

posva commented 4 years ago

@jacekkarczmarczyk with resolveComponent to retrieve the component, which could be done by the function itself if it gets a string (I updated the example)

jacekkarczmarczyk commented 4 years ago

Maybe I wasn't clear enough, I meant that I don't want to use components option for components that are registered globally. Imagine for example I want exaxt prop of a router-link to be always true, without need of adding it to components option as it is done in your example

CyberAP commented 4 years ago

I'd better go with a component wrapping for this one. It's already an established practice and works perfectly fine. Could you please expand on why component wrapping doesn't fit you and how this proposal fixes the issue?

yzx9 commented 4 years ago

Seems implement this feature as a lib is the better way.