1. Download the template
Execute in the directory where the project is stored:
git clone https://github.com/tastejs/todomvc-app-template.git
2. Installation dependencies
Enter the project directory
cd todomvc-vue
Install dependencies in the project directory
yarn
3. Introduce vue
Install vue
yarn add vue
In index.html
introducing the vue
<script src="node_modules/vue/dist/vue.js"></script>
In the app.js
creation vue object
( function ( Vue ) {
new Vue ( {
el : "#todoapp" ,
} )
} ) ( Vue )
And index.html
mount it to the DOM element in ( #todoapp
)
<section class="todoapp" id="todoapp">...</section>
After completing the above operations, open it with a browser index.html
. If the interface is as follows, the project initialization is successful .
1. List data rendering
data
an objectlet todos = [
// First write two fake data to test it
{ id : 1 , content : "Ababa" , completed : true } ,
{ id : 2 , content : "Macamaca" , completed : false }
]
new Vue ( {
...
data ( ) {
return {
all : all
}
} ,
} )
.main
And .footer
hide: v-if
conditions rendering<section class="main" v-if="todos.length">...</section>
<section class="footer" v-if="todos.length">...</section>
Thoughts : Why is used here v-if
instead of v-show
it? What is their difference?
In common : their functions are conditions rendered .
Different points : v-show
the principle is to modify css
the display attributes, and no operating dom
elements.
v-if
The principle is based on conditions, dynamically add or destroy dom
elements. But v-if
also inert
If the initial conditions false
, do nothing, wait until the conditions for true
the re-start rendering.
Therefore , v-if
there is a higher switching overhead, but v-show
there is a higher initial rendering overhead. If you need to switch frequently, it is recommended v-show
. If you don’t need to switch frequently, you can use it v-if
. And here .main
and .footer
does not frequently switch state, use v-if
.
When there is data
Dynamic rendering data list: v-for
list rendering
Bind the class in the corresponding state: :class
class binding
Checkbox selected state switch: v-model
two-way data binding
Label content rendering: Mustache
syntax
<ul class="todo-list">
<li v-for="(item,index) in todos"
:key="item.id"
:class="{completed:item.completed}">
<div class="view">
<input class="toggle"
type="checkbox"
v-model="item.completed">
<label>{{ item.content }}</label>
<button class="destroy"></button>
</div>
<input class="edit" value="Create a TodoMVC template">
</li>
</ul>
v-for
have to use :key
?v-for
when rendering the list of elements, will use a place multiplexing strategy, try as much as possible in-place modification / reuse the same type elements. And :key
it gives each node a unique identifier , so that the algorithm correctly identified Diff virtual nodes in the DOM, so reuse and reorder existing elements, and thus more efficiently update the virtual DOM .v-model
The principle?v-model
Two-way binding for form data is essentially syntactic sugar.v-bind
Bind a value attribute v-on
to bind the input event to the current element.<input v-model="something"></input>
The above operation is equivalent to
< input :value =" something " @input =" something = $event.target.value " > </ input >
First bind a something attribute, and pass it through the event by monitoring the input event when the user changes the input box data The target in the incoming event object finds the event source, and value represents the value of the event source, thereby achieving the effect of two-way data binding.
2. Add a new todo
Press Enter, the input is not empty, add one todo
:
@keyup.enter
Enter the keyboard and monitor events of vue methods
add the appropriate method
Creating newTodo
a data object, data acquisition, data model and todos
the same
With push
the newTodo
addition of todo
the
Do nothing if the content is empty
After adding, the content of the input box is cleared
<input class="new-todo"
placeholder="What needs to be done?"
autofocus
@keyup.enter="addTodo">
</input>
methods: {
addTodo ( $event ) {
// Create newTodo object and get data
const newTodo = {
id : this . todos . length + 1 ,
content : $event . target . value . trim ( ) ,
completed : false
}
// If the content is empty, do nothing
IF ( ! newTodo . content . length ) return
// If the content is not empty, add newTodo to todos
this . Todos . Push ( newTodo )
console . Log ( todos )
// Clear the content of the input box
$event . Target . Value = ''
}
}
3. Delete todo
Click .destroy
to delete where todo
:
@click
Monitoring button click event and the vue methods
add the appropriate method
An array of splice
methods to removetodo
<button class="destroy" @click="destroyTodo(index)"></button>
methods: {
...
destroyTodo ( index ) {
// Use the splice method to find the todo to be deleted through the parameter index, and delete an item of
this.todos.splice(index, 1)
}
}
4. Edit todo
Double click label
to enter edit mode
@dblclick
Monitor label
double-click event
:class
Where to li
bind the class.editing
This sets an intermediate variable currentEditing
(like the one state), when listening to label
you double-click an event, currentEditing = item
and when item === currentEditing
, the where give li
binding class
<li v-for="(item,index) in todos" :key="item.id"
:class="{ completed: item.completed , editing: item === currentEditing }">
</li>
<label @dblclick="currentEditing = item">{{ item.content }}</label>
directives
allows automatic input box acquired focus<input class="edit"
:value="item.content"
v-editing-focus="item === currentEditing">
</input>
directives: {
// update is called when the VNode (virtual node) of all components is updated, but it may happen before its child VNode is updated.
update ( el , binding ) {
// el: used to manipulate the element DOM
// binding.value: the binding value of the instruction here is item === currentEditing
if ( binding . value ) {
el . focus ( )
}
}
}
todo
content to replace the content of the input@keyup.enter
Monitor keyboard enter event; @blur
monitor defocus event<input class="edit"
:value="item.content"
v-editing-focus="item === currentEditing"
@keyup.enter="saveEditing(item,index,$event)" @blur="saveEditing(item,index,$event)">
methods
add the appropriate methodsaveEditing (item, index, $event) {
// Save the entered content to the newContent variable
const newContent = $event . Target . Value . Trim ( )
// delete todo
if the content is empty if ( ! NewContent ) this . DestroyTodo ( index )
// change the original content Replace with the entered content
item . content = newContent
//By setting currentEditing, remove .editing and exit the editing mode.
this . currentEditing = null
}
:value
To input
bind content<input class="toggle"
type="checkbox"
v-model="item.completed"
:value="item.content">
@keyup.esc
Enter the keyboard and monitor events of vue methods
add the appropriate method<input class="edit"
:value="item.content"
v-editing-focus="item === currentEditing"
@keyup.enter="saveEditing(item,index,$event)" @blur="saveEditing(item,index,$event)"
@keyup.esc="quitEditing">
quitEditing () {
// removed off by setting currentEditing .editing exit the edit mode
the this . CurrentEditing = null
}
5. State switching
6. Count
7. Clear all completed items
8. Three state data filtering
9. Data Persistence
Author: Nicklaus6
Source Code: https://github.com/Nicklaus6/todomvc-vue
#vuejs #vue #javascript