Vue3
发表于|更新于|web前端
|字数总计:2.6k|阅读时长:10分钟|阅读量:
认识Vue3
1. Vue2 选项式 API vs Vue3 组合式API
1 | <script> |
1 | <script setup> |
特点:
- 代码量变少
- 分散式维护变成集中式维护
2. Vue3的优势
使用create-vue搭建Vue3项目
1. 认识create-vue
create-vue是Vue官方新的脚手架工具,底层切换到了 vite (下一代前端工具链),为开发提供极速响应
2. 使用create-vue创建项目
前置条件 - 已安装16.0或更高版本的Node.js
执行如下命令,这一指令将会安装并执行 create-vue
1 | npm init vue@latest |
熟悉项目和关键文件
组合式API - setup选项
1. setup选项的写法和执行时机
写法
1 | <script> |
执行时机
在beforeCreate钩子之前执行
2. setup中写代码的特点
在setup函数中写的数据和方法需要在末尾以对象的方式return,才能给模版使用
1 | <script> |
3.
文章作者: 翎晗
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 L1n!
相关推荐
公告
平安喜樂,得偿所愿,祝你,也祝我
目录
- 认识Vue3
- 使用create-vue搭建Vue3项目
- 熟悉项目和关键文件
- 组合式API - setup选项
- 1. setup选项的写法和执行时机
- 2. setup中写代码的特点
- 3. 语法糖</h2><blockquote> <p>script标签添加 setup标记,不需要再写导出语句,默认会添加导出语句</p> </blockquote> <figure class="highlight plaintext"><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><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><script setup></span><br><span class="line"> const message = 'this is message'</span><br><span class="line"> const logMessage = ()=>{</span><br><span class="line"> console.log(message)</span><br><span class="line"> }</span><br><span class="line"></script></span><br></pre></td></tr></table></figure> <h1 id="组合式API-reactive和ref函数"><a href="#组合式API-reactive和ref函数" class="headerlink" title="组合式API - reactive和ref函数"></a>组合式API - reactive和ref函数</h1><h2 id="1-reactive"><a href="#1-reactive" class="headerlink" title="1. reactive"></a>1. reactive</h2><blockquote> <p>接受<strong>对象类型数据</strong>的参数传入并返回一个响应式的对象</p> </blockquote> <figure class="highlight plaintext"><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><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><script setup></span><br><span class="line"> // 导入</span><br><span class="line"> import { reactive } from 'vue'</span><br><span class="line"> // 执行函数 传入参数 变量接收</span><br><span class="line"> const state = reactive({</span><br><span class="line"> msg:'this is msg'</span><br><span class="line"> })</span><br><span class="line"> const setSate = ()=>{</span><br><span class="line"> // 修改数据更新视图</span><br><span class="line"> state.msg = 'this is new msg'</span><br><span class="line"> }</span><br><span class="line"></script></span><br><span class="line"></span><br><span class="line"><template></span><br><span class="line"> {{ state.msg }}</span><br><span class="line"> <button @click="setState">change msg</button></span><br><span class="line"></template></span><br></pre></td></tr></table></figure> <h2 id="2-ref"><a href="#2-ref" class="headerlink" title="2. ref"></a>2. ref</h2><blockquote> <p>接收<strong>简单类型</strong>或者<strong>对象类型</strong>的数据传入并返回一个响应式的对象</p> </blockquote> <figure class="highlight plaintext"><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><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><script setup></span><br><span class="line"> // 导入</span><br><span class="line"> import { ref } from 'vue'</span><br><span class="line"> // 执行函数 传入参数 变量接收</span><br><span class="line"> const count = ref(0)</span><br><span class="line"> const setCount = ()=>{</span><br><span class="line"> // 修改数据更新视图必须加上.value</span><br><span class="line"> count.value++</span><br><span class="line"> }</span><br><span class="line"></script></span><br><span class="line"></span><br><span class="line"><template></span><br><span class="line"> <button @click="setCount">{{count}}</button></span><br><span class="line"></template></span><br></pre></td></tr></table></figure> <h2 id="3-reactive-对比-ref"><a href="#3-reactive-对比-ref" class="headerlink" title="3. reactive 对比 ref"></a>3. reactive 对比 ref</h2><ol> <li>都是用来生成响应式数据</li> <li>不同点<ol> <li>reactive不能处理简单类型的数据</li> <li>ref参数类型支持更好,但是必须通过.value做访问修改</li> <li>ref函数内部的实现依赖于reactive函数</li> </ol> </li> <li>在实际工作中的推荐<ol> <li>推荐使用ref函数,减少记忆负担,小兔鲜项目都使用ref</li> </ol> </li> </ol> <h1 id="组合式API-computed"><a href="#组合式API-computed" class="headerlink" title="组合式API - computed"></a>组合式API - computed</h1><blockquote> <p>计算属性基本思想和Vue2保持一致,组合式API下的计算属性只是修改了API写法</p> </blockquote> <figure class="highlight plaintext"><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><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><script setup></span><br><span class="line">// 导入</span><br><span class="line">import {ref, computed } from 'vue'</span><br><span class="line">// 原始数据</span><br><span class="line">const count = ref(0)</span><br><span class="line">// 计算属性</span><br><span class="line">const doubleCount = computed(()=>count.value * 2)</span><br><span class="line"></span><br><span class="line">// 原始数据</span><br><span class="line">const list = ref([1,2,3,4,5,6,7,8])</span><br><span class="line">// 计算属性list</span><br><span class="line">const filterList = computed(item=>item > 2)</span><br><span class="line"></script></span><br></pre></td></tr></table></figure> <ul> <li>计算属性中不应该有 “副作用”<ul> <li>比如异步请求 / 修改dom</li> </ul> </li> <li>避免直接修改计算机属性的值<ul> <li>计算属性应该是只读的,特殊情况下可以配置get set</li> </ul> </li> </ul> <h1 id="组合式API-watch"><a href="#组合式API-watch" class="headerlink" title="组合式API - watch"></a>组合式API - watch</h1><blockquote> <p>作用:侦听一个或者多个数据的变化,数据变化时执行回调函数,</p> <p>俩个额外参数: immediate控制立刻执行,deep开启深度侦听</p> </blockquote> <h2 id="1-侦听单个数据"><a href="#1-侦听单个数据" class="headerlink" title="1. 侦听单个数据"></a>1. 侦听单个数据</h2><figure class="highlight plaintext"><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><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><script setup></span><br><span class="line"> // 1. 导入watch</span><br><span class="line"> import { ref, watch } from 'vue'</span><br><span class="line"> const count = ref(0)</span><br><span class="line"> // 2. 调用watch 侦听变化</span><br><span class="line"> watch(count, (newValue, oldValue)=>{</span><br><span class="line"> console.log(`count发生了变化,老值为${oldValue},新值为${newValue}`)</span><br><span class="line"> })</span><br><span class="line"></script></span><br></pre></td></tr></table></figure> <h2 id="2-侦听多个数据"><a href="#2-侦听多个数据" class="headerlink" title="2. 侦听多个数据"></a>2. 侦听多个数据</h2><blockquote> <p>侦听多个数据,第一个参数可以改写成数组的写法</p> </blockquote> <figure class="highlight plaintext"><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><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><script setup></span><br><span class="line"> // 1. 导入watch</span><br><span class="line"> import { ref, watch } from 'vue'</span><br><span class="line"> const count = ref(0)</span><br><span class="line"> const name = ref('cp')</span><br><span class="line"> // 2. 调用watch 侦听变化</span><br><span class="line"> watch([count, name], ([newCount, newName],[oldCount,oldName])=>{</span><br><span class="line"> console.log(`count或者name变化了,[newCount, newName],[oldCount,oldName])</span><br><span class="line"> })</span><br><span class="line"></script></span><br></pre></td></tr></table></figure> <h2 id="3-immediate"><a href="#3-immediate" class="headerlink" title="3. immediate"></a>3. immediate</h2><blockquote> <p>在侦听器创建时立即出发回调,响应式数据变化之后继续执行回调</p> </blockquote> <figure class="highlight plaintext"><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><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><script setup></span><br><span class="line"> // 1. 导入watch</span><br><span class="line"> import { ref, watch } from 'vue'</span><br><span class="line"> const count = ref(0)</span><br><span class="line"> // 2. 调用watch 侦听变化</span><br><span class="line"> watch(count, (newValue, oldValue)=>{</span><br><span class="line"> console.log(`count发生了变化,老值为${oldValue},新值为${newValue}`)</span><br><span class="line"> },{</span><br><span class="line"> immediate: true</span><br><span class="line"> })</span><br><span class="line"></script></span><br></pre></td></tr></table></figure> <h2 id="4-deep"><a href="#4-deep" class="headerlink" title="4. deep"></a>4. deep</h2><blockquote> <p>通过watch监听的ref对象默认是浅层侦听的,直接修改嵌套的对象属性不会触发回调执行,需要开启deep</p> </blockquote> <figure class="highlight plaintext"><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><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><script setup></span><br><span class="line"> // 1. 导入watch</span><br><span class="line"> import { ref, watch } from 'vue'</span><br><span class="line"> const state = ref({ count: 0 })</span><br><span class="line"> // 2. 监听对象state</span><br><span class="line"> watch(state, ()=>{</span><br><span class="line"> console.log('数据变化了')</span><br><span class="line"> })</span><br><span class="line"> const changeStateByCount = ()=>{</span><br><span class="line"> // 直接修改不会引发回调执行</span><br><span class="line"> state.value.count++</span><br><span class="line"> }</span><br><span class="line"></script></span><br><span class="line"></span><br><span class="line"><script setup></span><br><span class="line"> // 1. 导入watch</span><br><span class="line"> import { ref, watch } from 'vue'</span><br><span class="line"> const state = ref({ count: 0 })</span><br><span class="line"> // 2. 监听对象state 并开启deep</span><br><span class="line"> watch(state, ()=>{</span><br><span class="line"> console.log('数据变化了')</span><br><span class="line"> },{deep:true})</span><br><span class="line"> const changeStateByCount = ()=>{</span><br><span class="line"> // 此时修改可以触发回调</span><br><span class="line"> state.value.count++</span><br><span class="line"> }</span><br><span class="line"></script></span><br><span class="line"></span><br></pre></td></tr></table></figure> <h1 id="组合式API-生命周期函数"><a href="#组合式API-生命周期函数" class="headerlink" title="组合式API - 生命周期函数"></a>组合式API - 生命周期函数</h1><h2 id="1-选项式对比组合式"><a href="#1-选项式对比组合式" class="headerlink" title="1. 选项式对比组合式"></a>1. 选项式对比组合式</h2><p><img src="/./Vue3/6.png" alt="6"></p> <h2 id="2-生命周期函数基本使用"><a href="#2-生命周期函数基本使用" class="headerlink" title="2. 生命周期函数基本使用"></a>2. 生命周期函数基本使用</h2><blockquote> <ol> <li>导入生命周期函数</li> <li>执行生命周期函数,传入回调</li> </ol> </blockquote> <figure class="highlight plaintext"><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><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><scirpt setup></span><br><span class="line">import { onMounted } from 'vue'</span><br><span class="line">onMounted(()=>{</span><br><span class="line"> // 自定义逻辑</span><br><span class="line">})</span><br><span class="line"></script></span><br></pre></td></tr></table></figure> <h2 id="3-执行多次"><a href="#3-执行多次" class="headerlink" title="3. 执行多次"></a>3. 执行多次</h2><blockquote> <p>生命周期函数执行多次的时候,会按照顺序依次执行</p> </blockquote> <figure class="highlight plaintext"><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><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><scirpt setup></span><br><span class="line">import { onMounted } from 'vue'</span><br><span class="line">onMounted(()=>{</span><br><span class="line"> // 自定义逻辑</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line">onMounted(()=>{</span><br><span class="line"> // 自定义逻辑</span><br><span class="line">})</span><br><span class="line"></script></span><br></pre></td></tr></table></figure> <h1 id="组合式API-父子通信"><a href="#组合式API-父子通信" class="headerlink" title="组合式API - 父子通信"></a>组合式API - 父子通信</h1><h2 id="1-父传子"><a href="#1-父传子" class="headerlink" title="1. 父传子"></a>1. 父传子</h2><blockquote> <p>基本思想</p> <ol> <li>父组件中给子组件绑定属性</li> <li>子组件内部通过props选项接收数据</li> </ol> </blockquote> <p><img src="/./Vue3/7.png" alt="7"></p> <h2 id="2-子传父"><a href="#2-子传父" class="headerlink" title="2. 子传父"></a>2. 子传父</h2><blockquote> <p>基本思想</p> <ol> <li>父组件中给子组件标签通过@绑定事件</li> <li>子组件内部通过 emit 方法触发事件</li> </ol> </blockquote> <p><img src="/./Vue3/8.png" alt="8"></p> <h1 id="组合式API-模版引用"><a href="#组合式API-模版引用" class="headerlink" title="组合式API - 模版引用"></a>组合式API - 模版引用</h1><blockquote> <p>概念:通过 ref标识 获取真实的 dom对象或者组件实例对象</p> </blockquote> <h2 id="1-基本使用"><a href="#1-基本使用" class="headerlink" title="1. 基本使用"></a>1. 基本使用</h2><blockquote> <p>实现步骤:</p> <ol> <li>调用ref函数生成一个ref对象</li> <li>通过ref标识绑定ref对象到标签</li> </ol> </blockquote> <p><img src="/./Vue3/9.png" alt="9"></p> <h2 id="2-defineExpose"><a href="#2-defineExpose" class="headerlink" title="2. defineExpose"></a>2. defineExpose</h2><blockquote> <p>默认情况下在 <script setup>语法糖下组件内部的属性和方法是不开放给父组件访问的,可以通过defineExpose编译宏指定哪些属性和方法容许访问<br>说明:指定testMessage属性可以被访问到</p> </blockquote> <p><img src="/./Vue3/10.png" alt="10"></p> <h1 id="组合式API-provide和inject"><a href="#组合式API-provide和inject" class="headerlink" title="组合式API - provide和inject"></a>组合式API - provide和inject</h1><h2 id="1-作用和场景"><a href="#1-作用和场景" class="headerlink" title="1. 作用和场景"></a>1. 作用和场景</h2><blockquote> <p>顶层组件向任意的底层组件传递数据和方法,实现跨层组件通信</p> </blockquote> <p><img src="/./Vue3/11.png" alt="11"></p> <h2 id="2-跨层传递普通数据"><a href="#2-跨层传递普通数据" class="headerlink" title="2. 跨层传递普通数据"></a>2. 跨层传递普通数据</h2><blockquote> <p>实现步骤</p> <ol> <li>顶层组件通过 <code>provide</code> 函数提供数据</li> <li>底层组件通过 <code>inject</code> 函数提供数据</li> </ol> </blockquote> <p><img src="/./Vue3/12.png" alt="12"></p> <h2 id="3-跨层传递响应式数据"><a href="#3-跨层传递响应式数据" class="headerlink" title="3. 跨层传递响应式数据"></a>3. 跨层传递响应式数据</h2><blockquote> <p>在调用provide函数时,第二个参数设置为ref对象</p> </blockquote> <p><img src="/./Vue3/13.png" alt="13"></p> <h2 id="4-跨层传递方法"><a href="#4-跨层传递方法" class="headerlink" title="4. 跨层传递方法"></a>4. 跨层传递方法</h2><blockquote> <p>顶层组件可以向底层组件传递方法,底层组件调用方法修改顶层组件的数据</p> </blockquote> <p><img src="/./Vue3/14.png" alt="14"></p> <h1 id="Vue3-3-新特性-defineOptions"><a href="#Vue3-3-新特性-defineOptions" class="headerlink" title="Vue3.3 新特性-defineOptions"></a>Vue3.3 新特性-defineOptions</h1><p>背景说明:</p> <p>有 <script setup> 之前,如果要定义 props, emits 可以轻而易举地添加一个与 setup 平级的属性。 </p> <p>但是用了 <script setup> 后,就没法这么干了 setup 属性已经没有了,自然无法添加与其平级的属性。</p> <hr> <p>为了解决这一问题,引入了 defineProps 与 defineEmits 这两个宏。但这只解决了 props 与 emits 这两个属性。</p> <p>如果我们要定义组件的 name 或其他自定义的属性,还是得回到最原始的用法——再添加一个普通的 <script> 标签。</p> <p>这样就会存在两个 <script> 标签。让人无法接受。</p> <hr> <p>所以在 Vue 3.3 中新引入了 defineOptions 宏。顾名思义,主要是用来定义 Options API 的选项。可以用 defineOptions 定义任意的选项, props, emits, expose, slots 除外(因为这些可以使用 defineXXX 来做到)</p> <p><img src="/./Vue3/image-20230704082955748.png" alt="image-20230704082955748"></p> <h1 id="Vue3-3新特性-defineModel"><a href="#Vue3-3新特性-defineModel" class="headerlink" title="Vue3.3新特性-defineModel"></a>Vue3.3新特性-defineModel</h1><p>在Vue3中,自定义组件上使用v-model, 相当于传递一个modelValue属性,同时触发 update:modelValue 事件</p> <p><img src="/./Vue3/image-20230704083027349.png" alt="image-20230704083027349"></p> <p>我们需要先定义 props,再定义 emits 。其中有许多重复的代码。如果需要修改此值,还需要手动调用 emit 函数。</p> <p>于是乎 defineModel 诞生了。</p> <p><img src="/./Vue3/image-20230704083056549.png" alt="image-20230704083056549"></p> <p>生效需要配置 vite.config.js</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><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> { fileURLToPath, <span class="variable constant_">URL</span> } <span class="keyword">from</span> <span class="string">'node:url'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> { defineConfig } <span class="keyword">from</span> <span class="string">'vite'</span></span><br><span class="line"><span class="keyword">import</span> vue <span class="keyword">from</span> <span class="string">'@vitejs/plugin-vue'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// https://vitejs.dev/config/</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title function_">defineConfig</span>({</span><br><span class="line"> <span class="attr">plugins</span>: [</span><br><span class="line"> <span class="title function_">vue</span>({</span><br><span class="line"> <span class="attr">script</span>: {</span><br><span class="line"> <span class="attr">defineModel</span>: <span class="literal">true</span></span><br><span class="line"> }</span><br><span class="line"> }),</span><br><span class="line"> ],</span><br><span class="line"> <span class="attr">resolve</span>: {</span><br><span class="line"> <span class="attr">alias</span>: {</span><br><span class="line"> <span class="string">'@'</span>: <span class="title function_">fileURLToPath</span>(<span class="keyword">new</span> <span class="title function_">URL</span>(<span class="string">'./src'</span>, <span class="keyword">import</span>.<span class="property">meta</span>.<span class="property">url</span>))</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure> <h1 id="Vue3-状态管理-Pinia"><a href="#Vue3-状态管理-Pinia" class="headerlink" title="Vue3 状态管理 - Pinia"></a>Vue3 状态管理 - Pinia</h1><h2 id="1-什么是Pinia"><a href="#1-什么是Pinia" class="headerlink" title="1. 什么是Pinia"></a>1. 什么是Pinia</h2><p>Pinia 是 Vue 的专属的最新状态管理库 ,是 Vuex 状态管理工具的替代品<br><img src="/./Vue3/31.png" alt="image.png"></p> <h2 id="2-手动添加Pinia到Vue项目"><a href="#2-手动添加Pinia到Vue项目" class="headerlink" title="2. 手动添加Pinia到Vue项目"></a>2. 手动添加Pinia到Vue项目</h2><p>后面在实际开发项目的时候,Pinia可以在项目创建时自动添加,现在我们初次学习,从零开始:</p> <ol> <li>使用 Vite 创建一个空的 Vue3项目</li> </ol> <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm init vite@latest</span><br></pre></td></tr></table></figure> <ol start="2"> <li>按照官方文档安装 pinia 到项目中</li> </ol> <h2 id="3-Pinia基础使用"><a href="#3-Pinia基础使用" class="headerlink" title="3. Pinia基础使用"></a>3. Pinia基础使用</h2><ol> <li>定义store</li> <li>组件使用store</li> </ol> <p><img src="/./Vue3/32.png" alt="image.png"></p> <h2 id="4-getters实现"><a href="#4-getters实现" class="headerlink" title="4. getters实现"></a>4. getters实现</h2><p>Pinia中的 getters 直接使用 computed函数 进行模拟, 组件中需要使用需要把 getters return出去</p> <p><img src="/./Vue3/33.png" alt="image.png"></p> <h2 id="5-action异步实现"><a href="#5-action异步实现" class="headerlink" title="5. action异步实现"></a>5. action异步实现</h2><p>方式:异步action函数的写法和组件中获取异步数据的写法完全一致</p> <ul> <li><p>接口地址:<a href="http://geek.itheima.net/v1_0/channels">http://geek.itheima.net/v1_0/channels</a></p> </li> <li><p>请求方式:get</p> </li> <li><p>请求参数:无</p> </li> </ul> <p><img src="/./Vue3/34.png" alt="image.png"></p> <p>需求:在Pinia中获取频道列表数据并把数据渲染App组件的模板中<br><img src="/./Vue3/35.png" alt="image.png"></p> <h2 id="6-storeToRefs工具函数"><a href="#6-storeToRefs工具函数" class="headerlink" title="6. storeToRefs工具函数"></a>6. storeToRefs工具函数</h2><p>使用storeToRefs函数可以辅助保持数据(state + getter)的响应式解构<br><img src="/./Vue3/36.png" alt="image.png"></p> <h2 id="7-Pinia的调试"><a href="#7-Pinia的调试" class="headerlink" title="7. Pinia的调试"></a>7. Pinia的调试</h2><p>Vue官方的 dev-tools 调试工具 对 Pinia直接支持,可以直接进行调试<br><img src="/./Vue3/37.png" alt="image.png"></p> <h2 id="8-Pinia持久化插件"><a href="#8-Pinia持久化插件" class="headerlink" title="8. Pinia持久化插件"></a>8. Pinia持久化插件</h2><p>官方文档:<a href="https://prazdevs.github.io/pinia-plugin-persistedstate/zh/">https://prazdevs.github.io/pinia-plugin-persistedstate/zh/</a></p> <ol> <li>安装插件 pinia-plugin-persistedstate</li> </ol> <figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i pinia-plugin-persistedstate</span><br></pre></td></tr></table></figure> <ol start="2"> <li>使用 main.js</li> </ol> <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></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> persist <span class="keyword">from</span> <span class="string">'pinia-plugin-persistedstate'</span></span><br><span class="line">...</span><br><span class="line">app.<span class="title function_">use</span>(<span class="title function_">createPinia</span>().<span class="title function_">use</span>(persist))</span><br></pre></td></tr></table></figure> <ol start="3"> <li>配置 store/counter.js</li> </ol> <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><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> { defineStore } <span class="keyword">from</span> <span class="string">'pinia'</span></span><br><span class="line"><span class="keyword">import</span> { computed, ref } <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> useCounterStore = <span class="title function_">defineStore</span>(<span class="string">'counter'</span>, <span class="function">() =></span> {</span><br><span class="line"> ...</span><br><span class="line"> <span class="keyword">return</span> {</span><br><span class="line"> count,</span><br><span class="line"> doubleCount,</span><br><span class="line"> increment</span><br><span class="line"> }</span><br><span class="line">}, {</span><br><span class="line"> <span class="attr">persist</span>: <span class="literal">true</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure> <ol start="4"> <li>其他配置,看官网文档即可</li> </ol>