-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
474 lines (279 loc) · 21.6 KB
/
index.html
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="icon" type="image/png" href="/favicon.png"/>
<title>lsdsjy's blog</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta property="og:type" content="website">
<meta property="og:title" content="lsdsjy's blog">
<meta property="og:url" content="https://lsdsjy.github.io/index.html">
<meta property="og:site_name" content="lsdsjy's blog">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="lsdsjy">
<meta name="twitter:card" content="summary">
<link rel="alternate" href="/atom.xml" title="lsdsjy's blog" type="application/atom+xml">
<link rel="icon" href="/favicon.png">
<link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="/css/style.css">
<meta name="generator" content="Hexo 5.2.0"></head>
<body>
<div id="container">
<div id="wrap">
<header id="header">
<div id="banner"></div>
<div id="header-outer" class="outer">
<div id="header-title" class="inner">
<h1 id="logo-wrap">
<a href="/" id="logo">lsdsjy's blog</a>
</h1>
</div>
<div id="header-inner" class="inner">
<nav id="main-nav">
<a id="main-nav-toggle" class="nav-icon"></a>
<a class="main-nav-link" href="/">首页</a>
<a class="main-nav-link" href="/archives">归档</a>
</nav>
<nav id="sub-nav">
<a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="订阅"></a>
<a id="nav-search-btn" class="nav-icon" title="搜索"></a>
</nav>
<div id="search-form-wrap">
<form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit"></button><input type="hidden" name="sitesearch" value="https://lsdsjy.github.io"></form>
</div>
</div>
</div>
</header>
<div class="outer">
<section id="main">
<article id="post-ref-element-vue" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/ref-element-vue/" class="article-date">
<time datetime="2022-11-05T07:10:03.000Z" itemprop="datePublished">2022-11-05</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/ref-element-vue/">Ref Element of Component in Vue.js</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h1 id="The-Problem"><a href="#The-Problem" class="headerlink" title="The Problem"></a>The Problem</h1><p>In Vue.js we use <code>ref</code> to get the real DOM element of a node, like:</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> dom = ref()</span><br><span class="line"><span class="comment">// Output: a <div> element</span></span><br><span class="line">watch(dom, <span class="function">(<span class="params">v</span>) =></span> <span class="built_in">console</span>.log(v))</span><br><span class="line"></span><br><span class="line"><div ref=<span class="string">"dom"</span>></div></span><br></pre></td></tr></table></figure>
<p>However when the <code>ref</code>ed node is a custom component, <a target="_blank" rel="noopener" href="https://vuejs.org/guide/essentials/template-refs.html#ref-on-component">we’ll get the component instance instead</a>:</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> dom = ref()</span><br><span class="line"><span class="comment">// Output: a component instance</span></span><br><span class="line">watch(dom, <span class="function">(<span class="params">v</span>) =></span> <span class="built_in">console</span>.log(v))</span><br><span class="line"></span><br><span class="line"><Comp ref=<span class="string">"dom"</span>></Comp></span><br></pre></td></tr></table></figure>
<p>And we can use the <a target="_blank" rel="noopener" href="https://vuejs.org/api/component-instance.html#el"><code>$el</code></a> property of the instance to get the underlying DOM element:</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> dom = ref()</span><br><span class="line"><span class="comment">// Output: the root element of the component</span></span><br><span class="line">watch(dom, <span class="function">(<span class="params">v</span>) =></span> <span class="built_in">console</span>.log(v.$el))</span><br><span class="line"></span><br><span class="line"><Comp ref=<span class="string">"dom"</span>></Comp></span><br></pre></td></tr></table></figure>
<p>So far so good, unless your component has a <code>v-if</code> directive on the root:</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Comp.vue</span></span><br><span class="line"><template></span><br><span class="line"> <div v-<span class="keyword">if</span>=<span class="string">"flag"</span>>...</div></span><br><span class="line"></template></span><br></pre></td></tr></table></figure>
<p>If <code>flag</code> is not truthy when the component is mounted, we will get a comment node by accessing the <code>$el</code> property:</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Output: a comment node like <!-- --></span></span><br><span class="line">watch(dom, <span class="function">(<span class="params">v</span>) =></span> <span class="built_in">console</span>.log(v.$el))</span><br></pre></td></tr></table></figure>
<p>Of course when <code>flag</code> turns truthy the <code>$el</code> property will be the rendered <code><div></code> element. It’s all reasonable, except that <strong>the <code>$el</code> property of a component instance is not reactive</strong> and it won’t trigger any <code>watch</code> or <code>computed</code> expressions!</p>
<p>So if you want to attach event listeners to the root element of a component (I know it sounds like bad-smell code but sometimes you have to do that) using pre-defined composables like <a target="_blank" rel="noopener" href="https://vueuse.org/core/useeventlistener/#useeventlistener"><code>useEventListener</code></a>, it may not work because it uses <code>watch</code> under the hood. Basically we have no idea on the parent side when will the root element (with a <code>v-if</code>) of a child component be rendered, unless you manually <code>watch</code> the flag and emit a event from the child component.</p>
<h1 id="The-Solution"><a href="#The-Solution" class="headerlink" title="The Solution"></a>The Solution</h1><p>The solution is using <a target="_blank" rel="noopener" href="https://vuejs.org/guide/reusability/custom-directives.html#directive-hooks">custom directives</a>. As mentioned in the doc, custom directive hooks will be passed the element the directive is bound to as an argument. And in fact, the directive hooks will only be triggered when there is a real HTML element, i.e. when the root element is actually rendered. So we can write a custom directive like this:</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// binding.value should be a ref</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> vRefElement = <span class="function">(<span class="params">el, binding</span>) =></span> {</span><br><span class="line"> binding.value.value = el;</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<p>And use it like:</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> dom = ref()</span><br><span class="line"><Comp v-ref-element=<span class="string">"dom"</span> /></span><br></pre></td></tr></table></figure>
<p>Oops! An error:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'value')</span><br></pre></td></tr></table></figure>
<p>The problem here is that <a target="_blank" rel="noopener" href="https://vuejs.org/guide/essentials/reactivity-fundamentals.html#ref-unwrapping-in-templates">Vue unwraps refs in template</a> so the directive receives not the <code>dom</code> ref itself but its inside value, which is <code>undefined</code>! So to avoid this kind of unwrapping, we can wrap it in a plain object:</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> dom = ref()</span><br><span class="line"><span class="keyword">const</span> domWrap = { dom }</span><br><span class="line"><span class="comment">// Output(after flag becomes truthy): <div></span></span><br><span class="line">watch(dom, <span class="function">(<span class="params">v</span>) =></span> <span class="built_in">console</span>.log(v))</span><br><span class="line"><Comp v-ref-element=<span class="string">"domWrap.dom"</span> /></span><br></pre></td></tr></table></figure>
<p>Now it works! The <code>dom</code> ref will be filled with a <code><div></code> when <code>flag</code> becomes truthy and will trigger all relevant reactive computations.</p>
<p>See a demo here:</p>
<p><a target="_blank" rel="noopener" href="https://stackblitz.com/edit/vitejs-vite-3sqrri?file=src/App.vue">https://stackblitz.com/edit/vitejs-vite-3sqrri?file=src/App.vue</a></p>
</div>
<footer class="article-footer">
<!--<a data-url="https://lsdsjy.github.io/ref-element-vue/" data-id="cla3mturz0003oqou6fm9afnv" class="article-share-link">Share</a>-->
</footer>
</div>
</article>
<article id="post-double-underscore-esmodule" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/double-underscore-esmodule/" class="article-date">
<time datetime="2021-09-05T15:22:11.000Z" itemprop="datePublished">2021-09-05</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/double-underscore-esmodule/">为什么我们需要 __esModule</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>ES Modules(ESM)是 ES2015 规范中定义的 JavaScript 模块格式,而早在此之前社区就已经探索出了诸如 CommonJS 和 AMD 之类的模块格式,其中影响力最大的就是被 Node.js 采用的 CommonJS。在 ESM 规范出现之前,JS 社区里的三方包几乎全部采用 CJS 模块格式编写,所以当 ESM 出现时,社区必须考虑的一个问题就是 ES Modules 如何与 CommonJS Modules 互操作(interoperate)。</p>
<p class="article-more-link">
<a href="/double-underscore-esmodule/#more">…→</a>
</p>
</div>
<footer class="article-footer">
<!--<a data-url="https://lsdsjy.github.io/double-underscore-esmodule/" data-id="cla3mturw0001oqou4npt30m0" class="article-share-link">Share</a>-->
</footer>
</div>
</article>
<article id="post-blogging-platforms" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/blogging-platforms/" class="article-date">
<time datetime="2021-08-28T15:22:11.000Z" itemprop="datePublished">2021-08-28</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/blogging-platforms/">博客系统十年变迁</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>算起来接触独立博客已经有十年了,这十年来虽然产出不算频繁,但是一直都在关注博客圈子。之前简单回忆过<a href="https://lsdsjy.github.io/restart/">自己写博客的经历</a>,这次想从技术角度分析一下这十年博客系统的发展和背后的逻辑。</p>
<p class="article-more-link">
<a href="/blogging-platforms/#more">…→</a>
</p>
</div>
<footer class="article-footer">
<!--<a data-url="https://lsdsjy.github.io/blogging-platforms/" data-id="cla3mturq0000oqouh55p8dzl" class="article-share-link">Share</a>-->
</footer>
</div>
</article>
<article id="post-tech-talk-review" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/tech-talk-review/" class="article-date">
<time datetime="2020-11-09T15:22:11.000Z" itemprop="datePublished">2020-11-09</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/tech-talk-review/">简单回顾一次技术分享</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>前两周在公司内部做了一次线上的技术分享,分享前做了很多准备工作,包括花了近两个工作日的时间收集资料和制作 Keynote,以及在组内试讲、收集反馈。虽然最后的效果还是不尽如人意,但也还能接受。后来回顾了一下当时的录屏,结合组内同学的反馈以及自己的体验,总结一下这次技术分享中的不足。</p>
<p class="article-more-link">
<a href="/tech-talk-review/#more">…→</a>
</p>
</div>
<footer class="article-footer">
<!--<a data-url="https://lsdsjy.github.io/tech-talk-review/" data-id="cla3mtus30006oqouhiu8c6gv" class="article-share-link">Share</a>-->
</footer>
</div>
</article>
<article id="post-task-management" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/task-management/" class="article-date">
<time datetime="2020-11-07T15:50:00.000Z" itemprop="datePublished">2020-11-07</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/task-management/">个人任务管理工作流探索</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>最近一直在用 Microsoft To Do 记录自己要做的事,但是用多了之后越发觉得不爽,于是有了这篇文章。</p>
<h2 id="现状"><a href="#现状" class="headerlink" title="现状"></a>现状</h2><p>目前我每天用 To Do 和公司内部 Jira 管理自己要做的事。</p>
<p class="article-more-link">
<a href="/task-management/#more">…→</a>
</p>
</div>
<footer class="article-footer">
<!--<a data-url="https://lsdsjy.github.io/task-management/" data-id="cla3mtus20005oqoudht5b2ip" class="article-share-link">Share</a>-->
</footer>
</div>
</article>
<article id="post-node" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/node/" class="article-date">
<time datetime="2020-10-24T11:09:18.000Z" itemprop="datePublished">2020-10-24</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/node/">写在人生的一个小小节点</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>大约是四年前,我写了一篇文章,叫做<a target="_blank" rel="noopener" href="https://www.jianshu.com/p/f4df282e945d">《大学的十六分之一》</a>。当时的我第一次离开家乡那座小城,独自来到北京读书;高中时对大学的憧憬和计划一点点地变得模糊,日常生活被懒惰和消极侵占,但又仍然保持一点清醒,所以才能写下那些文字。可惜的是后来再也没有类似的场景。前些日子从学校毕业的时候,看到别人的大学四年总结,我却想不到能写些什么。我不是一个好学生,也没做些有趣的事,甚至没能成为一个合格的人。</p>
<p class="article-more-link">
<a href="/node/#more">…→</a>
</p>
</div>
<footer class="article-footer">
<!--<a data-url="https://lsdsjy.github.io/node/" data-id="cla3mturx0002oqou0hla9fjx" class="article-share-link">Share</a>-->
</footer>
</div>
</article>
<article id="post-restart" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/restart/" class="article-date">
<time datetime="2019-01-04T11:09:18.000Z" itemprop="datePublished">2019-01-04</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/restart/">重新开始记录</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>我在中学阶段曾两度写文章(也许用 post 更为合适)阐述“博客的意义”,却没想到会在大学时被自己遗忘殆尽。</p>
<p class="article-more-link">
<a href="/restart/#more">…→</a>
</p>
</div>
<footer class="article-footer">
<!--<a data-url="https://lsdsjy.github.io/restart/" data-id="cla3mtus10004oqou4mqg0ezt" class="article-share-link">Share</a>-->
</footer>
</div>
</article>
</section>
<aside id="sidebar">
<div class="widget-wrap">
<h3 class="widget-title">归档</h3>
<div class="widget">
<ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/11/">十一月 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/09/">九月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">八月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/11/">十一月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/10/">十月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/01/">一月 2019</a></li></ul>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">最新文章</h3>
<div class="widget">
<ul>
<li>
<a href="/ref-element-vue/">Ref Element of Component in Vue.js</a>
</li>
<li>
<a href="/double-underscore-esmodule/">为什么我们需要 __esModule</a>
</li>
<li>
<a href="/blogging-platforms/">博客系统十年变迁</a>
</li>
<li>
<a href="/tech-talk-review/">简单回顾一次技术分享</a>
</li>
<li>
<a href="/task-management/">个人任务管理工作流探索</a>
</li>
</ul>
</div>
</div>
</aside>
</div>
<footer id="footer">
<div class="outer">
<div id="footer-info" class="inner">
© 2022 lsdsjy<br>
Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
</div>
</div>
</footer>
</div>
<nav id="mobile-nav">
<a href="/" class="mobile-nav-link">首页</a>
<a href="/archives" class="mobile-nav-link">归档</a>
</nav>
<script src="https://cdn.jsdelivr.net/npm/jquery@2/dist/jquery.min.js"></script>
<script src="/js/script.js"></script>
</div>
</body>
</html>