Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v-bind support binding multiple objects #550

Closed
sqal opened this issue Jul 12, 2021 · 7 comments
Closed

v-bind support binding multiple objects #550

sqal opened this issue Jul 12, 2021 · 7 comments

Comments

@sqal
Copy link

sqal commented Jul 12, 2021

What problem does this feature solve?

Currently, if we want to pass attributes from multiple sources we have to manually import mergeProps helper and do

<MyComponent v-bind="mergeProps($attrs, featAProps, featBProps)" />

or use computed property

It would be better if we could just do:

<script setup>
  const featAProps = useFeatureA()
  const featBProps = useFeatureB()
</script>

<template>
  <MyComponent v-bind="[$attrs, featAProps, featBProps]" />
</template>

What does the proposed API look like?

Allow passing an array of props to v-bind v-bind="[x,y,..]"

@sqal sqal changed the title v-bind supports binding multiple objects v-bind support binding multiple objects Jul 12, 2021
@cyfung1031
Copy link

cyfung1031 commented Sep 23, 2021

Currently most scripts use {...$attrs, ...featAProps, ...featBProps}
Is it really neccessary to introduce the array form?

@LinusBorg
Copy link
Member

I agree, I don't think we need to add another variation to this API.

@cyfung1031
Copy link

cyfung1031 commented Sep 23, 2021

@posva I think this is not a issue...
we have native ES syntax for combining multiple objects
{...$attrs, ...featAProps, ...featBProps}
I think this issue can be closed.

@sqal
Copy link
Author

sqal commented Sep 23, 2021

@cyfung1031 using spread operator or Object.assing would result in overriding props instread of merging them. What if featAProps have onClick/class and featBProps as well? that's why you have to use mergeProps

@cyfung1031
Copy link

cyfung1031 commented Sep 23, 2021

@cyfung1031 using spread operator or Object.assing would result in overriding props instread of merging them. What if featAProps have onClick/class and featBProps as well? that's why you have to use mergeProps

This is another issue. You are not asking for binding multiple objects. You are asking for binding multiple methods to the same attribute in the same component.

You should open another issue for this. They are different.

The working principle for v-bind is based on the key-value relationship.

@KaelWD
Copy link

KaelWD commented Nov 29, 2021

This seems to be supported (accidentally?) only if you have other attributes bound too.

// Works
<MyComponent foo="bar" v-bind="$attrs, featAProps, featBProps" />
// Doesn't
<MyComponent v-bind="$attrs, featAProps, featBProps" />

This is because attributes + v-bind is compiled directly to a mergeProps call:

_createBlock(_component_MyComponent, _mergeProps({ foo: "bar" }, _ctx.$attrs, _ctx.featAProps, _ctx.featBProps), null, 16 /* FULL_PROPS */)

But v-bind alone uses guardReactiveProps instead which only accepts a single argument:

_createBlock(_component_MyComponent, _normalizeProps(_guardReactiveProps(_ctx.$attrs, _ctx.featAProps, _ctx.featBProps)), null, 16 /* FULL_PROPS */)

Related: vuejs/eslint-plugin-vue#1731

@ThaDaVos
Copy link

This seems to be supported (accidentally?) only if you have other attributes bound too.

// Works
<MyComponent foo="bar" v-bind="$attrs, featAProps, featBProps" />
// Doesn't
<MyComponent v-bind="$attrs, featAProps, featBProps" />

This is because attributes + v-bind is compiled directly to a mergeProps call:

_createBlock(_component_MyComponent, _mergeProps({ foo: "bar" }, _ctx.$attrs, _ctx.featAProps, _ctx.featBProps), null, 16 /* FULL_PROPS */)

But v-bind alone uses guardReactiveProps instead which only accepts a single argument:

_createBlock(_component_MyComponent, _normalizeProps(_guardReactiveProps(_ctx.$attrs, _ctx.featAProps, _ctx.featBProps)), null, 16 /* FULL_PROPS */)

Related: vuejs/eslint-plugin-vue#1731

Is this still considered a bug or is it a feature? It seems to still work in Vue 3 without any errors and I only have a separate class attribute on it, not even a prop

@haoqunjiang haoqunjiang transferred this issue from vuejs/core Sep 1, 2023
@vuejs vuejs locked and limited conversation to collaborators Sep 1, 2023
@haoqunjiang haoqunjiang converted this issue into discussion #551 Sep 1, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

6 participants