使用Vue做一個(gè)簡(jiǎn)單的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. 全局組件注冊(cè)
<!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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue輕量級(jí)框架無(wú)法獲取到vue對(duì)象解決方法
這篇文章主要介紹了vue輕量級(jí)框架無(wú)法獲取到vue對(duì)象解決方法相關(guān)知識(shí)點(diǎn),有需要的讀者們跟著學(xué)習(xí)下。2019-05-05
一款移動(dòng)優(yōu)先的Solid.js路由solid router stack使用詳解
這篇文章主要為大家介紹了一款移動(dòng)優(yōu)先的Solid.js路由solid router stack使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Vscode關(guān)閉Eslint語(yǔ)法檢查的多種方式(保證有效)
eslint是一個(gè)JavaScript的校驗(yàn)插件,通常用來(lái)校驗(yàn)語(yǔ)法或代碼的書(shū)寫(xiě)風(fēng)格,下面這篇文章主要給大家介紹了關(guān)于Vscode關(guān)閉Eslint語(yǔ)法檢查的多種方式,文章通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
vue.js中使用微信掃一掃解決invalid signature問(wèn)題(完美解決)
這篇文章主要介紹了vue.js中使用微信掃一掃解決invalid signature問(wèn)題(推薦),本文通過(guò)實(shí)例代碼給出解決方法,代碼簡(jiǎn)單易懂非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
使用vue-cli+webpack搭建vue開(kāi)發(fā)環(huán)境的方法
這篇文章主要介紹了使用vue-cli+webpack搭建vue開(kāi)發(fā)環(huán)境的方法,需要的朋友可以參考下2017-12-12
vue.js或js實(shí)現(xiàn)中文A-Z排序的方法
下面小編就為大家分享一篇vue.js或js實(shí)現(xiàn)中文A-Z排序的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
vue3動(dòng)態(tài)路由刷新后空白或者404問(wèn)題的解決
在vue項(xiàng)目中采用動(dòng)態(tài)添加路由的方式,第一次進(jìn)入頁(yè)面會(huì)正常顯示,但是點(diǎn)擊刷新頁(yè)面后會(huì)導(dǎo)致頁(yè)面空白,所以下面這篇文章主要給大家介紹了關(guān)于vue3動(dòng)態(tài)路由刷新后空白或者404問(wèn)題的解決方法,需要的朋友可以參考下2022-07-07

