投稿時間:2023-08-28 01:11:04 RSSフィード2023-08-28 01:00 分まとめ(12件)

カテゴリー等 サイト名等 記事タイトル・トレンドワード等 リンクURL 頻出ワード・要約等/検索ボリューム 登録日
AWS lambdaタグが付けられた新着投稿 - Qiita ある日突然 LINE ボットの定期通知が来なくなった https://qiita.com/yaskitie/items/a53f2284e6c523e21748 通知 2023-08-28 00:21:48
js JavaScriptタグが付けられた新着投稿 - Qiita 【make it easy】pdf-libとpdfmakeをefwに使ってみる https://qiita.com/changkejun/items/6a966c87575989ac21b8 ecmascript 2023-08-28 00:21:12
AWS AWSタグが付けられた新着投稿 - Qiita ある日突然 LINE ボットの定期通知が来なくなった https://qiita.com/yaskitie/items/a53f2284e6c523e21748 通知 2023-08-28 00:21:48
AWS AWSタグが付けられた新着投稿 - Qiita 【AWS】IAM Roleって何のためにある? https://qiita.com/Sho0987/items/93418709dfa84a1172b6 iamrole 2023-08-28 00:19:23
海外TECH MakeUseOf How to Create a WhatsApp Channel https://www.makeuseof.com/how-to-create-whatsapp-channel/ broadcast 2023-08-27 15:31:25
海外TECH MakeUseOf How to Fix the Recycle Bin Icon Setting Grayed Out in Windows 11 https://www.makeuseof.com/recycle-bin-icon-grayed-out-windows-11/ recycle 2023-08-27 15:15:27
海外TECH MakeUseOf How to Fix the Flickering Mouse Pointer on Windows https://www.makeuseof.com/fix-flickering-mouse-pointer-on-windows/ windows 2023-08-27 15:00:28
海外TECH DEV Community Unlocking the Puzzle: Investigating Multiple Event Listeners in Vue.js https://dev.to/charnog/unlocking-the-puzzle-investigating-multiple-event-listeners-in-vuejs-3cl2 Unlocking the Puzzle Investigating Multiple Event Listeners in Vue jsIn this article we will tackle a question Does Vue js support multiple event listeners Our journey will take us deep into the mechanics of Vue js unraveling some intriguing undocumented behaviors along the way Let s begin with a closer look at the official documentation on Event Handling in Vue js The primary method of attaching an event listener is through the v on click handler syntax which can also be simplified as click handler In this syntax the handler refers to a reference to a function Additionally in the Inline Handlers section it s highlighted that you can employ arbitrary JavaScript code directly within the attribute For instance you can use click count to increment a variable An important note is provided in the Method vs Inline Detection section which indicates thatThe template compiler detects method handlers by checking whether the v on value string is a valid JavaScript identifier or a property access path So does Vue support multiple listeners The answer seems to lean towards no but it s not entirely clear cut Let recap it with the code lt script setup gt import ref from vue const count ref function inc count value lt script gt lt template gt lt h gt count lt h gt lt button click count gt Incremenet by count lt button gt lt button click inc gt Incremenet by ref lt button gt lt button click inc gt Incremenet by call lt button gt lt button click gt inc gt Incremenet by lambda lt button gt lt template gt Now let s take a plunge into the JS tab within the Vue SFC Playground to closely examine how the Vue js compiler has compiled these listeners We will encounter the following code snippet I ve omitted the cache cache event gt count value portions for the sake of readability createElementVNode h null toDisplayString count value TEXT click count will be compiled to createElementVNode button onClick event gt count value Incremenet by count click inc will be compiled to createElementVNode button onClick inc Incremenet by ref click inc will be compiled to createElementVNode button onClick event gt inc Incremenet by call click gt inc will be compiled to createElementVNode button onClick gt inc Incremenet by lambda The behavior is indeed intriguing When a reference to inc is passed the compiler simplifies it to onClick inc However for count inc and gt inc the compiler follows a distinct route It encapsulates the code enclosed within the template s into a lambda function and then proceeds to execute it exactly as it s written This observation offers valuable insight if the compiler wraps code within a lambda we can leverage native JavaScript capabilities to call multiple functions within a single expression using fn fn or fn fn Let s try it We will introduce another function showAlert which will invoke the native alert function and pass count value into it You can access the updated Playground here Here is the code click count showAlert will be compiled to createElementVNode button onClick event gt count value showAlert Increment by count How to pass multiple refs createElementVNode button onClick inc Increment by ref click inc showAlert will be compiled to createElementVNode button onClick event gt inc showAlert Increment by call click gt inc showAlert will be compiled to createElementVNode button onClick gt inc showAlert Increment by lambda For click count showAlert click inc showAlert and click gt inc showAlert everything works fine allowing us to call multiple functions for a single event The issue arises when dealing with the ref case How can we pass multiple refs into the click handler The official documentation is notably silent on the topic of passing multiple references It appears that this feature might not be supported leaving us unable to achieve this behavior directly To explore this further let s experiment with the two initial approaches that come to mind fn fn and fn f and observe how they are compiled by Vue js click inc showAlert will be complied to createElementVNode button onClick event gt inc showAlert Multiple refs click inc showAlert will be complied to createElementVNode button onClick event gt inc showAlert Multiple refs Unfortunately both attempts do not yield success Vue js compiles these expressions in a manner that involves encapsulating the code enclosed within the template s This approach is consistent with the behavior we previously uncovered Let s take a step back and examine the scenario where we simply pass a function identifier without any accompanying braces in the event handler click inc will be compiled to createElementVNode button onClick inc Incremenet by ref Vue simply maps inc to onClick Now let s recap the rule we extracted from the documentation The template compiler detects method handlers by checking whether the v on value string is a valid JavaScript identifier or a property access path Incorporating the insights gained from the above we can rephrase this rule as follows If the string within the template s v on or event is recognized as a valid JavaScript identifier Vue js compiler will directly map it to onEvent lt Valid JS Identifier gt Or like this Using only the name of a variable or a function will result in direct mapping Our revised definition omits any reference to a method handler it purely states that when a valid identifier is used it is directly passed as is This implies that you can even pass a numeric value like to an onClick handler provided you first create an identifier in other words a variable that s bound to the value Passing a number as a handler clearly won t yield the desired results However as we recall our aim is to pass multiple handlers as an array of refs Given our newly established understanding this is achievable However the prerequisite is to create a valid JS Identifier variable to store the reference to the array Let s put this into action and see the results Take a look here An interesting observation unfolds Firstly using a valid JS Identifier variable named multiple we successfully pass an array to onClick and it gets mapped accordingly However TypeScript expresses its discontent It raises an error stating Type gt void is not assignable to type payload MouseEvent gt void Type gt void provides no match for the signature payload MouseEvent void ts In essence the types within Vue js prevent us from passing an array of functions as a click listener Let s set this aside for now and simply click on the button to observe whether both listeners will be invoked And yes they are We witness the counter value incrementing followed by the appearance of the alert But hold on there s a puzzle to solve Why is this functioning What s going on behind the scenes To comprehend this we must delve deeper and grasp the mechanics underlying the translation of Vue s VNode created by the createElementVNode function into a native DOM element The key lies in exploring the source code of Vue js itself When we call the createApp function in our main app js or index js it triggers a chain of events that leads to the execution of the createRenderer function look for createApp function here This sequence results in the formation of an app instance complete with a mount method This method establishes an association with the renderer see ensureRenderer here This renderer s primary task is to convert our VNodes into the native DOM elements we interact with Here s an overview of the key steps We compile our template resulting in a series of createElementVNode calls These calls build our Virtual DOM generating VNodes The renderer then traverses these nodes converting them into native DOM elements As the renderer transforms VNodes into native DOM elements it performs additional tasks using the props object of a VNode through the patchProp method Additionally note that the createRenderer rendererOptions function is invoked with extended rendererOptions encompassing a patched patchProp method Let s delve into this for further understanding export const patchProp DOMRendererOptions patchProp Omitted params gt if key class patchClass el nextValue isSVG else if key style patchStyle el prevValue nextValue Keep in mind that we provide an object containing on lt EventName gt keys isOn key will return true for these keys else if isOn key if isModelListener key If the listener isn t intended for v model we utilize the patchEvent mechanism patchEvent el key prevValue nextValue parentComponent We can interpret the code as follows If a prop is class apply special handling based on class values If a prop is style implement special handling based on style values And if a prop begins with on perform specific actions using patchEvent Let s direct our attention to the patchEvent method We ve reached the bottom where Vue establishes native event bindings through the browser s addEventListener method However prior to this step there are additional operations in play The high level call chain is as follows patchEvent is invoked It proceeds to call createInvoker in order to generate an invoker function Inside the invoker we invoke callWithAsyncErrorHandling passing a wrapped version altered by patchStopImmediatePropagation of the value provided in the click event handler Now let s examine patchStopImmediatePropagation to uncover the answer to the question Why does passing multiple refs to a function work function patchStopImmediatePropagation e Event value EventValue EventValue If the value is an array there s even more to explore We can call event stopImmediatePropagation and other functions within the array won t be invoked if isArray value const originalStop e stopImmediatePropagation e stopImmediatePropagation gt originalStop call e e as any stopped true This is where the actual function calls occur return value map fn gt e Event gt e as any stopped amp amp fn amp amp fn e else return value And here we are fully informed Even though the official documentation and TypeScript might not explicitly endorse it we ve found a code segment that effectively allows us to pass event listeners using an array of function references There is the commit that introduced this functionality It appears that at some point in the past there might have been an intention to enable the capability of passing multiple listeners However as it stands now this remains an undocumented feature Lastly let s address the question we initially posed Does Vue support multiple listeners The answer hinges on your interpretation of supports To summarize We can invoke multiple functions using fn fn and there s a test for that We can also invoke them using fn fn We can pass it through an array if stored in a variable Alternatively given the newfound knowledge we can even call them like so lt template gt lt button click fn fn forEach fn gt fn event gt Click lt button gt lt template gt 2023-08-27 15:48:40
Apple AppleInsider - Frontpage News Daily deals Aug. 27: $200 off 15" MacBook Air, up to $650 off Samsung Tab S9, Google Home for $75, more https://appleinsider.com/articles/23/08/27/daily-deals-aug-27-200-off-15-macbook-air-up-to-650-off-samsung-tab-s9-google-home-for-75-more?utm_medium=rss Daily deals Aug off quot MacBook Air up to off Samsung Tab S Google Home for moreToday s top deals include off a De Longhi portable air conditioner off an ecobee SmartCamera off a MSI Aegis Gaming PC Chromebooks from and more Save on a JBL Xtreme SpeakerThe AppleInsider team searches the internet for top notch deals at online stores to create a list of stellar bargains on popular tech products including deals on Apple items TVs accessories and other gadgets We post the most valuable deals daily to help you save money Read more 2023-08-27 15:23:21
金融 ◇◇ 保険デイリーニュース ◇◇(損保担当者必携!) 保険デイリーニュース(08/28) http://www.yanaharu.com/ins/?p=5318 損保ジャパン 2023-08-27 15:00:36
ニュース BBC News - Home In pictures: Redheads celebrate fiery locks at Dutch festival https://www.bbc.co.uk/news/world-europe-66632465?at_medium=RSS&at_campaign=KARANGA ducth 2023-08-27 15:34:12
ニュース BBC News - Home Dutch Grand Prix: Max Verstappen survives losing lead to equal all-time consecutive wins record https://www.bbc.co.uk/sport/formula1/66633708?at_medium=RSS&at_campaign=KARANGA Dutch Grand Prix Max Verstappen survives losing lead to equal all time consecutive wins recordRed Bull s Max Verstappen survives losing the lead in early rain and a red flag for a late torrential downpour to take victory in a chaotic Dutch Grand Prix 2023-08-27 15:52:40

コメント

このブログの人気の投稿

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2020-12-01 09:41:49 RSSフィード2020-12-01 09:00 分まとめ(69件)