Vue.js
Posted by Bruce Tsai
09/08/2016
Vue.js 是以 ViewModel 為基礎發展的 JavaScript library。與其他前端框架不同的是,Vue.js 的目標是用簡單的 API 來實作資料繫結與操作網頁上的組件, 同時也因為 Vue.js 只把焦點關注在 View 層級上,遂能夠輕易地與其他 JS library 整合使用。
範例
<!-- component template -->
<script type="text/x-template" id="grid-template"></script>
<!-- demo root element -->
<div id="demo">
<form id="search">
Search <input name="query" v-model="searchQuery">
</form>
<demo-grid
:data="gridData"
:columns="gridColumns"
:filter-key="searchQuery">
</demo-grid>
</div>
// register the grid component
Vue.component('demo-grid', {
template: '#grid-template',
props: {
data: Array,
columns: Array,
filterKey: String
},
data: function () {
var sortOrders = {}
this.columns.forEach(function (key) {
sortOrders[key] = 1
})
return {
sortKey: '',
sortOrders: sortOrders
}
},
methods: {
sortBy: function (key) {
this.sortKey = key
this.sortOrders[key] = this.sortOrders[key] * -1
}
}
})
// bootstrap the demo
var demo = new Vue({
el: '#demo',
data: {
searchQuery: '',
gridColumns: ['name', 'power'],
gridData: [
{ name: 'Chuck Norris', power: Infinity },
{ name: 'Bruce Lee', power: 9000 },
{ name: 'Jackie Chan', power: 7000 },
{ name: 'Jet Li', power: 8000 }
]
}
})