This will be a quick post on a basic vue list example. When working with unordered or ordered list elements in a template, typically I will end up using the vue for directive to bind to an array in the vue data object.
However there is just having a basic list example, and then there is staring to create an actual application that is a basic todo list application. So this will just be a pretty simple vuejs example of a todo list when it comes to getting this one out of the way.
In this section I will be going over the source code of my basic todo app vuejs example. The sore code for this example as well as all of my other vuejs examples can be found at by test vuejs repository at github.
For this example I have a single mixins file where I am adding in a bunch of methods that I may use across more than one vuejs instance. So var I just have a main vuejs instance and a single component, but if i do continue working on this example a little now and then I will want to have some global methods.
I have methods that I am all ready using in one component that have to do with creating an array of values from an id attribute of an element in a template. You see I often find myself encoding info into an id of an element, rather than making use of the data attribute of an html element. So I have some methods that I can use to quickly get at things that might be encoded into an id of a target element in an event object. More on this later when I get into components with this example.
In the the methods mixin here I also have a create item method. I am going to want to have a standard way of going about creating item objects for a list and for now this is where I am choosing to park such a method. So far I am just using this method in the main vue instance, but even there I am using it to push new items into a list, but also using it as a way to load a list from local storage also via a mounted hook.
Speaking of local storage I am also placing save and lod methods that make use of the local storage api as a way to save the state of a list.
Here I have a component that is used to render a single list item in the current array of list items in the data object of the main vue instance. Here I am making use of the Vue.component method to make this list-item component a global component.
In the static template of this component I am using the v-if directive as a way to render a text input element in the event that an item is not done. This way the text of the item can still be updated by making use of this text input element. If the event that the item is done, then just a span element will be rendered to display the message text, and a done text class will be used with the element that will contain appropriate style for a done element such as line through style text.
In this component I am then emitting events that will then be used as a way to update data in the main vuejs instance. I have to do things this way because I can not mutate the props of the component, so events are the way to indirectly mutate data that is back in the parent vue instance that I will be getting to next.
I then have a main.js file that contains the main vuejs instance that will be rendering to the hard coded html file. In the static template I am using the list-item component with the v-for directive as a way to create an instance for the component for each item in the items array of the data object for this main, or parent vue instance.
This is the old example that I made for this post. I do not think this is a good way to get started with this kind of application. However for now I think I will keep this here just for the hell of it.
So here is the basic copy and past vue list example that I put together for this post. The only fix you might have to do is the link to vuejs as needed. The basic process is to start out with a vue instance by calling the main vue constructor with the new keyword, and then pass an options object. In that options object I am using the vue el directive to mount to a div element in my html, I have a template, data object, and methods object also.
In the template I have a div as a root element because I must have one for a vue template, I then have an text input element and a a button type input element that when clicked will add a new list item with a message value given in the text input element. I then have a ul element and I am using the vue for directive to create li elements for each element in the items array.
There is then the methods object where I have a push new method that is called by the button that creates new items in the items array of the data object that is also used to create the list in the template. There is also a delete item method that will be called by a delete item button for each list item when clicked, it will of course delete the item.
So the todo list app is just a basic idea for an application that is often what is used as a way to test out a framework.
There is noting ground breaking that I wanted to do with this vuejs example, but that is often the aim of such a project anyway. However if I do put more time into this I guess there are at least a few things that come to mind when it comes to adding features. It would be nice to save and load lists as plain text files on a local file system rather than just depending on the local storage api. However there is only so much more that I would want to add when it comes to an application such as this.