Vue無疑是近來最火的一個(gè)前端框架喇辽,它吸取了angular1.x和react的精華蜘犁,性能優(yōu)良扔字,而且易于上手,本文主要是關(guān)于如何去寫一個(gè)組件格嘁。
首先是項(xiàng)目目錄
path.png
編寫組件
在這里我寫了一個(gè)日期控件(移動(dòng)端的)笛求,在components目錄下建立文件夾,并建立vue文件和index.js文件注冊(cè)組件。
.vue文件中的內(nèi)容我就不多說了探入,去看我的項(xiàng)目即可狡孔。
組件注冊(cè)
import datepicker from './datepicker.vue'
//這里第一個(gè)參數(shù)可以設(shè)置別的名字,作為頁面標(biāo)簽使用
datepicker.install = (Vue) => Vue.component('datepicker',datepicker)
export default datepicker
組件使用
import datepicker from './components/datepicker'
import Vue from 'vue'
Vue.use(datepicker);
//html中
<datepicker @cancle="cancle" @sure="sure" :selecteddate="olddate"></datepicker>
最后附上代碼地址https://github.com/Stevenzwzhai/vue-datepicker
演示地址 https://stevenzwzhai.github.io/vue-datepicker/新症。打開mobile調(diào)試模式步氏,因?yàn)槭褂昧藅ouch事件,所以pc無效徒爹。