问题
Which of the following is better practice?
{{my-component title=model.title body=model.body}}
or
{{my-component model=model}}
Is there any difference between the two, or does it not really matter?
回答1:
It's difficult to not subjectively judge something like this. But let's see, what we can do from a few different standpoints.
First you could argue, that you can infer which properties are required for this component to function, the second not so much. That being said, we're not really even able to infer much more about the properties the component require, like what types they need to be etc.
The second standpoint would be, simplicity in the second component make it more attractive/clean. Especially when the properties you require for the component start getting out of control.
This could easily later look something like.
{{my-component
title=model.title
body=model.body
extraprop1=model.prop
extraprop2=parent.prop
}}
For now, I would say, use what makes sense on a component by component basis, and don't over think it.
You could consider integrating something like TypeScript into your project, and maybe start explicitly defining what type of property you need where in a component, without really having to worry about the api you expose to component via the template.
Here is a twiddle that demonstrates a simple test for those properties.
来源:https://stackoverflow.com/questions/36876502/passing-parameters-to-ember-components-naming-convention