-
Notifications
You must be signed in to change notification settings - Fork 592
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
How do you pass props to components in v2.0 ? #176
Comments
Thank your suggestion! Now all the item props only come from But from your question and this pr #177, and someone who ask me in email, it's really a problem I didn't take into consideration, and it's really not flexible enough. So I come up an idea to solve it (adding a The parent(root) component like that: <template>
<div>
<virtual-list
:data-key="'id'"
:data-sources="items"
:data-componnet="itemComponent"
// adding data-props to pass a extra props.
:data-props="{ otherValue1, otherValue2 }"
></virtual-list>
</div>
</template>
<script>
export default {
name: 'root',
data () {
return {
otherValue1: 'abc',
otherValue2: 'xyz',
itemComponent: Item,
items: [{ id: '1', foo: 'xx'}, { id: '2', foo: 'yy'}],
}
}
}
</script> And child(item) component like that: <script>
export default {
name: 'item-component',
props: {
// from data-props(parent data) special designation
otherValue1: String,
otherValue2: String,
// from data-sources original array, here is: { id: '1', foo: 'xx'}
source: Object
}
}
</script> What do you think this idea? Welcome to provide more ideas and suggestions. If it is acceptable, I will support and update into next version. |
I think this approach resolves the problem estated by @MOSMekawy. And, keeps the workflow reasonable simple. |
@tangbc I read your reply a second time thoroughly and while this is helpful permitting providing props outside the list items but doesn't solve the problem of having to list all the keys in a list item in case of complex objects such as mentioned in pr #177 so if it's possible to pass the the whole object as prop to the child component this would be useful. |
@MOSMekawy In item component <virtual-list
:data-key="'id'"
:data-sources="items"
:data-componnet="itemComponent"
// adding data-props to pass a extra props.
:data-props="{ extraProp: { otherValue1, otherValue2 } }"
></virtual-list> Maybe is usefull but I don't think it's flat enough, not convenient to make every prop type definition. |
I wonder if there's any way to keep props flat and also can unified it in a whole object. |
I don't know about your underlying implementation but how about by default the whole object is passed down to child component unless the developer provides a some sort of a reducer function like in vue-router. For example
|
Published <template>
<div>{{ source.text }} - {{ otherPropValue }}</div>
</template>
<script>
export default {
name: 'item-component',
props: {
source: {
type: Object,
default () {
return {}
}
},
otherPropValue: String
}
}
</script> |
What i concluded is that the props of the component should be the same as the keys in the data. That's not convenient. is there any other way ?
The text was updated successfully, but these errors were encountered: