發表文章

Make a react-window (1)

圖片
  Introduction if your DOM is to much that slow dow your render performance, most of case will happend on the table element, we had  react-window  could help us resolve this issue react-window FixedList  we have to give it  parameter: height  : scroll list height itemcount  : row arrary length itemSize : row height width : scroll list width actually it already resolve to much DOM problem , but I want dig more deep tech that I could understand how it work and I could did more flexible design DOM no need rely the plugin Let see the detail: how parameter work: firsrt div: height  is for first  div  make ****max-height and we also give it the overflow properties second   div:  height ****is  itemcount * itemSize  that we could render the scroll row div:  we are use  absoulte  and give it  index * itemSize = top Height when we are scrolling that you can find it will change : row top height replace the DOM tree the row DOM won’t be increase or decrease , it’s seem

Vue(Vue3取代 Vuex?)

圖片
  哎呀,以前在資料管理的部分都被Vuex綁死死,但是在typescript相對來說是很不友善的(我有寫一篇 for vuex在typescript 去定義),自從我用了vue3 的composition API及reactive 就直接使用了 yarn remove vuex ,在typescript的世界裡就是要清清楚楚,簡簡單單,甚麼模板轉換的才不需要呢 我們來創自己管資料的地方吧 type.ts: export interface InterComicsData extends InterGenre { id: string; mainIndex: string; mainIndex_name: string; descr: string; link: string; title: string; thumbnail_name: string; thumbnail_url: string; lastUpdateTime: string; createTime: string; genreDatas: InterGenre[]; } export interface InterGenre { id: string; rootComics_id: string; name: string; } export interface InterComics { comicDatas: InterComicsData[]; genreDatas: InterGenre[]; pageLimit: Number; } comics.ts: 我有做一個漫畫站,這邊是用來管理comics的地方 import Vue, { reactive, defineComponent, readonly } from "vue"; import { InterComicsData, InterComics, InterGenre } from "@/store/comics/type"; import axios from "axios"; import * as data

前端優化效能-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可以大大的減少這方面的困擾

爬蟲7-棄坑Scrapy框架(request)

圖片
  在開發到一半的爬蟲突然覺得Scrapy的彈性有點低,所以就立馬轉彎了 Scrapy缺點: 1.在yield資料的時候,只能擇一pipline來做資料的存取,假如我要ftp跟sql跟json只能擇一來做資料的存取 2.本身的功能還有些bug 是你無法去修復的 3.開發的彈性太差,不好擴充功能,必須照著官方檔案來實作 MySpider: 最後決定用最原始的方式來爬蟲,而這是我專案的結構 C:. └───mySpider-Sample │ .gitattributes │ .gitignore │ .gitignore.swp │ chromedriver.exe │ errorRecoder.py │ ftp.py │ main.py │ myfilter.py │ myProxy.py │ mysql.py │ README.md │ requirements.txt │ spider.py │ spider_playwright.py │ └───chromedriver chromedriver.exe 主項: a.spider: 1.主要是request搭配beautifulsoup 2.有存json以及圖片的功能 3.有限制防爬蟲的網站,目前沒有好的解決辦法 b.spider_playwright: 1.以playwright為主體 2.有存json以及圖片的功能 3.模擬器叫起瀏覽器方式 c.mysql: 1.存取sql的方法可以寫在這裡 2.我有寫一個getData的功能,(可以自行擴充喔) 3.擴充sql方法都可以寫在這裡 d.main: 程式的起始點 e.myproxy: 1.爬取免費的proxy  2.經過muyiple threads 驗證過拿的proxy都是活的,品質的話則不一定 f.myfilter: 1.透過跟sql要資料去排除重複性的資料 # here is get sql data to check it's existed it will remove duplicat