vue components
A vue component is a way to create an asset of sorts for a vuejs instance. It is similar to but also very different from vue extend that can be used to create custom vuejs constructors.
A component has a name assigned to it, and can be used as a way to make custom elements that can be used in templates. There is a great deal to cover about components, as well as the many other vuejs related topics that a developer should be up to speed with before hand, but lets take a look at some basic vue component examples for starters.
1 - Some basic examples of
In this section I will be going over just a few quick basic examples of vue components. There is just making a simple hello world style component that is a good start, but then there are two general ideas that I think I should get solid thus far with components which are properties and events.
Properties are for passing values from a parent vue instance, to a component while events are for passing things back to a parent. So lets get some of these basic examples out of the way so we can more on to some real examples that might prove to be a little fun.
1.1 - vue component basic example
Here is a simple counter example of a vue component, it makes use of template, data, and method options. Many of the options that are used when making a plain old Vue instance can also be used with components as well, with some exceptions such as vue el. To create a component I call the vue component method, followed by the name I want to set for the component, then an object. In this object I can used most of the options that I would also use for a regular plain old Vue Instance including templates, render methods, data and more. Once I have my component I can then use the component in a template of a vuejs instance.
|
|
S0 then a component is a good way of going about breaking what would otherwise be a more complex vuejs project into smaller parts. These parts can be used just once, or a whole bunch of times. So in some cases starting to make components is just what needs to happen when I start to work on real vuejs projects.
1.2 - Adding properties to a custom vue component tag
To add properties to a component I just need to use the props option. The value for the props option can be an array of strings for each property name or an object syntax.
|
|
In this example I am defining an si property that can be used to set the starting index of the step element. With the object syntax it is possible to set defaults and much more, but that is a matter for another post.
|
|
1.3 - Now for some events
The props of a component are for sending state data to a component, but the prop values should be treated as read only. When it comes to creating a component that is just going to render some data in a main parent data object this is not a problem. However what if I do want to mutate a prop value? Well it is not a good idea to just mutate the prop in the component directly, however it is a good idea to set up some emit some events in the component.
In this example I am making use of props as a way to send main state data to a component, but then I am also using events as a way to trigger the mutation of main state data.
|
|
3 - Conclusion
That is it for not of course there is a great deal more to write about with vue components. My content on vuejs is still new, and I could stand to log some more hours writing demos, and some actual projects with vuejs. This is a post that I will likely come back to at some point and expand this one, if you have anything to add in the comments that might help to expedite that