解釋在上面的示例中,我們創建了一個貨幣轉換器,它將一種貨幣值轉換爲另一種貨幣的選定值。我們已經創建了兩個貨幣下拉列表。當我們在文本框中輸入要轉換的金額時,轉換後的金額將顯示在下面。我們正在使用computed屬性進行必要的貨幣轉換計算。
Example 2: Customer Details
<html> <head> <title>VueJs Instance</title> <script type = "text/javascript" src = "js/vue.js"></script> </head> <body> <style> #databinding{ padding: 20px 15px 15px 15px; margin: 0 0 25px 0; width: auto; } span, option, input { font-size:20px; } .Table{ display: table; width:80%; } .Title{ display: table-caption; text-align: center; font-weight: bold; font-size: larger; } .Heading{ display: table-row; font-weight: bold; text-align: center; } .Row{ display: table-row; } .Cell{ display: table-cell; border: solid; border-width: thin; padding-left: 5px; padding-right: 5px; width:30%; } </style> <div id = "databinding" style = ""> <h1>Customer Details</h1> <span>First Name</span> <input type = "text" placeholder = "Enter First Name" v-model = "fname"/> <span>Last Name</span> <input type = "text" placeholder = "Enter Last Name" v-model = "lname"/> <span>Address</span> <input type = "text" placeholder = "Enter Address" v-model = "addr"/> <button v-on:click = "showdata" v-bind:style = "styleobj">Add</button> <br/> <br/> <customercomponent v-for = "(item, index) in custdet" v-bind:item = "item" v-bind:index = "index" v-bind:itr = "item" v-bind:key = "item.fname" v-on:removeelement = "custdet.splice(index, 1)"> </customercomponent> </div> <script type = "text/javascript"> Vue.component('customercomponent',{ template : '<div class = "Table"><div class = "Row" v-bind:style = "styleobj"><div class = "Cell"><p>{{itr.fname}}</p></div><div class = "Cell"><p>{{itr.lname}}</p></div><div class = "Cell"><p>{{itr.addr}}</p></div><div class = "Cell"><p><button v-on:click = "$emit(\'removeelement\')">X</button></p></div></div></div>', props: ['itr', 'index'], data: function() { return { styleobj : { backgroundColor:this.getcolor(), fontSize : 20 } } }, methods:{ getcolor : function() { if (this.index % 2) { return "#FFE633"; } else { return "#D4CA87"; } } } }); var vm = new Vue({ el: '#databinding', data: { fname:'', lname:'', addr : '', custdet:[], styleobj: { backgroundColor: '#2196F3!important', cursor: 'pointer', padding: '8px 16px', verticalAlign: 'middle', } }, methods :{ showdata : function() { this.custdet.push({ fname: this.fname, lname: this.lname, addr : this.addr }); this.fname = ""; this.lname = ""; this.addr = ""; } } }); </script> </body> </html>
Output
Output after deletion
解釋在上面的例子中,我們有三個文本框要輸入-名字、姓氏和地址。有一個add按鈕,它使用delete按鈕以表格格式添加在文本框中輸入的值。
表格格式是使用組件創建的。單擊按鈕使用emit事件從數組中刪除elemet與父組件交互。輸入的值存儲在數組中,並使用prop屬性與子組件共享。