1.新一个vue对象的时候你可以设置它的属性,其中最重要的包括三个,分别是data,methods,watch。
2.其中数据代表VUE对象的数据,方法代表VUE对象的方法,看设置了对象监听的方法。
3.Vue对象里的设置通过HTML指令进行关联。
4.重要的指令包括:
- V-文本渲染数据
- V-如果控制显示
- V-上绑定事件
- V型的循环渲染等
一个简单的任务清单(可以标记是否完成任务)
源代码:
App.vue
<template>
<div id="app">
<h1 v-html="title"></h1>
<input v-model="newItem" v-on:keyup.enter="addNew">
<ul>
<li v-for="item in items" v-bind:class="{finished: item.isFinished}" v-on:click="toggleFinish(item)">
{{item.label}}
</li>
</ul>
</div>
</template>
<script>
import Store from './store'
export default {
data: function() {
return {
title: 'this is a todo list',
items: Store.fetch(),
newItem: ''
}
},
watch: {
items: {
handler: function(items) {
Store.save(items)
},
deep: true
}
},
methods: {
toggleFinish: function(item) {
item.isFinished = !item.isFinished
},
addNew: function() {
this.items.push({
label: this.newItem,
isFinished: false
})
this.newItem = ''
}
}
}
</script>
<style>
.finished {
text-decoration: underline;
}
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
Store.js
const STORAGE_KEY = 'todos-vuejs'
export default {
fetch: function () {
return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]')
},
save: function (items) {
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(items))
}
}