-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
184 lines (184 loc) · 103 KB
/
search.xml
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
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>Hello Hexo</title>
<url>/2021/11/04/hello-world/</url>
<content><![CDATA[<p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>
]]></content>
<categories>
<category>前端</category>
</categories>
<tags>
<tag>javascript</tag>
</tags>
</entry>
<entry>
<title>认识Vue</title>
<url>/2021/11/05/test/</url>
<content><![CDATA[<h2 id="认识Vue"><a href="#认识Vue" class="headerlink" title="认识Vue"></a>认识Vue</h2><p>关于 Vue 的描述有不少,不外乎都会拿来与 Angular 和 React 对比,同样头顶 MVVM 双向数据驱动设计模式光环的 Angular 自然被对比的最多,但到目前为止,Angular 在热度上已明显不及 Vue,性能已成为最大的诟病。</p>
<p>Vue、React 和 Angular,其实都是在原生 JS 基础上,对面向对象不一样的实现方式而已,所以要想使用这三者中的任意一种,首先要有一定的 JS 基础和对面向对象有一定的认识。</p>
<p>在代码层面,Vue 只是一个构造函数,整个 Vue 的实现都在实例化这个构造函数开始。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://unpkg.com/vue/dist/vue.js"</span>></span><span class="tag"></<span class="name">script</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span><span class="comment">// Vue 实例元素</span></span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="comment">//数据</span></span><br><span class="line"> }</span><br><span class="line"> ...</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h3 id="认识数据驱动模式"><a href="#认识数据驱动模式" class="headerlink" title="认识数据驱动模式"></a>认识数据驱动模式</h3><p>开始接触前端编程的基本上都是先学习 DOM 节点操作,jQuery 在这一领域上一度成为了标准,所以在前端编程的领域中,jQuery 几乎是每个开发者的标配。随着前后端分离的成熟,产品或项目都趋于分布式部署,开发者已不满足于操作 DOM 节点, 设计模式便慢慢的被前端化。</p>
<p>数据驱动的设计模式在思维逻辑上与 DOM 节点操作完全不一样。<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"div1"</span>></span><span class="tag"></<span class="name">div</span>></span></span><br><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span>></span>{{message}}<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//DOM 节点操作</span></span><br><span class="line"><span class="built_in">document</span>.getElementById(<span class="string">'div1'</span>).innerText = <span class="string">'节点被动改变'</span> </span><br><span class="line"></span><br><span class="line"><span class="comment">//Vue 数据驱动: 当 message 发生改变的时候,span 会相应的发生改变,而不需要手动去改变 span。</span></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="attr">message</span>: <span class="string">'我是通过映射显示的文本'</span></span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
<h3 id="认识-MVVM-模式"><a href="#认识-MVVM-模式" class="headerlink" title="认识 MVVM 模式"></a>认识 MVVM 模式</h3><p>M:Model,称之为数据模型,在前端以对象的形式表现。<br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> data = {<span class="attr">message</span>: <span class="string">'我就是一个数据模型'</span>}</span><br></pre></td></tr></table></figure><br>V:View,视图,也就是 HTML<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span>></span>我是视图<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br>VM:ViewModel,就是连接数据和视图的桥梁,当 Model 发生改变的时候,ViewModel 便将数据映射到视图。</p>
<p>那么数据驱动模式和 MVVM 模式有什么关系,换句话说,MVVM 是数据驱动模式的一种实现,Vue 是 MVVM 的一种实现。</p>
]]></content>
<categories>
<category>前端</category>
</categories>
<tags>
<tag>vue</tag>
</tags>
</entry>
<entry>
<title>Vue组件</title>
<url>/2021/11/05/components/</url>
<content><![CDATA[<h2 id="Vue组件"><a href="#Vue组件" class="headerlink" title="Vue组件"></a>Vue组件</h2><p>组件(Component)是前端在单页面应用(SPA)上最好的一种实现方式,把所有功能模块拆解成单独的组件,每个组件都有独立的作用域,且还可以相互通信</p>
<h3 id="认识单页面应用(SPA)"><a href="#认识单页面应用(SPA)" class="headerlink" title="认识单页面应用(SPA)"></a>认识单页面应用(SPA)</h3><p>在传统的页面之间跳转,是通过刷新,重新渲染一个页面而实现,在渲染的过程中势必要加载外部资源文件,页面在服务器中渲染出来是通过一系列的生命周期,在这个过程中会因为网速等硬件问题直接影响页面的加载速度,为解决这一问题,前端在新的设计模式上引入了组件的概念,页面之间的跳转变成了组件之间的切换,不需要重新加载整个页面,也不用考虑页面的生命周期,换成组件的生命周期,在性能上大大的提升了。</p>
<h3 id="Vue-的组件实现"><a href="#Vue-的组件实现" class="headerlink" title="Vue 的组件实现"></a>Vue 的组件实现</h3><h4 id="全局组件"><a href="#全局组件" class="headerlink" title="全局组件"></a>全局组件</h4><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--组件的使用--></span></span><br><span class="line"> <span class="tag"><<span class="name">global-component</span>></span><span class="tag"></<span class="name">global-component</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//组件的定义 Vue.component(组件名称, {template})</span></span><br><span class="line">Vue.component(<span class="string">'global-component'</span>, {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<h1>全局组件</h1>'</span></span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<p>最终渲染的效果<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>全局组件<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h4 id="局部组件"><a href="#局部组件" class="headerlink" title="局部组件"></a>局部组件</h4><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--组件的使用--></span></span><br><span class="line"> <span class="tag"><<span class="name">private-component</span>></span><span class="tag"></<span class="name">private-component</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//组件的定义 Vue.component(组件名称, {template})</span></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">components</span>:{</span><br><span class="line"> <span class="string">'private-component'</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<h1>局部组件</h1>'</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<p>最终渲染的效果<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>局部组件<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h3 id="组件是一个单独的作用域"><a href="#组件是一个单独的作用域" class="headerlink" title="组件是一个单独的作用域"></a>组件是一个单独的作用域</h3><p>每个组件都有单独的作用域<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>{{count}}<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">component1</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span> </span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="attr">count</span>: <span class="number">10</span></span><br><span class="line"> },</span><br><span class="line"> <span class="attr">methods</span>: {</span><br><span class="line"> <span class="attr">increment</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">this</span>.count += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">components</span>:{</span><br><span class="line"> <span class="string">'component1'</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<button v-on:click="increment">{{ count }}</button>'</span>,</span><br><span class="line"> <span class="attr">data</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="comment">//在组件里面 data 一定是 function 并返回一个对象</span></span><br><span class="line"> <span class="keyword">return</span> {</span><br><span class="line"> <span class="attr">count</span>: <span class="number">0</span></span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">methods</span>: {</span><br><span class="line"> <span class="attr">increment</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">this</span>.count += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure><br>渲染结果为<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>10<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="comment"><!--</span></span><br><span class="line"><span class="comment"> 此按钮每次点击都会自增 1,而 p 标签永远都是为 10</span></span><br><span class="line"><span class="comment"> 原因为组件的作用域是单独的</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">button</span>></span>0<span class="tag"></<span class="name">button</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span> </span><br></pre></td></tr></table></figure><br><a href="https://dk-lan.github.io/vue/VueBasic/Component/component.html">效果预览</a></p>
<h3 id="特殊的-HTML-结构中使用-is"><a href="#特殊的-HTML-结构中使用-is" class="headerlink" title="特殊的 HTML 结构中使用 is"></a>特殊的 HTML 结构中使用 is</h3><p>比如在下拉列表(select)元素里面,子元素必须为 option,则在使用组件的时候用 is<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">select</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">option</span> <span class="attr">is</span>=<span class="string">"privateOption"</span>></span><span class="tag"></<span class="name">option</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">components</span>: {</span><br><span class="line"> <span class="string">'privateOption'</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<option value=1>1</otpion>'</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure><br>渲染结果<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">select</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">option</span> <span class="attr">value</span>=<span class="string">"1"</span>></span>1<span class="tag"></<span class="name">option</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h3 id="动态组件-is"><a href="#动态组件-is" class="headerlink" title="动态组件 - :is"></a>动态组件 - :is</h3><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span> <span class="attr">style</span>=<span class="string">"display: none;"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"changeLight"</span> @<span class="attr">click</span>=<span class="string">"changeLight"</span> /></span></span><br><span class="line"> <span class="tag"><<span class="name">br</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span> <span class="attr">:is</span>=<span class="string">"show"</span>></span><span class="tag"></<span class="name">p</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>></span><span class="javascript"></span></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span></span><br><span class="line"><span class="javascript"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span></span><br><span class="line"><span class="javascript"> <span class="attr">data</span>: {</span></span><br><span class="line"><span class="javascript"> <span class="attr">show</span>: <span class="string">'red'</span>,</span></span><br><span class="line"><span class="javascript"> },</span></span><br><span class="line"><span class="javascript"> <span class="attr">methods</span>:{</span></span><br><span class="line"><span class="javascript"> <span class="attr">changeLight</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="built_in">this</span>.show = <span class="built_in">this</span>.show == <span class="string">'red'</span> ? <span class="string">'green'</span> : <span class="string">'red'</span>;</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> },</span></span><br><span class="line"><span class="javascript"> <span class="attr">components</span>: {</span></span><br><span class="line"><span class="javascript"> <span class="attr">red</span>: {</span></span><br><span class="line"><span class="javascript"> <span class="attr">template</span>: <span class="string">'<h1>Red</h1>'</span></span></span><br><span class="line"><span class="javascript"> },</span></span><br><span class="line"><span class="javascript"> <span class="attr">green</span>: {</span></span><br><span class="line"><span class="javascript"> <span class="attr">template</span>: <span class="string">'<h1>Green</h1>'</span></span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> })</span></span><br><span class="line"><span class="javascript"></span><span class="tag"></<span class="name">script</span>></span></span><br></pre></td></tr></table></figure>
<h3 id="组件属性"><a href="#组件属性" class="headerlink" title="组件属性"></a>组件属性</h3><p>组件的属性要先声明后使用,props: [‘属性名’…]<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--组件的使用--></span></span><br><span class="line"> <span class="tag"><<span class="name">private-component</span> <span class="attr">title</span>=<span class="string">"组件属性"</span> <span class="attr">:text</span>=<span class="string">"mess"</span>></span><span class="tag"></<span class="name">private-component</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//组件的定义 Vue.component(组件名称, {template})</span></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="attr">mess</span>: <span class="string">'-动态属性'</span></span><br><span class="line"> }</span><br><span class="line"> <span class="attr">components</span>:{</span><br><span class="line"> <span class="string">'private-component'</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<h1>{{title + text}}</h1>'</span>,</span><br><span class="line"> <span class="attr">props</span>: [<span class="string">'title'</span>, <span class="string">'text'</span>]</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure><br>最终渲染的效果<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>组件属性-动态属性<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h3 id="组件自定义事件"><a href="#组件自定义事件" class="headerlink" title="组件自定义事件"></a>组件自定义事件</h3><p>和组件属性不一样的在于 <组件名 v-bind:属性名=””>,属性名要在组件中先声明再使用:props: [‘属性名’]<br>自定义事件:<组件名 v-on:自定义事件名=””>,自定义事件名不需要声明,直接用 $emit() 触发<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>{{total}}<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">increment-total</span> <span class="attr">v-on:count</span>=<span class="string">"incrementTotal"</span>></span><span class="tag"></<span class="name">increment-total</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="attr">total</span>: <span class="number">0</span></span><br><span class="line"> },</span><br><span class="line"> <span class="attr">methods</span>: {</span><br><span class="line"> <span class="attr">incrementTotal</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">this</span>.total += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">components</span>: {</span><br><span class="line"> <span class="string">'incrementTotal'</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<input type="button" @click="incrementTotal" value="Total" />'</span>,</span><br><span class="line"> <span class="attr">data</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> {</span><br><span class="line"> <span class="attr">total</span>: <span class="number">0</span></span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">methods</span>: {</span><br><span class="line"> <span class="attr">incrementTotal</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">this</span>.total += <span class="number">1</span>;</span><br><span class="line"> <span class="built_in">this</span>.$emit(<span class="string">'count'</span>)</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
<h3 id="slot-分发内容"><a href="#slot-分发内容" class="headerlink" title="slot 分发内容"></a>slot 分发内容</h3><p>Vue 组件默认是覆盖渲染,为了解决这一问题,Vue 提出了 slot 分发内容</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">component1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Sam<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Lucy<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">component1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'component1'</span>, {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string"> <div></span></span><br><span class="line"><span class="string"> <h1>Tom</h1></span></span><br><span class="line"><span class="string"> <slot></slot></span></span><br><span class="line"><span class="string"> </div></span></span><br><span class="line"><span class="string"> `</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<p>最终渲染的效果<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">component1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Tom<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="comment"><!--</span></span><br><span class="line"><span class="comment"> 如果在组件定义的 template 当中没有 <slot></slot>,那么下面两个 h1 标签将不会存在</span></span><br><span class="line"><span class="comment"> 换句话说就是 <slot></slot> = <h1>Sam</h1><h1>Lucy</h1></span></span><br><span class="line"><span class="comment"> <slot></slot>可以放到 <h1>Tom</h1> 上面进行位置调换</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Sam<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Lucy<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">component1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h4 id="具名-slot"><a href="#具名-slot" class="headerlink" title="具名 slot"></a>具名 slot</h4><p>如果要将组件里面不同的子元素放到不同的地方,那就为子元素加上一个属性 slot=”名称”,然后在组件定义的时候用名称对应位置 <slot name="名称"></slot>,其它没有 slot 属性的子元素将统一分发到 <slot></slot> 里面<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">component1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Sam<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span> <span class="attr">slot</span>=<span class="string">"lucy"</span>></span>Lucy<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">component1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'component1'</span>, {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string"> <div></span></span><br><span class="line"><span class="string"> <slot name="lucy"></slot></span></span><br><span class="line"><span class="string"> <h1>Tom</h1></span></span><br><span class="line"><span class="string"> <slot></slot></span></span><br><span class="line"><span class="string"> </div></span></span><br><span class="line"><span class="string"> `</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure><br>最终渲染的效果<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">component1</span>></span></span><br><span class="line"> <span class="comment"><!--<slot name="lucy"></slot> = <h1 slot="lucy">Lucy</h1>--></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Lucy<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Tom<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="comment"><!--其它没有 slot 属性的子元素将全部分发到 <slot></slot> 标签--></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Sam<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">component1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h3 id="模版写法"><a href="#模版写法" class="headerlink" title="模版写法"></a>模版写法</h3><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">template</span> <span class="attr">id</span>=<span class="string">"component1"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">v-model</span>=<span class="string">"name"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>{{name}}<span class="tag"></<span class="name">p</span>></span> </span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"><span class="tag"></<span class="name">template</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">component1</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span> </span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">components</span>: {</span><br><span class="line"> <span class="string">'component1'</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'#component1'</span>,</span><br><span class="line"> <span class="attr">data</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> {<span class="attr">name</span>: <span class="string">'Tom'</span>};</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category>前端</category>
</categories>
<tags>
<tag>vue</tag>
</tags>
</entry>
<entry>
<title>Vuex</title>
<url>/2021/11/08/vuex/</url>
<content><![CDATA[<h2 id="Vuex"><a href="#Vuex" class="headerlink" title="Vuex"></a>Vuex</h2><p>在介绍 Vuex 之前,我先抛出一个问题,如下<br>主要描述的结构是:</p>
<ul>
<li>组件A<ul>
<li>组件B<ul>
<li>组件B1<ul>
<li>事件:Event1</li>
<li>事件:Event2</li>
<li>事件:Event3</li>
</ul>
</li>
<li>组件B2<ul>
<li>事件:Event1</li>
<li>事件:Event2</li>
<li>事件:Event3</li>
</ul>
</li>
</ul>
</li>
<li>组件C<ul>
<li>组件C1<ul>
<li>事件:Event1</li>
<li>事件:Event2</li>
<li>事件:Event3</li>
</ul>
</li>
<li>组件C2<ul>
<li>事件:Event1</li>
<li>事件:Event2</li>
<li>事件:Event3</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>现在的问题要解决是组件C1或者C2要和组件B1或B2通信,前面有详细讲过 Vue 本身组件之间通信的方法,最简单的就是 <code>$parent</code> 和 <code>$children</code>,如果不知道如何使用的请回到上面看 <a href="https://github.com/dk-lan/vue-erp/tree/master/VueBasic/Communication">组件通信</a></p>
<p>现在用 <code>$parent</code> 和 <code>$children</code> 来上面的问题:组件C1调用组件B2的方法<br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="built_in">this</span>.$parent.$parent.$children[<span class="number">0</span>].$children[<span class="number">1</span>].Event1()</span><br></pre></td></tr></table></figure><br>代码写到这问题解决了,但是如果再多几层的组件嵌套关系,又或者组件层级关系因需求发生了改变,那上面的代码又得重新改变。</p>
<p>为了解决这样的问题,有人想到了把为何不把所有组件的方法统一到一个公共对象,因为对象是一个无序属性集合,而且还是引用类型,然后把这个对象放到最外层,这样不管组件嵌套多少级,都能调用到对应的方法。</p>
<p>那上面的案例就可以生成如下面这种结构的对象了<br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = {</span><br><span class="line"> <span class="attr">A</span>: {},</span><br><span class="line"> <span class="attr">B</span>: {},</span><br><span class="line"> <span class="attr">B1</span>: {</span><br><span class="line"> <span class="attr">Event1</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event2</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event3</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{}</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">B2</span>: {</span><br><span class="line"> <span class="attr">Event1</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event2</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event3</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{}</span><br><span class="line"> }, </span><br><span class="line"> <span class="attr">C</span>: {},</span><br><span class="line"> <span class="attr">C1</span>: {</span><br><span class="line"> <span class="attr">Event1</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event2</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event3</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{}</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">C2</span>: {</span><br><span class="line"> <span class="attr">Event1</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event2</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event3</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{}</span><br><span class="line"> }, </span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h3 id="跨组件通信之——公共对象"><a href="#跨组件通信之——公共对象" class="headerlink" title="跨组件通信之——公共对象"></a>跨组件通信之——公共对象</h3><p>场景:在组件 home 里面使用组件 counter,在 home 组件中实现计数,在组件 counter 同步显示当前计数。</p>
<h4 id="common-js"><a href="#common-js" class="headerlink" title="common.js"></a>common.js</h4><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> {</span><br><span class="line"> <span class="attr">state</span>: {</span><br><span class="line"> <span class="attr">count</span>: <span class="number">0</span></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h4 id="countercomponent"><a href="#countercomponent" class="headerlink" title="countercomponent"></a>countercomponent</h4><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">template</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h3</span>></span>count-{{common.state.count}}<span class="tag"></<span class="name">h3</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"><span class="tag"></<span class="name">template</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">script</span>></span><span class="javascript"></span></span><br><span class="line"><span class="javascript"> <span class="keyword">import</span> common <span class="keyword">from</span> <span class="string">'../../common/common.js'</span></span></span><br><span class="line"><span class="javascript"> <span class="keyword">export</span> <span class="keyword">default</span> {</span></span><br><span class="line"><span class="javascript"> <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="keyword">return</span> {</span></span><br><span class="line"><span class="javascript"> common</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"></span><span class="tag"></<span class="name">script</span>></span></span><br></pre></td></tr></table></figure>
<h4 id="homecomponent"><a href="#homecomponent" class="headerlink" title="homecomponent"></a>homecomponent</h4><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">template</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">fieldset</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">legend</span>></span><span class="tag"><<span class="name">h3</span>></span>home<span class="tag"></<span class="name">h3</span>></span><span class="tag"></<span class="name">legend</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"increment"</span> @<span class="attr">click</span>=<span class="string">"increment"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span>></span>{{common.state.count}}<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">fieldset</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">fieldset</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">legend</span>></span><span class="tag"><<span class="name">h3</span>></span>counter<span class="tag"></<span class="name">h3</span>></span><span class="tag"></<span class="name">legend</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">counter</span>></span><span class="tag"></<span class="name">counter</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">fieldset</span>></span></span><br><span class="line"> </span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"><span class="tag"></<span class="name">template</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">script</span>></span><span class="javascript"></span></span><br><span class="line"><span class="javascript"> <span class="keyword">import</span> common <span class="keyword">from</span> <span class="string">'../../common/common.js'</span></span></span><br><span class="line"><span class="javascript"> <span class="keyword">import</span> counter <span class="keyword">from</span> <span class="string">'../counter/counter.vue'</span></span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript"> <span class="keyword">export</span> <span class="keyword">default</span> {</span></span><br><span class="line"><span class="javascript"> <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="keyword">return</span> {</span></span><br><span class="line"><span class="javascript"> common</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> },</span></span><br><span class="line"><span class="javascript"> <span class="attr">components</span>: {</span></span><br><span class="line"><span class="javascript"> datagrid,</span></span><br><span class="line"><span class="javascript"> counter</span></span><br><span class="line"><span class="javascript"> },</span></span><br><span class="line"><span class="javascript"> <span class="attr">methods</span>: {</span></span><br><span class="line"><span class="javascript"> <span class="function"><span class="title">increment</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="built_in">this</span>.common.state.count += <span class="number">1</span>;</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"></span><span class="tag"></<span class="name">script</span>></span></span><br></pre></td></tr></table></figure>
<p>上面的案例是通过公共对象解决了简单的跨组件通信的问题,但如果组件 counter 也想能可以实现计数,那最简单的实现方法就是在组件 counter 当中也写一个事件 increment,但这样一来,事件就重复了。</p>
<p>封装实现就是把事件 increment 也放到 common.js 对象中去,然后组件都调用这个公共的方法。<br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> state = {</span><br><span class="line"> <span class="attr">count</span>: <span class="number">0</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> mutations = {</span><br><span class="line"> <span class="function"><span class="title">increment</span>(<span class="params"></span>)</span>{</span><br><span class="line"> state.count += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> {</span><br><span class="line"> state,</span><br><span class="line"> mutations</span><br><span class="line">};</span><br></pre></td></tr></table></figure></p>
<p>看似解决了问题,但是如果场景更复杂,组件层级更深,组件间要通信的信息更多,组件间存在同名属性同名方法但不同逻辑等,目前这种方法就解决不了,需要进行更深层次的封装。</p>
<p>Vuex 就是上面这种解决方案的更深层次的实现。</p>
<h3 id="跨组件通信之——Vuex"><a href="#跨组件通信之——Vuex" class="headerlink" title="跨组件通信之——Vuex"></a>跨组件通信之——Vuex</h3><h4 id="store-js"><a href="#store-js" class="headerlink" title="store.js"></a>store.js</h4><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">'vuex'</span></span><br><span class="line"></span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> state = {</span><br><span class="line"> <span class="attr">count</span>: <span class="number">0</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> mutations = {</span><br><span class="line"> <span class="function"><span class="title">increment</span>(<span class="params"></span>)</span>{</span><br><span class="line"> state.count += <span class="number">1</span>;</span><br><span class="line"> },</span><br><span class="line"> <span class="function"><span class="title">decrement</span>(<span class="params"></span>)</span>{</span><br><span class="line"> state.count -= <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store({</span><br><span class="line"> state,</span><br><span class="line"> mutations</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> store</span><br></pre></td></tr></table></figure>
<h4 id="app-js"><a href="#app-js" class="headerlink" title="app.js"></a>app.js</h4><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'./vuex/store'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> homeComponent <span class="keyword">from</span> <span class="string">'./components/home/home.vue'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> store,</span><br><span class="line"> <span class="attr">render</span>:<span class="function"><span class="params">h</span> =></span> h(homeComponent)</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h4 id="homecomponent-1"><a href="#homecomponent-1" class="headerlink" title="homecomponent"></a>homecomponent</h4><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"increment"</span> @<span class="attr">click</span>=<span class="string">"increment"</span>/></span></span><br><span class="line"><span class="tag"><<span class="name">span</span>></span>{{$store.state.count}}<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>></span><span class="javascript"></span></span><br><span class="line"><span class="javascript"> <span class="attr">methods</span>: {</span></span><br><span class="line"><span class="javascript"> <span class="function"><span class="title">increment</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="built_in">this</span>.$store.commit(<span class="string">'increment'</span>);</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> } </span></span><br><span class="line"><span class="javascript"></span><span class="tag"></<span class="name">script</span>></span></span><br></pre></td></tr></table></figure>
<h4 id="countercomponent-1"><a href="#countercomponent-1" class="headerlink" title="countercomponent"></a>countercomponent</h4><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">span</span>></span>{{$store.state.count}}<span class="tag"></<span class="name">span</span>></span></span><br></pre></td></tr></table></figure>
<h3 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h3><ul>
<li>Vuex 实例 store,添加到 Vue 实例化当中去,然后整个 Vue 实例都能通过 $store 获取到公共对象 store。</li>
<li>用 <code>this.$store.commit('increment');</code> 触发 mutations 的 increment 方法</li>
<li>state 是通过 mutations 来改变</li>
<li>用<code>$store.state.count</code>获取 store 的 state。</li>
</ul>
]]></content>
<categories>
<category>前端</category>
</categories>
<tags>
<tag>vue</tag>
</tags>
</entry>
<entry>
<title>路由</title>
<url>/2021/11/08/router/</url>
<content><![CDATA[<h2 id="路由"><a href="#路由" class="headerlink" title="路由"></a>路由</h2><p>通过 URL 映射到对应的功能实现,Vue 的路由使用要先引入 vue-router.js</p>
<h3 id="基本路由入门"><a href="#基本路由入门" class="headerlink" title="基本路由入门"></a>基本路由入门</h3><p>定义 component<br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> Foo = { <span class="attr">template</span>: <span class="string">'<div><h1>Foo Content</h1></div>'</span> };</span><br><span class="line"><span class="keyword">const</span> Bar = { <span class="attr">template</span>: <span class="string">'<div><h1>Bar Content</h1></div>'</span> };</span><br></pre></td></tr></table></figure><br>定义路由规则<br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//每个路由应该映射一个组件。 其中"component" 可以是自定义的组件</span></span><br><span class="line"><span class="comment">//当 url 为 http://localhost/index.html#/foo 页面会渲染组件 Foo</span></span><br><span class="line"><span class="comment">//当 url 为 http://localhost/index.html#/bar 页面会渲染组件 Bar</span></span><br><span class="line"><span class="keyword">const</span> routes = [</span><br><span class="line"> {<span class="attr">path</span>: <span class="string">'/foo'</span>, <span class="attr">component</span>: Foo},</span><br><span class="line"> {<span class="attr">path</span>: <span class="string">'/bar'</span>, <span class="attr">component</span>: Bar}</span><br><span class="line">]</span><br></pre></td></tr></table></figure><br>使用<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello VueRouter<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"><span class="comment"><!-- 使用 router-link 组件来导航. --></span></span><br><span class="line"><span class="comment"><!-- 通过传入 `to` 属性指定链接. --></span></span><br><span class="line"><span class="comment"><!-- <router-link> 默认会被渲染成一个 `<a>` 标签 --></span></span><br><span class="line"> <span class="comment"><!-- 属性 `to` 对应生成 `<a>` 标签的 `href` 属性--></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/foo"</span>></span>Foo<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/bar"</span>></span>Bar<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="comment"><!--路由匹配的组件在此处渲染--></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter({</span><br><span class="line"> routes <span class="comment">// (缩写)相当于 routes: routes</span></span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line"> router</span><br><span class="line">}).$mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure><br><a href="https://dk-lan.github.io/vue/VueBasic/Router/router.html">效果预览</a></p>
<h3 id="路由参数"><a href="#路由参数" class="headerlink" title="路由参数"></a>路由参数</h3><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello VueRouter<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/user/1"</span>></span>User1<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/user/2"</span>></span>User2<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<p>通过对象 $route.params 来获取参数<br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> User = { <span class="attr">template</span>: <span class="string">'<div><h1>{{$route.params.userid}}</h1></div>'</span> };</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> routes = [</span><br><span class="line"> {<span class="attr">path</span>: <span class="string">'/user/:userid'</span>, <span class="attr">component</span>: User}</span><br><span class="line">] </span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter({</span><br><span class="line"> routes</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line"> router</span><br><span class="line">}).$mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure><br><a href="https://dk-lan.github.io/vue/VueBasic/Router/params.html">效果预览</a></p>
<h3 id="嵌套路由"><a href="#嵌套路由" class="headerlink" title="嵌套路由"></a>嵌套路由</h3><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello VueRouter<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/floor1"</span>></span>一楼<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> Floor1 = { </span><br><span class="line"> <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string"> <div></span></span><br><span class="line"><span class="string"> <h1>一楼</h1></span></span><br><span class="line"><span class="string"> <router-link to="/floor1/floor2">二楼</router-link></span></span><br><span class="line"><span class="string"> <router-view></router-view></span></span><br><span class="line"><span class="string"> </div>`</span> </span><br><span class="line">};</span><br><span class="line"><span class="keyword">const</span> Floor2 = { <span class="attr">template</span>: <span class="string">'<div><h1>二楼</h1></div>'</span> }; </span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> routes = [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">path</span>: <span class="string">'/floor1'</span>,</span><br><span class="line"> <span class="attr">component</span>: Floor1,</span><br><span class="line"> <span class="attr">children</span>: [{</span><br><span class="line"> <span class="comment">// floor2 会被渲染在 Floor1 的 <router-view> 中</span></span><br><span class="line"> <span class="attr">path</span>: <span class="string">'floor2'</span>,</span><br><span class="line"> <span class="attr">component</span>: Floor2</span><br><span class="line"> }]</span><br><span class="line"> }</span><br><span class="line">] </span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter({</span><br><span class="line"> routes</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line"> router</span><br><span class="line">}).$mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure>
<p><a href="https://dk-lan.github.io/vue/VueBasic/Router/routerChildren.html">效果预览</a></p>
<h2 id="编程式导航"><a href="#编程式导航" class="headerlink" title="编程式导航"></a>编程式导航</h2><p>用 javascript 跳转路由<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello VueRouter<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"> <span class="comment"><!--用 `router-link` 组件进行跳转--></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/floor1"</span>></span>一楼<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="comment"><!--编程式导航1:router.replace--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"一楼"</span> @<span class="attr">click</span>=<span class="string">"router.replace('/floor1')"</span>></span></span><br><span class="line"> <span class="comment"><!--编程式导航2:router.push()--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"一楼"</span> @<span class="attr">click</span>=<span class="string">"router.push('/floor1')"</span>></span></span><br><span class="line"> <span class="comment"><!--编程式导航3:router.push({})--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"一楼"</span> @<span class="attr">click</span>=<span class="string">"router.push({path: '/floor1'})"</span>></span> </span><br><span class="line"> <span class="comment"><!--编程式导航4:router.push({}) 传参数 #/floor1/123--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"一楼"</span> @<span class="attr">click</span>=<span class="string">"router.push({path: '/floor1', params: {id: 123}})"</span>></span> </span><br><span class="line"> <span class="comment"><!--编程式导航5:router.push({}) 传参数 #/floor1?id=123--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"一楼"</span> @<span class="attr">click</span>=<span class="string">"router.push({path: '/floor1', query: {id: 123}})"</span>></span> </span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h2 id="命名路由"><a href="#命名路由" class="headerlink" title="命名路由"></a>命名路由</h2><p>在路由映射表中添加属性 name,用以对该路由映射规则命名,在编程式导航跳转路由时可以用 router.push({name: ‘名称’})<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello VueRouter<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/floor1"</span>></span>一楼<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="comment"><!--编程式导航4:router.push({name: '名称'})--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"一楼"</span> @<span class="attr">click</span>=<span class="string">"router.push({name: 'floor1'})"</span>></span> </span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> Floor1 = { </span><br><span class="line"> <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string"> <div></span></span><br><span class="line"><span class="string"> <h1>一楼</h1></span></span><br><span class="line"><span class="string"> <router-link to="/floor1/floor2">二楼</router-link></span></span><br><span class="line"><span class="string"> <router-view></router-view></span></span><br><span class="line"><span class="string"> </div>`</span> </span><br><span class="line">};</span><br><span class="line"><span class="keyword">const</span> Floor2 = { <span class="attr">template</span>: <span class="string">'<div><h1>二楼</h1></div>'</span> }; </span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> routes = [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">path</span>: <span class="string">'/floor1'</span>,</span><br><span class="line"> <span class="attr">component</span>: Floor1,</span><br><span class="line"> <span class="attr">name</span>: <span class="string">'floor1'</span>, <span class="comment">//命名</span></span><br><span class="line"> <span class="attr">children</span>: [{</span><br><span class="line"> <span class="comment">// floor2 会被渲染在 Floor1 的 <router-view> 中</span></span><br><span class="line"> <span class="attr">path</span>: <span class="string">'floor2'</span>,</span><br><span class="line"> <span class="attr">component</span>: Floor2,</span><br><span class="line"> <span class="attr">name</span>: <span class="string">'floor2'</span> <span class="comment">//命名</span></span><br><span class="line"> }]</span><br><span class="line"> }</span><br><span class="line">] </span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter({</span><br><span class="line"> routes</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line"> router</span><br><span class="line">}).$mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure></p>
<h2 id="命名视图"><a href="#命名视图" class="headerlink" title="命名视图"></a>命名视图</h2><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello VueRouter<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span> <span class="attr">name</span>=<span class="string">"a"</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span> <span class="attr">name</span>=<span class="string">"b"</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter({</span><br><span class="line"> <span class="attr">routes</span>: [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">path</span>: <span class="string">'/'</span>,</span><br><span class="line"> <span class="attr">components</span>: {</span><br><span class="line"> <span class="attr">default</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<h1>defalut router view</h1>'</span></span><br><span class="line"> },</span><br><span class="line"> <span class="attr">a</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<h1>a router view</h1>'</span></span><br><span class="line"> },</span><br><span class="line"> <span class="attr">b</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<h1>b router view</h1>'</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> router</span><br><span class="line">})</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category>前端</category>
</categories>
<tags>
<tag>vue</tag>
</tags>
</entry>
<entry>
<title>Vue自定义指令</title>
<url>/2021/11/09/directive/</url>
<content><![CDATA[<h2 id="自定义指令"><a href="#自定义指令" class="headerlink" title="自定义指令"></a>自定义指令</h2><p>在某些情况下,Vue 提供给我们的指令是非常不够的,比如有一个输入框需要在点击的时候能弹出日期控件,这个功能 Vue 并不支持,但 Vue 支持我们自定义指令。</p>
<p>自定义指令和定义组件的方式很类式,也是有全局指令和局部指令之分</p>
<h3 id="全局指令"><a href="#全局指令" class="headerlink" title="全局指令"></a>全局指令</h3><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--在这里使用了一个自定义的指令 v-global--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">value</span>=<span class="string">""</span> <span class="attr">v-global</span> /></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 注册一个全局自定义指令 v-global,指令名称不用加前缀 v-</span></span><br><span class="line"><span class="comment">// 当元素使用了 v-global 这个指令时会触发对应的 function</span></span><br><span class="line"><span class="comment">// 参数 element:使用指令的元素</span></span><br><span class="line">Vue.directive(<span class="string">'global'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">element</span>)</span>{</span><br><span class="line"> <span class="comment">//默认触发钩子函数的 inserted</span></span><br><span class="line"> element.value = <span class="string">"世界和平"</span>;</span><br><span class="line"> element.focus();</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h3 id="局部指令"><a href="#局部指令" class="headerlink" title="局部指令"></a>局部指令</h3><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--在这里使用了一个自定义的指令 v-private--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">value</span>=<span class="string">""</span> <span class="attr">v-private</span> /></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">directives</span>: {</span><br><span class="line"> <span class="comment">//注册一个局布指令 private,指令名称不用加前缀 v-</span></span><br><span class="line"> <span class="comment">// 当元素使用了 v-private 这个指令时会触发对应的 function</span></span><br><span class="line"> <span class="comment">// 参数 element:使用指令的元素</span></span><br><span class="line"> <span class="attr">private</span>: <span class="function"><span class="keyword">function</span>(<span class="params">element</span>)</span>{</span><br><span class="line"> element.style.background = <span class="string">'#ccc'</span>;</span><br><span class="line"> element.value = <span class="string">"世界和平"</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h3 id="钩子函数"><a href="#钩子函数" class="headerlink" title="钩子函数"></a>钩子函数</h3><p>钩子函数可以理解成是指令的生命周期<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--在这里使用了一个自定义的指令 v-global--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">v-model</span>=<span class="string">"text"</span> <span class="attr">v-demo</span>=<span class="string">"{color:'red'}"</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">Vue.directive(<span class="string">'demo'</span>, {</span><br><span class="line"> <span class="comment">//被绑定元素插入父节点时调用</span></span><br><span class="line"> <span class="comment">//默认触发此方法 Vue.directive('demo', function(element){})</span></span><br><span class="line"> <span class="comment">//后于 bind 触发 </span></span><br><span class="line"> <span class="comment">//参数 element: 使用指令的元素</span></span><br><span class="line"> <span class="comment">//参数 binding: 使用指令的属性对象</span></span><br><span class="line"> <span class="comment">//参数 vnode: 整个 Vue 实例</span></span><br><span class="line"> <span class="attr">inserted</span>: <span class="function"><span class="keyword">function</span>(<span class="params">element, binding, vnode</span>)</span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'inserted'</span>);</span><br><span class="line"> },</span><br><span class="line"> <span class="comment">//只调用一次,指令第一次绑定到元素时调用,</span></span><br><span class="line"> <span class="comment">//用这个钩子函数可以定义一个在绑定时执行一次的初始化动作</span></span><br><span class="line"> <span class="comment">//先于 inserted 触发</span></span><br><span class="line"> <span class="attr">bind</span>: <span class="function"><span class="keyword">function</span>(<span class="params">element, binding, vnode</span>)</span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'bind'</span>);</span><br><span class="line"> element.style.color = binding.value.color</span><br><span class="line"> },</span><br><span class="line"> <span class="comment">//被绑定元素所在的模板更新时调用,而不论绑定值是否变化</span></span><br><span class="line"> <span class="attr">update</span>: <span class="function"><span class="keyword">function</span>(<span class="params">element, binding, vnode</span>)</span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'update'</span>);</span><br><span class="line"> },</span><br><span class="line"> <span class="comment">//被绑定元素所在模板完成一次更新周期时调用。</span></span><br><span class="line"> <span class="attr">componentUpdated</span>: <span class="function"><span class="keyword">function</span>(<span class="params">element, binding, vnode</span>)</span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'componentUpdated'</span>);</span><br><span class="line"> }</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>:{</span><br><span class="line"> <span class="attr">text</span>: <span class="string">'钩子函数'</span></span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
<h3 id="案例:自定义日期控件"><a href="#案例:自定义日期控件" class="headerlink" title="案例:自定义日期控件"></a>案例:自定义日期控件</h3><p>这里用 boostrap 的 datepicker 插件实现<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--直接在 jQuery 环境下使用 datepicker 插件--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">id</span>=<span class="string">"datepicker"</span> <span class="attr">data-date-format</span>=<span class="string">"yyyy-mm-dd"</span>/></span></span><br><span class="line"> <span class="comment"><!--使用 Vue 自定义指令 v-datepicker--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">v-datepicker</span> <span class="attr">data-date-format</span>=<span class="string">"yyyy-mm-dd"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"保存"</span> @<span class="attr">click</span>=<span class="string">"save"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span>></span>{{dataform.birthday}}<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//在没有使用 Vue 前,datepicker 插件在 jQuery 的环境下是这样使用</span></span><br><span class="line">$(<span class="string">'#datepicker'</span>).datepicker();</span><br><span class="line"></span><br><span class="line"><span class="comment">//使用 Vue 自定义指令 v-datepicker</span></span><br><span class="line">Vue.directive(<span class="string">'datepicker'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">element, binding, vnode</span>)</span>{</span><br><span class="line"> <span class="comment">// data = dataform.birthday</span></span><br><span class="line"> $(element).datepicker({</span><br><span class="line"> <span class="attr">language</span>: <span class="string">'zh-CN'</span>,</span><br><span class="line"> <span class="attr">pickTime</span>: <span class="literal">false</span>,</span><br><span class="line"> <span class="attr">todayBtn</span>: <span class="literal">true</span>,</span><br><span class="line"> <span class="attr">autoclose</span>: <span class="literal">true</span></span><br><span class="line"> }).on(<span class="string">'changeDate'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="comment">//由于不是手动在 input 输入值,所以双向绑定 v-model 无效</span></span><br><span class="line"> <span class="comment">//所以需要手动改变实例的数据模型</span></span><br><span class="line"> <span class="keyword">var</span> data = $(element).data(<span class="string">'model'</span>);</span><br><span class="line"> <span class="keyword">if</span>(data){</span><br><span class="line"> <span class="comment">// datas = ['dataform', 'birthday']</span></span><br><span class="line"> <span class="keyword">var</span> datas = data.split(<span class="string">'.'</span>);</span><br><span class="line"> <span class="comment">//context = vm</span></span><br><span class="line"> <span class="keyword">var</span> context = vnode.context;</span><br><span class="line"> <span class="comment">//循环属性自动添加</span></span><br><span class="line"> datas.map(<span class="function">(<span class="params">ele, idx</span>) =></span> {</span><br><span class="line"> <span class="comment">//最后一个属性就直接赋值</span></span><br><span class="line"> <span class="keyword">if</span>(idx == datas.length - <span class="number">1</span>){</span><br><span class="line"> context[ele] = element.value</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">//动态添加属性</span></span><br><span class="line"> context = context[ele]</span><br><span class="line"> }</span><br><span class="line"> })</span><br><span class="line"> }</span><br><span class="line"> })</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="attr">dataform</span>: {}</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">methods</span>: {</span><br><span class="line"> <span class="attr">save</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="comment">//使用 $set 更新 dataform</span></span><br><span class="line"> <span class="comment">//更多 $set 的使用在下面继续介绍</span></span><br><span class="line"> <span class="built_in">this</span>.$set(<span class="built_in">this</span>.dataform)</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
<h2 id="set-介绍"><a href="#set-介绍" class="headerlink" title="$set 介绍"></a>$set 介绍</h2><p>当实例对象 data 先设置好了结构,比如:data: {dataform: {}},在后期想添加一个属性 username 时,这个 username 不会自动绑定到视图当中,所以调用 $set(原对象,新属性名,属性值) 进行绑定到视图当中<br><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"set"</span> @<span class="attr">click</span>=<span class="string">"set"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span>></span>{{dataform.username}}<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="attr">dataform</span>: {}</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">methods</span>: {</span><br><span class="line"> <span class="attr">set</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="comment">// 直接赋值不会显示在视图上</span></span><br><span class="line"> <span class="comment">// this.dataform.username = '123'</span></span><br><span class="line"> <span class="comment">//改用 $set 更新可以在视图上显示</span></span><br><span class="line"> <span class="built_in">this</span>.$set(<span class="built_in">this</span>.dataform, <span class="string">'username'</span>, <span class="string">'123'</span>)</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
]]></content>
<categories>
<category>前端</category>
</categories>
<tags>
<tag>vue</tag>
</tags>
</entry>
</search>