使用Vue做一個(gè)簡單的todo應(yīng)用的三種方式的示例代碼
1. 引用vue.js
<!DOCTYPE html>
<html>
<head>
<script src="http://vuejs.org/js/vue.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="root">
<input type="text" v-model="inputValue">
<button @click="handlerAdd">提交</button>
<ul>
<li
v-for="(item,index) of lists"
:key="index"
@click="handlerDel(index)"
>
{{item}}
</li>
</ul>
</div>
<script>
new Vue({
el: '#root',
data: {
inputValue: '',
lists: []
},
methods: {
handlerAdd: function() {
this.lists.push(this.inputValue);
this.inputValue = '';
},
handlerDel: function(index) {
this.lists.splice(index, 1);
}
}
});
</script>
</body>
</html>
2. 全局組件注冊
<!DOCTYPE html>
<html>
<head>
<script src="http://vuejs.org/js/vue.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="root">
<input type="text" v-model="inputValue">
<button @click="handlerAdd">提交</button>
<ul>
<todo-item
v-for="(item,index) of lists"
:content = "item"
:index = "index"
:key = "index"
@delete="handlerDel"
>
</todo-item>
</ul>
</div>
<script>
Vue.component('todoItem', {
props: {
content: String,
index: Number
},
template: '<li @click="handlerClick">{{content}}</li>',
methods: {
handlerClick: function(){
this.$emit('delete', this.index);
}
}
});
new Vue({
el: '#root',
data: {
inputValue: '' ,
lists: []
},
methods: {
handlerAdd: function() {
this.lists.push(this.inputValue);
this.inputValue = '';
},
handlerDel: function(index) {
this.lists.splice(index,1);
}
}
});
</script>
</body>
</html>
3. vue-cli腳手架
// Todo.Vue
<template>
<div>
<input type="text" v-model="inputValue">
<button @click="handlerAdd">提交</button>
<ul>
<todo-item
v-for="(item,index) of lists"
:key="index"
:content="item"
:index="index"
@delete="handlerDel"
></todo-item>
</ul>
</div>
</template>
<script>
import TodoItem from './components/todoItem'
export default {
data () {
return {
inputValue: '',
lists: []
}
},
methods: {
handlerAdd () {
this.lists.push(this.inputValue)
this.inputValue = ''
},
handlerDel (index) {
this.lists.splice(index, 1)
}
},
components: {
'todo-item': TodoItem
}
}
</script>
<style>
</style>
// TodoItem.vue
<template>
<li @click="handlerClick" class="item">{{content}}</li>
</template>
<script>
export default {
props: ['content', 'index'],
methods: {
handlerClick () {
this.$emit('delete', this.index)
}
}
}
</script>
<style scoped>
ul,li {
list-style: none;
}
.item {
color: blueviolet;
}
</style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue源碼學(xué)習(xí)之Object.defineProperty 對數(shù)組監(jiān)聽
這篇文章主要介紹了vue源碼學(xué)習(xí)之Object.defineProperty 對數(shù)組監(jiān)聽,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Avue實(shí)現(xiàn)動(dòng)態(tài)查詢與數(shù)據(jù)展示的示例代碼
Avue是一個(gè)基于Vue.js的前端框架,它是由阿里云開發(fā)的一款企業(yè)級UI組件庫,旨在提供一套全面、易用且高性能的界面解決方案本文介紹了Avue實(shí)現(xiàn)動(dòng)態(tài)查詢與數(shù)據(jù)展示的示例,需要的朋友可以參考下2024-08-08
解決vue2使用腳手架配置prettier報(bào)錯(cuò)prettier/prettier:context.getPhysical
這篇文章主要介紹了解決vue2使用腳手架配置prettier報(bào)錯(cuò)prettier/prettier:context.getPhysicalFilename is not a function問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
使用Vue實(shí)現(xiàn)簡單的信號和電池電量組件
這篇文章主要為大家詳細(xì)介紹了如何使用Vue實(shí)現(xiàn)簡單的信號和電池電量組件效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
使用Vue?Query實(shí)現(xiàn)高級數(shù)據(jù)獲取的示例詳解
構(gòu)建現(xiàn)代大規(guī)模應(yīng)用程序最具挑戰(zhàn)性的方面之一是數(shù)據(jù)獲取,這也是?Vue?Query?庫的用途所在,下面就跟隨小編一起學(xué)習(xí)一下如何利用Vue?Query實(shí)現(xiàn)高級數(shù)據(jù)獲取吧2023-08-08
Vue聲明式導(dǎo)航與編程式導(dǎo)航示例分析講解
這篇文章主要介紹了Vue中聲明式導(dǎo)航與編程式導(dǎo)航,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11
基于element日歷組件實(shí)現(xiàn)簽卡記錄
這篇文章主要為大家詳細(xì)介紹了基于element日歷組件實(shí)現(xiàn)簽卡記錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
vue+elementui實(shí)現(xiàn)動(dòng)態(tài)控制表格列的顯示和隱藏
這篇文章主要介紹了vue+elementui實(shí)現(xiàn)動(dòng)態(tài)控制表格列的顯示和隱藏,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

