<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          vue/react組件庫中??5個"作者不造的輪子"

          共 2959字,需瀏覽 6分鐘

           ·

          2020-08-31 09:18


          來源: 鐵皮飯盒https://juejin.im/post/5d89cd156fb9a06acb3ee19e

          ? 這五個輪子其實是5個純js實現(xiàn)的插件, 都非常優(yōu)秀, 下面一一給大家揭秘.

          async-validator(數(shù)據(jù)驗證工具)

          默認(rèn)集成了urlemail驗證, 支持異步驗證. element-ui和iview的表單組件都是用他實現(xiàn)的驗證功能.

          1. import schema from 'async-validator';

          2. // 監(jiān)視對象'name'字段的值是否等于muji, 且必須存在

          3. var descriptor = {

          4. name: {

          5. type: "string",

          6. required: true,

          7. validator: (rule, value) => value === 'muji',

          8. }

          9. };

          10. var validator = new schema(descriptor);

          11. validator.validate({name: "muji"}, (errors, fields) => {

          12. if(errors) {

          13. return handleErrors(errors, fields);

          14. }

          15. });

          https://github.com/yiminghe/async-validator

          補充: 看了作者的github, 作者應(yīng)該是阿里的員工, 而且也是ant design的代碼維護者.

          moment | day.js(操作時間)

          ant design在DatePicker組件中用了moment.

          moment由于歷史兼容原因體積比較大, 現(xiàn)在建議大家用day.js代替他, 兩者語法相似.

          1. dayjs('2018-08-08') // 解析字符串


          2. dayjs().format('{YYYY} MM-DDTHH:mm:ss SSS [Z] A') // 格式化日期


          3. dayjs().add(1, 'year') // 當(dāng)前年份增加一年


          4. dayjs().isBefore(dayjs()) // 比較

          popover(氣泡對話框)

          element-ui和iview的tooltip和popover組件都是基于vue-popover實現(xiàn)的, 而vue-popover只是對popper做了一層vue的封裝, 所以氣泡對話框的核心是popper.

          1. class="my-button">按鈕

        2. class="my-popper">氣泡菜單

          1. var reference = document.querySelector('.my-button');

          2. var popper = document.querySelector('.my-popper');

          3. var popperInstance = new Popper(reference, popper, {

          4. // 更多設(shè)置

          5. });

          autosize(讓textarea隨著文字換行而變化高度)

          vux的textarea用autosize讓textarea組件隨著輸入文字換行而變化高度.

          1. // 就一行, 就實現(xiàn)了"textarea隨著輸入文字換行而變化高度"

          2. autosize(document.querySelector('textarea'));

          resize-observer-polyfill(Resize Observer API的兼容補丁)

          基本所有的ui組件庫都在用, 讓低版本瀏覽器也支持Resize Observer API, 這樣我們可以放心的監(jiān)視元素的尺寸變化.

          1. import ResizeObserver from 'resize-observer-polyfill';

          2. const ro = new ResizeObserver((entries, observer) => {

          3. for (const entry of entries) {

          4. const {left, top, width, height} = entry.contentRect;

          5. console.log('Element:', entry.target);

          6. console.log(`Element's size: ${ width }px x ${ height }px`);

          7. console.log(`Element's paddings: ${ top }px ; ${ left }px`);

          8. }

          9. });

          10. ro.observe(document.body);

          最后

          學(xué)習(xí)了很多組件庫的源碼, 基于對寫代碼的熱情, 我用ts寫了2個小插件, 抽象了一些組件中重復(fù)的代碼, 大家看下是否需要.

          any-touch

          ?一個手勢庫, 支持tap(點擊) / press(按) / pan(拖拽) / swipe(劃) / pinch(捏合) / rotate(旋轉(zhuǎn)) 6大類手勢, 同時支持鼠標(biāo)和觸屏.

          在線演示

          1. import AnyTouch from "any-touch";

          2. const el = doucument.getElementById("box");

          3. const at = new AnyTouch(el);


          4. at.on("pan", ev => {

          5. // 拖拽觸發(fā).

          6. });

          tap(點擊)

          用來解決移動端"click的300ms延遲問題", 同時通過設(shè)置支持"雙擊"事件.

          press(按)

          用來觸發(fā)自定義菜單.

          pan(拖拽)

          這應(yīng)該是組件庫中最常用的手勢, carousel(輪播) / drawer(抽屜) / scroll(滑動) / tabs(標(biāo)簽頁)等都需要"拖拽識別"

          swipe(滑)

          carousel/tabs的切換下一幅, scroll的快速滑動等.

          pinch(捏合) / rotate(旋轉(zhuǎn))

          pinch用來縮放商品圖片, rotate一般用在高級定制化功能呢, 比如對圖片(商品)刻字后旋轉(zhuǎn)文字.

          ? 更多說明: https://github.com/any86/any-touch

          vue-create-root

          ? 不到1kb的小工具, 把vue組件變成this.$xxx這樣的命令.

          1. // main.js

          2. Vue.use(createRoot);


          3. // xxx.vue

          4. import UCom from '../UCom.vue';

          5. {

          6. mounted(){

          7. // 默認(rèn)組件被插入到尾部

          8. this.$createRoot(UCom, {props: {value:'hello vue!'}});

          9. // 或者簡寫為:

          10. this.$createRoot(UCom, {value:'hello vue!'});

          11. }

          12. }

          ? 更多說明: https://github.com/any86/vue-create-root

          掃碼關(guān)注公眾號,訂閱更多精彩內(nèi)容。


          給個[在看],是對達(dá)達(dá)最大的支持!
          瀏覽 81
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  免费看日皮视频 | 大香蕉伊人网综合 | 国产成人久久精品激情 | 欧美在线黄色 | 高潮视频在线观看免费 |