-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
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
Warn when component option should be an object, but is a function #5605
Comments
There's nothing to be embarrassed by 🙂 |
ping @phoenixeliot |
It's less a lack of understanding and more going through the boilerplate a little too fast, and then later on when actually using the options, things fail in ways that aren't clear. When making a new component, I'll just throw in the common options to start with, then fill them out later, just to take the mental overhead out of remembering exactly what they're called. (We haven't been using Vue for terribly long, so I don't have them down by heart yet). Also, side note: We use CoffeeScript. But I think in ES6 notation it could look pretty similar. So I'll throw together a new component and it'll look something like this (when done right):
And it's easy to not notice if I put a
But then when I change Also note: This usually happens like, the next day, when I'm adding some new functionality, and thus don't have the boilerplate fresh in my head and am (erroneously) assuming it works because I hadn't seen any problems. |
you must put your data inside bracket and then return it |
like this: |
If properties that needed to be functions had verb prefixes e.g. |
What problem does this feature solve?
I often get mixed up on which properties of a component should be functions and which shouldn't, and mistakenly define some as functions when they should have been objects.
In the opposite scenario, when a property that needs to be a function is instead an object, we already get a very helpful warning message:
[Vue warn]: The "data" option should be a function that returns a per-instance value in component definitions.
But if you define the
watch
,methods
,created
, orcomputed
properties (and probably some others) as functions instead, Vue either silently fails to use the property at all, or gives a confusing error message. I've spent an embarrassing amount of my Vue dev time because of this simple mistake.Here's what those ones do if the value is a function instead of an object:
watch
will simply be ignored if it's a function instead of an object.components
will prevent any children from rendering and log this message:Unknown custom element: <child-component> - did you register the component correctly? For recursive components, make sure to provide the "name" option. (found in root instance)
methods
will cause a runtime error with this message:[Vue warn]: Property or method "someMethod" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in root instance)
computed
— similar errormethods
Most of these errors are at least somewhat misleading as to what the real problem is.
The main benefit of a warning for this kind of mistake would be to spend less time futzing around with fixing the boilerplate after getting it slightly wrong.
What does the proposed API look like?
If a component is initialized with any properties whose values are functions, but should be objects, emit a warning similar to the warning for
data
. eg:[Vue warn]: The "computed" option should be an object, not a function, in component definitions.
The text was updated successfully, but these errors were encountered: