本文最后更新于 2024-03-27T11:41:45+08:00
https://www.lanqiao.cn/problem-list/28/
3. 迷惑的 this 1 2 3 4 5 handle ( ) { this .inputEl .addEventListener ('input' ,(e )=> { this .handleInput (e) }) }
这个回调的调用者是this
也就是search对象
这样就不行:
1 2 3 4 5 6 7 8 9 handle ( ) { function deal (e ) { this .handleInput (e) } this .inputEl .addEventListener ('input' ,(e )=> { deal (e) }) },
原因:**this
指向调用者**。
deal的调用者是一个箭头函数,而箭头函数的this
是window
。
(箭头函数不会创建自己的 this
上下文,所以 this
会在词法上指向外层的上下文,这里的外层上下文是全局上下文。)
4. 魔法失灵了 1 2 3 4 5 6 7 8 9 let { value }= toRefs ( data)function update (val ) { value.value = val }
5. 燃烧你的卡路里 1 2 3 4 5 6 7 8 9 10 11 12 13 14 const sortItem = (arr, pro, compare ) => { function comp (a,b ){ if (a[pro]>b[pro]) return -1 ; if (a[pro]==b[pro]) return 0 ; if (a[pro]<b[pro]) return 1 ; } arr.sort (comp) console .log (arr); for (let i=0 ;i<arr.length ;i++){ if (arr[i][pro]<=compare) return arr[i] } };
6 司龄统计 考点:Echarts,动态属性名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 const groupByAge = (peoples ) => { const obj = {}; peoples.forEach ((element ) => { let age = element.age ; if (!obj[age]) { obj[age] = []; } obj[age].push (element); }); return obj; };const app = Vue .createApp ({ setup ( ) { const { updateData } = useECharts (); let xAxisData = Vue .ref ([]); let seriesData = Vue .ref ([]); const groupedPeople = Vue .ref ([]); Vue .onMounted (async () => { const data = await (await fetch ("./mock/data.json" )).json (); groupedPeople.value = groupByAge (data); xAxisData.value =Object .keys (groupedPeople.value ) seriesData.value =Object .values (groupedPeople.value ).map ((item )=> item.length ) updateData (xAxisData.value , seriesData.value ); }); return { groupedPeople, }; }, }); app.mount ("#app" );
7 不翼而飞的余额
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 setup ( ) { const depositAmount = Vue .ref () const store = useMoneyStore () function deposit ( ) { store.addDeposit (depositAmount.value ) depositAmount.value =null } return { deposit, store, depositAmount } }
1 2 3 4 5 6 7 8 9 const useMoneyStore = defineStore ('money' , () => { const balance = ref (23 ); const addDeposit =(add )=>{ balance.value +=add } return { balance,addDeposit } })
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 const { createPinia } = Pinia ;const { createRouter,createWebHistory } = VueRouter ; const app = createApp ({}); app.use (createPinia ());const router = createRouter ({ history :createWebHistory (), routes :[ { path :'/' , component : WalletPage }, { path :'/deposit' , component : DepositPage }, ] })
8 个性化推荐 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 const http = require ("http" );const fs = require ("fs" );const path = require ("path" );const qs = require ("querystring" );const data = require ("../data.json" );const server = http.createServer ((req, res ) => { if (req.url === "/" ) { const indexPage = fs.readFileSync (path.join (__dirname, "../index.html" ), { encoding : "utf8" , }); res.writeHead (200 , { "Content-Type" : "text/html" }); res.write (indexPage); res.end (); } else if (req.url .startsWith ("/customized" )) { let body = "" ; req.on ("data" , (chunk ) => { body += chunk; }); req.on ("end" , () => { let { interested = [] } = qs.parse (body); let result = "" ; if (interested.length ===0 ) { result = `<div class="unselect">你还未选择任何感兴趣的标签!</div>` ; } else { if (! Array .isArray (interested)) interested=[interested] const findContent = (target ) => { for (const item of data) { if (item.tag == target) { return item; } } console .log ("findContent" ); }; const list = []; const set = new Set (); for (const interest of interested) { const content = findContent (interest); list.push (content); set.add (content); } for (const content of list){ if (!content.relevance .length ) continue for (const relevance of content.relevance ) { const relevanceContent = findContent (relevance); if (!set.has (relevanceContent)) { list.push (relevanceContent); set.add (relevanceContent); } } } console .log (list); result=list.map ((item )=> `<div class="interest"> <div class="tag">${item.tag} </div> <div>${item.content} </div> </div>` ).join ('' ) } let indexPage = fs.readFileSync (path.join (__dirname, "../customized.html" ), { encoding : "utf8" , }); indexPage=indexPage.replace ('<body></body>' , `<body>` + result+`</body>` ) res.write (indexPage) res.end (); }); } }); server.listen (8080 , () => { console .log ("server is running in port 8080" ); });
9 贪吃蛇 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 nextStep ( ) { for (let i = this .snakeBody .length - 1 ; i >= 1 ; i--) { this .snakeBody [i] = { ...this .snakeBody [i - 1 ] } } switch (this .direction ) { case 'right' : this .snakeBody [0 ].left += this .size break ; case 'down' : this .snakeBody [0 ].top += this .size break ; case 'left' : this .snakeBody [0 ].left -= this .size break ; case 'up' : this .snakeBody [0 ].top -= this .size break ; } this .snakeBody .unshift (newHead); this .snakeBody .pop (); }
10 自定义表单验证器 1 2 3 watch (inputValue, (newValue ) => { emit ('update:modelValue' , newValue) })
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 const is_email = (val ) => { let [before, behind] = val.split ('@' ) if (typeof (behind)!='string' ) return false let userReg = /[0-9a-zA-Z]+/ if (!before.match (userReg)) return false let [firstBehind, lastBehind] = behind.split ('.' ) let firstBehindReg = /^[0-9a-zA-Z]+$/ if (!firstBehind.match (firstBehindReg)) return false let lastBehindReg = /^[0-9a-zA-Z]{2,4}$/ if (!lastBehind.match (lastBehindReg)) return false console .log (111 ); return true }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 for (let key in props.formData ) { for (let rule of props.rules [key]) { if (key in errors) break if ('validator' in rule) { rule.validator (rule, props.formData [key], (error ) => { if (error) errors.value [key] = error.message ; }) } else { if ('required' in rule) { if (rule.required && props.formData [key] === '' ) { errors.value [key] = rule.message } } if ('type' in rule && rule.type === 'email' ) { if (!validateByType (rule.type , props.formData [key]) || props.formData [key].length < 8 || props.formData [key].length > 20 ) { errors.value [key] = rule.message } } } } }