發表文章

目前顯示的是有「Vue」標籤的文章

前端優化效能-1(lazy-img)

圖片
在Chrome底下可以使用在img DOM加入 loading="lazy" 就可以直接使用懶加載了,或者裝線上有的plugin也可以的(但是我喜歡自己來) <img src="image.png" loading="lazy" alt="..." /> 但IE跟Safari是不支援這樣的語法的,所以只好自己幹一個了,我們來用 IntersectionObserver吧(結果IE 也沒支援attribute QAQ) //註冊在讀取圖檔時要做的事情 window.addEventListener('load', () => {      const watcher = new IntersectionObserver(onEnterView) const elementArrary = document.getElementsByClassName('your_imgName'); for (let image of elementArrary) { watcher.observe(image) // 開始監視 } } //IntersectionObserver 的call back function onEnterView(entries: IntersectionObserverEntry[], observer: IntersectionObserver) { for (let entry of entries) { if (entry.isIntersecting) { // Element show on the loadImage(entry.target) // cancel watch observer.unobserve(entry.target) } } } // load img logic function loadImage(img: HTMLImageElement) { const removeMockup = (

Vue那些我踩過的坑(Vuex-TypeScript)

圖片
在var來var去的同時雖然很方便,但也留下了許多技術債,在專案變大的同時未來是需要還的,但是在Vuex裡面使用TypeScript是非常痛苦的,因為VUEX本身的寫法是不支援TypeScript,這邊我示範一下如何重新定義store能夠好好低與typeScript結合吧 Store結構: type,是定義本身型別放置的地方 store │ index.ts │ type.ts │ └───rootComic actions.ts getters.ts index.ts mutation.ts type.ts index.d.ts: 我們可以從index底層Store的code,看到有個模板可以轉換成你想要的物件,裡面的 <S> 就是你定義的型別 export interface StoreOptions<S> { state?: S | (() => S); getters?: GetterTree<S, S>; actions?: ActionTree<S, S>; mutations?: MutationTree<S>; modules?: ModuleTree<S>; plugins?: Plugin<S>[]; strict?: boolean; devtools?: boolean; } Store定義: Step1: 開一個type.ts的檔案宣告我們 <S> 的型別,State本是個放資料的地方,要放資料的地方一定少不了interface,我們就以interface的方式來定義我們的state吧 export interface RootState { "rootmessage": string } Step2: 原本的寫法 import { createStore } from 'vuex' export default createStore({ state: { }, mutations: { }, actions: { }, modules: { } });

Vue那些我踩過的坑(eslint)

圖片
  程式碼有太多風格,程式碼越來越亂嗎?快來使用eslint提升你程式碼的品質 安裝: npm install eslint -g 要點: 1.找出語法錯誤 沒宣告變數就拿來用、少了括號等等常見的語法錯誤 2.讓你使用最佳實踐 不使用全域變數、建議使用 === 而非 ==、不使用 eval … 3. 提醒你刪掉多餘的程式碼 有些變數宣告了卻沒有使用、 import  了沒有使用的模組、空的 class constructor … 4. 統一基本的 coding style 要不要加分號、使用單引號或雙引號、縮排使用 space 或 tab 等等 與git整合: 我們有npm run lint 這個 script,但一定有人忘了跑直接就上code 了,可以配合 pre-commit 先跑  npm install --save-dev pre-commit  安裝他,安裝完之後在  package.json  加上  "pre-commit": ["lint"]  就完成了,整個檔案會長這樣 "name": "...", "scripts": { "lint": "eslint src/*.js" }, "pre-commit": ["lint"], "dependencies": { ... } "devDependenceies": { "pre-commit": "^1.2.2", ... } 那以後當任何人在專案下跑  git commit -m "message …"  就會先觸發 ESLint 檢查程式碼,通過之後才會成功 commit 結語: 在開發的時候常會有人用不嚴謹的方式宣告變數以及寫法,用了eslint可以大大的減少這方面的困擾