官网提供的按需引入方法为全量按需引入,在打包分离中,仍旧存在使用不到的图表被打包进去。
例如:组件A使用了折线图、柱状图,组件B只用到了折线图,但是打包组件B的时候,柱状图也会被打包进去。
本文提供一种动态按需引入的思路,使得只用到折线图的组件B,打包的时候只打包折线图,不会将组件A用到[……]
原理:通过同步更新 useRef 来获取最新值
1 2 3 4 5 6 7 8 9 10 11 |
// util.ts export const useRefState = (init: any = null) => { const [state, setState] = useState(init); const stateRef = useRef(init); const setProxy = (newVal: any) => { stateRef.current = newVal; setState(newVal); }; const getState = () => stateRef.current; return [state, setProxy, getState]; }; |
使用:
1 2 3 4 5 6 7 |
import { useRefState } from "util" const [state, setState, getState] = useRefState(0) state // state 值,变动后更新DOM setState // setState,变动 state getState() // 获取最新值 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// 处理报错 ResizeObserver loop completed with undelivered notifications. export const handlerResizeObserverError = () => { const debounce = (callback: (...args: any[]) => void, delay: number) => { let tid: any; return function (...args: any[]) { const ctx = self; tid && clearTimeout(tid); tid = setTimeout(() => { callback.apply(ctx, args) }, delay) } } const _ = (window as any).ResizeObserver; (window as any).ResizeObserver = class ResizeObserver extends _ { constructor(callback: (...args: any[]) => void) { callback = debounce(callback, 20) super(callback) } } } |
之所以100vh在移动端出现问题,原因大致如上图,真搞不懂,为什么总是有反人类的设计出现。
经过多方参考,实测有效的方案如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<style> :root { --vh: 1vh; } </style> <script> !(function (n, e) { function setViewHeight() { var windowVH = e.innerHeight / 100; n.documentElement.style.setProperty('--vh', windowVH + 'px'); } var i = 'orientationchange' in window ? 'orientationchange' : 'resize'; n.addEventListener('DOMContentLoaded', setViewHeight); e.addEventListener(i, setViewHeight); })(document, window) </script> |
使用:
[crayon-673ee5e79bc7c37921746[……]