vue實(shí)現(xiàn)商品列表的添加刪除實(shí)例講解
我們首先來看下代碼:
<div id="app">
<div class="container"><form class="form-inline">
<div class="form-group"><label for="exampleInputName2">ID:</label> <input id="exampleInputName2" class="form-control" type="text" /></div>
<div class="form-group"><label for="exampleInputEmail2">Name:</label> <input class="form-control" type="text" /></div>
<button class="btn btn-primary" type="button">提交</button></form>
<table class="table table-hover table-striped">
<tbody>
<tr>
<td>ID</td>
<td>品牌名稱</td>
<td>添加時(shí)間</td>
<td>操作</td>
</tr>
<tr>
<td>{{item.id}}</td>
<td>{{item.pp_name}}</td>
<td>{{item.add_time | getTime()}}</td>
<td><a>刪除</a></td>
</tr>
</tbody>
</table>
</div>
</div>
<script type="text/javascript">// <![CDATA[
var app = new Vue({
el: '#app',
data: {
id : '',
name : '',
list : [
{id : 1, pp_name : '安踏', add_time : new Date()},
{id : 2, pp_name : '李寧', add_time : new Date()},
{id : 3, pp_name : '捷豹', add_time : new Date()},
{id : 4, pp_name : '悍馬', add_time : new Date()}
]
},
methods: {
add : function(){
// 數(shù)據(jù)插入數(shù)組操作
this.list.push({id : this.id, pp_name : this.name, add_time : new Date()});
this.id = this.name = ''
},
/*
根據(jù)id刪除數(shù)據(jù)
分析: 先要找到這一項(xiàng)數(shù)據(jù)的id,然后根據(jù)id刪除索引
找到索引之后直接調(diào)用數(shù)組的splice方法
*/
del : function(id){
this.list.some((item,i) =>{
if(item.id === id){
this.list.splice(i,1);
// 在數(shù)組some中 如果返回值為true,則會(huì)立即終止后續(xù)的循環(huán)
return true;
}
})
}
},
// 時(shí)間的過濾
filters:{
getTime:function(value){
let date = new Date(value),
Y = date.getFullYear(),
m = date.getMonth() + 1,
d = date.getDate(),
h = date.getHours(),
min = date.getMinutes(),
s = date.getSeconds();
if(m<10){m = '0' +m;}
if(d<10){d = '0' +d;}
if(h<10){h = '0' +h;}
if(min<10){min = '0' +min;}
if(s<10){s = '0' +s;}
let t = Y + '-' + m + '-' + d + ' | ' + h + ':' + min + ':' + s;
return t;
}
}
})
// ]]></script>
內(nèi)容補(bǔ)充:
vue中注冊(cè)組件,實(shí)現(xiàn)列表的添加刪除效果
1、首先在html的<code><head>標(biāo)簽中</code>導(dǎo)入vue.js
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
2、在body中創(chuàng)建一個(gè)應(yīng)用vue模板的容器
// vue起作用的區(qū)域root <div id="root"> // input與mesg數(shù)據(jù)綁定 <input v-model="mesg" /> <button @click="handle">提交</button> <ul> <todo-item v-for='(item,index) in list' :key='index' :index='index' :content='item' @delete='deletes'></todo-item> </ul> </div>
3、在script標(biāo)簽中創(chuàng)建并注冊(cè)名為todo-item的組件
Vue.component('todo-item', {
props: ['content', 'index'],
template: '<li @click="handelClick">{{content}}</li>',
methods: {
handelClick: function() {
//點(diǎn)擊li元素就觸發(fā)delete方法
this.$emit('delete', this.index);
}
}
})
4、在script標(biāo)簽中初始化vue實(shí)例
new Vue({
el: '#root',
data() {
return {
list: [],
mesg: ''
}
},
methods: {
//點(diǎn)擊提交按鈕,輸入文本信息就加入列表
handle: function() {
if(this.mesg==''){
return;
}
this.list.push(this.mesg);
this.mesg = ''
},
deletes: function(index) {
alert(index)
this.list.splice(index, 1);
}
}
})
到此這篇關(guān)于vue實(shí)現(xiàn)商品列表的添加刪除實(shí)例講解的文章就介紹到這了,更多相關(guān)vue商品列表添加刪除內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue設(shè)計(jì)一個(gè)倒計(jì)時(shí)秒殺的組件詳解
這篇文章主要介紹了vue設(shè)計(jì)一個(gè)倒計(jì)時(shí)秒殺的組件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue導(dǎo)出少量pdf文件實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了vue導(dǎo)出少量pdf文件實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
vue swipe自定義組件實(shí)現(xiàn)輪播效果
這篇文章主要為大家詳細(xì)介紹了vue swipe自定義組件實(shí)現(xiàn)輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Vue+webpack+Element 兼容問題總結(jié)(小結(jié))
這篇文章主要介紹了Vue+webpack+Element 兼容問題總結(jié)(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
基于vue和react的spa進(jìn)行按需加載的實(shí)現(xiàn)方法
這篇文章主要介紹了基于vue和react的spa進(jìn)行按需加載,需要的朋友可以參考下2018-09-09
vue 動(dòng)態(tài)組件用法示例小結(jié)
這篇文章主要介紹了vue 動(dòng)態(tài)組件用法,結(jié)合實(shí)例形式總結(jié)分析了vue 動(dòng)態(tài)組件基本功能、原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
Vue兩個(gè)版本的區(qū)別和使用方法(更深層次了解)
在我們使用 vue時(shí),我們可以引用兩個(gè)不同版本的 Vue,分別是 Vue完整版(vue.js)和 Vue(vue.runtime.js )非完整版,那么它們的區(qū)別是什么呢,今天我們就來分析下這兩個(gè)不同版本之間的區(qū)別,一起看看吧2020-02-02
關(guān)于this.$refs獲取不到dom的可能原因及解決方法
這篇文章主要介紹了關(guān)于this.$refs獲取不到dom的可能原因及解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
electron-vite工具打包后如何通過內(nèi)置配置文件動(dòng)態(tài)修改接口地址
使用electron-vite?工具開發(fā)項(xiàng)目打包完后每次要改接口地址都要重新打包,對(duì)于多環(huán)境切換或者頻繁變更接口地址就顯得麻煩,這篇文章主要介紹了electron-vite工具打包后通過內(nèi)置配置文件動(dòng)態(tài)修改接口地址實(shí)現(xiàn)方法,需要的朋友可以參考下2024-05-05

