-
Notifications
You must be signed in to change notification settings - Fork 0
/
43093808.html
1 lines (1 loc) · 65.9 KB
/
43093808.html
1
<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover"><title>pinia的基本创建和统一创建和解构失去响应式解决办法等知识点 | 梦洁小站-属于你我的小天地</title><meta name="author" content="梦洁"><meta name="copyright" content="梦洁"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="在线代码演示 https://stackblitz.com/github/superBiuBiuMan/pinia_registerWay 使用注意点不能直接结构赋值 如果直接结构赋值,就像下面一样,就会失去响应式效果(数据变了,视图依旧不会更新) 12345<template> {{ name }}</template>cons"><meta property="og:type" content="article"><meta property="og:title" content="pinia的基本创建和统一创建和解构失去响应式解决办法等知识点"><meta property="og:url" content="https://www.dreamlove.top/43093808.html"><meta property="og:site_name" content="梦洁小站-属于你我的小天地"><meta property="og:description" content="在线代码演示 https://stackblitz.com/github/superBiuBiuMan/pinia_registerWay 使用注意点不能直接结构赋值 如果直接结构赋值,就像下面一样,就会失去响应式效果(数据变了,视图依旧不会更新) 12345<template> {{ name }}</template>cons"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202304101624410.png"><meta property="article:published_time" content="2023-02-13T17:49:01.000Z"><meta property="article:modified_time" content="2023-05-05T17:49:01.000Z"><meta property="article:author" content="梦洁"><meta property="article:tag" content="vue"><meta property="article:tag" content="pinia"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202304101624410.png"><link rel="shortcut icon" href="https://oss.dreamlove.top/i/2024/02/02/nw24wu.png"><link rel="canonical" href="https://www.dreamlove.top/43093808.html"><link rel="preconnect" href="https://lib.baomitu.com"><link rel="preconnect" href="//busuanzi.ibruce.info"><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="https://lib.baomitu.com/font-awesome/6.5.1/css/all.min.css"><link rel="stylesheet" href="https://lib.baomitu.com/fancyapps-ui/5.0.33/fancybox/fancybox.min.css" media="print" onload='this.media="all"'><script>const GLOBAL_CONFIG={root:"/",algolia:{appId:"G7A2D1SYPY",apiKey:"49056313a9be02852c7dbedfcb772831",indexName:"blog",hits:{per_page:20},languages:{input_placeholder:"搜索文章",hits_empty:"找不到您查询的内容:${query}",hits_stats:"找到 ${hits} 条结果,用时 ${time} 毫秒"}},localSearch:void 0,translate:void 0,noticeOutdate:void 0,highlight:{plugin:"highlight.js",highlightCopy:!0,highlightLang:!0,highlightHeightLimit:!1},copy:{success:"复制成功",error:"复制错误",noSupport:"浏览器不支持"},relativeDate:{homepage:!1,post:!1},runtime:"天",dateSuffix:{just:"刚刚",min:"分钟前",hour:"小时前",day:"天前",month:"个月前"},copyright:void 0,lightbox:"fancybox",Snackbar:void 0,infinitegrid:{js:"https://lib.baomitu.com/egjs-infinitegrid/4.11.1/infinitegrid.min.js",buttonText:"加载更多"},isPhotoFigcaption:!1,islazyload:!1,isAnchor:!1,percent:{toc:!0,rightside:!1},autoDarkmode:!1}</script><script id="config-diff">var GLOBAL_CONFIG_SITE={title:"pinia的基本创建和统一创建和解构失去响应式解决办法等知识点",isPost:!0,isHome:!1,isHighlightShrink:!1,isToc:!0,postUpdate:"2023-05-05 17:49:01"}</script><script>(e=>{e.saveToLocal={set:(e,t,o)=>{if(0===o)return;const a={value:t,expiry:Date.now()+864e5*o};localStorage.setItem(e,JSON.stringify(a))},get:e=>{const t=localStorage.getItem(e);if(!t)return;const o=JSON.parse(t);if(!(Date.now()>o.expiry))return o.value;localStorage.removeItem(e)}},e.getScript=(e,t={})=>new Promise((o,a)=>{const n=document.createElement("script");n.src=e,n.async=!0,n.onerror=a,n.onload=n.onreadystatechange=function(){const e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(n.onload=n.onreadystatechange=null,o())},Object.keys(t).forEach(e=>{n.setAttribute(e,t[e])}),document.head.appendChild(n)}),e.getCSS=(e,t=!1)=>new Promise((o,a)=>{const n=document.createElement("link");n.rel="stylesheet",n.href=e,t&&(n.id=t),n.onerror=a,n.onload=n.onreadystatechange=function(){const e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(n.onload=n.onreadystatechange=null,o())},document.head.appendChild(n)}),e.activateDarkMode=()=>{document.documentElement.setAttribute("data-theme","dark"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#0d0d0d")},e.activateLightMode=()=>{document.documentElement.setAttribute("data-theme","light"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#ffffff")};const t=saveToLocal.get("theme");"dark"===t?activateDarkMode():"light"===t&&activateLightMode();const o=saveToLocal.get("aside-status");void 0!==o&&("hide"===o?document.documentElement.classList.add("hide-aside"):document.documentElement.classList.remove("hide-aside"));/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)&&document.documentElement.classList.add("apple")})(window)</script><meta name="generator" content="Hexo 7.1.1"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://oss.dreamlove.top/i/2024/02/02/nw24wu.png" onerror='onerror=null,src="/img/404.png"' alt="avatar"></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">170</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">94</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">70</div></a></div><hr class="custom-hr"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情和链接</span></a></div><div class="menus_item"><a class="site-page" href="/navigation/"><i class="fa-fw fas fa-link"></i><span> 链接导航</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 私有化导航</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://mark.123916.xyz/"><i class="fa-fw fas fa-database"></i><span> 导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://cloud.reassurehome.com/"><i class="fa-fw fas fa-hippo"></i><span> Alist(私有)</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://short.123916.xyz/027a2"><i class="fa-fw fas fa-paperclip"></i><span> clash免费订阅地址</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://short.123916.xyz/admin/"><i class="fa-fw fas fa-paperclip"></i><span> 短链接生成</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 好玩的</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://make.girls.moe/#/"><i class="fa-fw fas fa-database"></i><span> 动漫头像制作</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://dfqshy.ysepan.com/"><i class="fa-fw fas fa-database"></i><span> FC游戏大全网盘</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yikm.net/"><i class="fa-fw fas fa-database"></i><span> 小霸王</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.return8090.com/"><i class="fa-fw fas fa-database"></i><span> 小霸王(广告多)</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://farm.dreamlove.top"><i class="fa-fw fas fa-database"></i><span> QQ农场经典版</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://codepip.com/"><i class="fa-fw fas fa-database"></i><span> CssGame</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://photo.123916.xyz/"><i class="fa-fw fas fa-database"></i><span> 有趣的照片</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 其他</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://buy.dreamlove.top/"><i class="fa-fw fas fa-heart"></i><span> 购物</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image:url(https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202304101624410.png)"><nav id="nav"><span id="blog-info"><a href="/" title="梦洁小站-属于你我的小天地"><span class="site-name">梦洁小站-属于你我的小天地</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情和链接</span></a></div><div class="menus_item"><a class="site-page" href="/navigation/"><i class="fa-fw fas fa-link"></i><span> 链接导航</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 私有化导航</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://mark.123916.xyz/"><i class="fa-fw fas fa-database"></i><span> 导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://cloud.reassurehome.com/"><i class="fa-fw fas fa-hippo"></i><span> Alist(私有)</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://short.123916.xyz/027a2"><i class="fa-fw fas fa-paperclip"></i><span> clash免费订阅地址</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://short.123916.xyz/admin/"><i class="fa-fw fas fa-paperclip"></i><span> 短链接生成</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 好玩的</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://make.girls.moe/#/"><i class="fa-fw fas fa-database"></i><span> 动漫头像制作</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://dfqshy.ysepan.com/"><i class="fa-fw fas fa-database"></i><span> FC游戏大全网盘</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yikm.net/"><i class="fa-fw fas fa-database"></i><span> 小霸王</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.return8090.com/"><i class="fa-fw fas fa-database"></i><span> 小霸王(广告多)</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://farm.dreamlove.top"><i class="fa-fw fas fa-database"></i><span> QQ农场经典版</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://codepip.com/"><i class="fa-fw fas fa-database"></i><span> CssGame</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://photo.123916.xyz/"><i class="fa-fw fas fa-database"></i><span> 有趣的照片</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 其他</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://buy.dreamlove.top/"><i class="fa-fw fas fa-heart"></i><span> 购物</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">pinia的基本创建和统一创建和解构失去响应式解决办法等知识点</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-02-13T17:49:01.000Z" title="发表于 2023-02-13 17:49:01">2023-02-13</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-05-05T17:49:01.000Z" title="更新于 2023-05-05 17:49:01">2023-05-05</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/vue/">vue</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.6k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>6分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" data-flag-title="pinia的基本创建和统一创建和解构失去响应式解决办法等知识点"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="在线代码演示"><a href="#在线代码演示" class="headerlink" title="在线代码演示"></a>在线代码演示</h2><ul><li><a target="_blank" rel="noopener" href="https://stackblitz.com/github/superBiuBiuMan/pinia_registerWay">https://stackblitz.com/github/superBiuBiuMan/pinia_registerWay</a></li></ul><h2 id="使用注意点"><a href="#使用注意点" class="headerlink" title="使用注意点"></a>使用注意点</h2><h3 id="不能直接结构赋值"><a href="#不能直接结构赋值" class="headerlink" title="不能直接结构赋值"></a>不能直接结构赋值</h3><ul><li>如果直接结构赋值,就像下面一样,就会失去响应式效果(数据变了,视图依旧不会更新)</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><template></span><br><span class="line"> {{ name }}</span><br><span class="line"></template></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> { name } = <span class="title function_">useCounterStore</span>();</span><br></pre></td></tr></table></figure><ul><li>如果确实需要解构赋值,可以使用<code>storeToRefs</code><ul><li><a target="_blank" rel="noopener" href="https://pinia.vuejs.org/zh/core-concepts/index.html#using-the-store">https://pinia.vuejs.org/zh/core-concepts/index.html#using-the-store</a></li></ul></li></ul><figure class="highlight js"><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="comment">// 官网示例代码</span></span><br><span class="line"><span class="keyword">import</span> { storeToRefs } <span class="keyword">from</span> <span class="string">'pinia'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="title function_">defineComponent</span>({</span><br><span class="line"> <span class="title function_">setup</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="keyword">const</span> store = <span class="title function_">useCounterStore</span>()</span><br><span class="line"> <span class="comment">// `name` and `doubleCount` 都是响应式 refs</span></span><br><span class="line"> <span class="comment">// 这也将为由插件添加的属性创建 refs</span></span><br><span class="line"> <span class="comment">// 同时会跳过任何 action 或非响应式(非 ref/响应式)属性</span></span><br><span class="line"> <span class="keyword">const</span> { name, doubleCount } = <span class="title function_">storeToRefs</span>(store)</span><br><span class="line"> <span class="comment">// 名为 increment 的 action 可以直接提取</span></span><br><span class="line"> <span class="keyword">const</span> { increment } = store</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> {</span><br><span class="line"> name,</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><br></pre></td></tr></table></figure><h3 id="多次使用依旧是相同的对象"><a href="#多次使用依旧是相同的对象" class="headerlink" title="多次使用依旧是相同的对象"></a>多次使用依旧是相同的对象</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> store2 = <span class="title function_">useShopInfo</span>();</span><br><span class="line"><span class="keyword">const</span> store3 = <span class="title function_">useShopInfo</span>();</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(store2 === store3)</span><br><span class="line"><span class="comment">//输出true</span></span><br></pre></td></tr></table></figure><h2 id="创建方式-多种"><a href="#创建方式-多种" class="headerlink" title="创建方式(多种)"></a>创建方式(多种)</h2><ul><li>官方示例的会导致重复打包</li></ul><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></pre></td><td class="code"><pre><span class="line">使用store时要先把store的定义import进来,再执行定义函数使得实例化。</span><br><span class="line">但是,在项目逐渐庞大起来后,每个组件要使用时候都要实例化吗?</span><br><span class="line">在文中开头讲过,pinia的代码分割机制是把引用它的页面合并打包,</span><br><span class="line">那像下面的例子就会有问题,user被多个页面引用,最后user store被重复打包。</span><br></pre></td></tr></table></figure><h3 id="方法0-官方示例写法"><a href="#方法0-官方示例写法" class="headerlink" title="方法0:官方示例写法"></a>方法0:官方示例写法</h3><ul><li>src/store/useShopInfo.ts</li></ul><figure class="highlight js"><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">export</span> <span class="keyword">default</span> <span class="title function_">deinfStore</span>(<span class="string">'shopInfo'</span>,{</span><br><span class="line"> ...</span><br><span class="line">})</span><br></pre></td></tr></table></figure><ul><li>main.ts</li></ul><figure class="highlight js"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> { createApp } <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> { createPinia } <span class="keyword">from</span> <span class="string">"pinia"</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">'./style.css'</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">App</span> <span class="keyword">from</span> <span class="string">'./App.vue'</span></span><br><span class="line"><span class="keyword">const</span> app = <span class="title function_">createApp</span>(<span class="title class_">App</span>);</span><br><span class="line">app.<span class="title function_">use</span>(<span class="title function_">createPinia</span>())</span><br><span class="line">app.<span class="title function_">mount</span>(<span class="string">'#app'</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure><ul><li>使用</li></ul><figure class="highlight js"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> useShopInfo <span class="keyword">from</span> <span class="string">"./store/useShopInfo.ts"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> store = <span class="title function_">useShopInfo</span>();</span><br><span class="line"></span><br><span class="line"> store.<span class="title function_">addCar</span>({</span><br><span class="line"> <span class="attr">name</span>:name.<span class="property">value</span>,</span><br><span class="line"> <span class="attr">price</span>:price.<span class="property">value</span></span><br><span class="line"> })</span><br></pre></td></tr></table></figure><h3 id="先简单了解下app-use方法"><a href="#先简单了解下app-use方法" class="headerlink" title="先简单了解下app.use方法"></a>先简单了解下app.use方法</h3><ul><li>我们先来了解下vue的<code>app.use</code>方法,<code>app.use</code>用来注册插件,插件可以是一个带 <code>install()</code> 方法的对象,亦或直接是一个将被用作 <code>install()</code> 方法的函数。插件选项 (<code>app.use()</code> 的第二个参数) 将会传递给插件的 <code>install()</code> 方法。<ul><li>若 <code>app.use()</code> <strong>对同一个插件多次调用,该插件只会被安装一次</strong>。</li></ul></li></ul><figure class="highlight js"><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"><span class="keyword">import</span> {createApp} <span class="keyword">from</span> <span class="string">"vue"</span>;</span><br><span class="line"><span class="keyword">const</span> app = <span class="title function_">createApp</span>();</span><br><span class="line"><span class="keyword">import</span> 插件带install方法 <span class="keyword">from</span> <span class="string">"./test"</span></span><br><span class="line"><span class="keyword">import</span> 插件不带install方法 <span class="keyword">from</span> <span class="string">"./test"</span></span><br><span class="line"></span><br><span class="line">app.<span class="title function_">use</span>(插件带install方法);<span class="comment">//会自动调用install方法</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//或者手动执行,以一个自定义函数作为install方法</span></span><br><span class="line">app.<span class="title function_">use</span>(<span class="function">(<span class="params">app, options</span>) =></span> {</span><br><span class="line"> <span class="comment">// dosomething</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure><ul><li>基于<code>app.use()</code> <strong>对同一个插件多次调用,该插件只会被安装一次</strong>。我们其实可以看到很多组件库都会叫我们这样使用组件库<ul><li>以<a target="_blank" rel="noopener" href="https://tdesign.tencent.com/">TDesign</a>为例</li></ul></li></ul><figure class="highlight js"><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"><span class="keyword">import</span> <span class="title class_">TDesign</span> <span class="keyword">from</span> <span class="string">'tdesign-vue-next'</span>;</span><br><span class="line"><span class="keyword">import</span> {createApp} <span class="keyword">from</span> <span class="string">"vue"</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">App</span> <span class="keyword">from</span> <span class="string">'./app.vue'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> app = <span class="title function_">createApp</span>(<span class="title class_">App</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//重点</span></span><br><span class="line">app.<span class="title function_">use</span>(<span class="title class_">TDesign</span>);</span><br><span class="line"></span><br></pre></td></tr></table></figure><ul><li>我们看看我们引入的<code>TDesign</code>是什么</li></ul><figure class="highlight js"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> {<span class="title class_">App</span>} <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> {</span><br><span class="line"> <span class="title class_">Button</span>,</span><br><span class="line"> <span class="title class_">Popup</span>,</span><br><span class="line"> <span class="title class_">Avatar</span>,</span><br><span class="line"> <span class="title class_">Icon</span>,</span><br><span class="line"> ...</span><br><span class="line">} <span class="keyword">from</span> <span class="string">'tdesign-vue-next'</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (<span class="attr">app</span>:<span class="title class_">App</span>) => {</span><br><span class="line"> app.<span class="title function_">use</span>(<span class="title class_">Button</span>)</span><br><span class="line"> app.<span class="title function_">use</span>(<span class="title class_">Popup</span>)</span><br><span class="line"> app.<span class="title function_">use</span>(<span class="title class_">Avatar</span>)</span><br><span class="line"> app.<span class="title function_">use</span>(<span class="title class_">Icon</span>)</span><br><span class="line"> app.<span class="title function_">use</span>(....)</span><br><span class="line">}</span><br></pre></td></tr></table></figure><ul><li>现在明白了吧,我们引入的<code>TDesign</code>就是一个主入口文件,他帮我们一个一个的使用了插件,从而达到了全局引入的效果,当然,你也可以自己去引入,然后按需使用插件</li></ul><h3 id="方法1-store统一实例化并暴露使用"><a href="#方法1-store统一实例化并暴露使用" class="headerlink" title="方法1:store统一实例化并暴露使用"></a>方法1:store统一实例化并暴露使用</h3><ul><li>我们可以使用此方法,我们在store文件夹下方的<code>index.ts</code>对其他store进行统一实例化然后暴露使用</li></ul><figure class="highlight js"><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"><span class="comment">//未统一实例化</span></span><br><span class="line">store</span><br><span class="line"> a.<span class="property">ts</span></span><br><span class="line"> b.<span class="property">ts</span></span><br><span class="line"> <span class="comment">//使用的时候(一般以useXXX命名,当然,也随你命名)</span></span><br><span class="line"> <span class="keyword">import</span> useA <span class="keyword">from</span> <span class="string">"../store/a"</span>;</span><br><span class="line"> <span class="keyword">const</span> infoA = <span class="title function_">useA</span>();</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(infoA.<span class="property">name</span>);<span class="comment">//输出存储的名字信息</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//统一实例化并暴露使用</span></span><br><span class="line">store</span><br><span class="line"> a.<span class="property">ts</span></span><br><span class="line"> b.<span class="property">ts</span></span><br><span class="line"> index.<span class="property">ts</span></span><br><span class="line"> <span class="comment">//使用的时候</span></span><br><span class="line"> <span class="keyword">import</span> appStore <span class="keyword">from</span> <span class="string">"./store"</span>;</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(appStore.<span class="property">infoA</span>.<span class="property">name</span>);<span class="comment">//输出存储的名字信息</span></span><br></pre></td></tr></table></figure><h4 id="创建总路口和其他仓库"><a href="#创建总路口和其他仓库" class="headerlink" title="创建总路口和其他仓库"></a>创建总路口和其他仓库</h4><ul><li>store/index.ts</li></ul><figure class="highlight js"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> shopInfo <span class="keyword">from</span> <span class="string">"./useShopInfo"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> interface <span class="title class_">AppStore</span> {</span><br><span class="line"> <span class="attr">shopInfo</span>:<span class="title class_">ReturnType</span><<span class="keyword">typeof</span> shopInfo>,</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> appStore = {} <span class="keyword">as</span> <span class="title class_">AppStore</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 注册store状态库 */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">registerStore</span> = (<span class="params"></span>) => {</span><br><span class="line"> appStore.<span class="property">shopInfo</span> = <span class="title function_">shopInfo</span>();<span class="comment">//由于没有传入pinia对象,会自动去寻找pinia对象</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> appStore;</span><br><span class="line"></span><br></pre></td></tr></table></figure><ul><li>store/user.ts</li></ul><figure class="highlight js"><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></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">export</span> <span class="keyword">default</span> <span class="title function_">defineStore</span>(<span class="string">'shopInfo'</span>,{</span><br><span class="line"> <span class="comment">//...</span></span><br><span class="line"> <span class="attr">state</span>:{</span><br><span class="line"> <span class="attr">money</span>:<span class="string">''</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//...</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure><h4 id="全局注册"><a href="#全局注册" class="headerlink" title="全局注册"></a>全局注册</h4><figure class="highlight js"><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"><span class="keyword">import</span> {createApp} <span class="keyword">from</span> <span class="string">"vue"</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">App</span> <span class="keyword">from</span> <span class="string">"./App.vue"</span>;</span><br><span class="line"><span class="keyword">import</span> {registerStore} <span class="keyword">from</span> <span class="string">"./store"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> app = <span class="title function_">createApp</span>(<span class="title class_">App</span>);</span><br><span class="line">app.<span class="title function_">use</span>(createPinia);<span class="comment">/* 使用pinia后就可以紧跟着注册了 */</span></span><br><span class="line"><span class="comment">// 注册pinia状态管理库</span></span><br><span class="line"><span class="title function_">registerStore</span>();</span><br><span class="line"></span><br><span class="line">app.<span class="title function_">mount</span>(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure><h4 id="组件使用"><a href="#组件使用" class="headerlink" title="组件使用"></a>组件使用</h4><ul><li>src/pages/A.vue</li></ul><figure class="highlight js"><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> appStore <span class="keyword">from</span> <span class="string">"../store"</span>;</span><br><span class="line"></span><br><span class="line"><span class="language-xml"><span class="tag"><<span class="name">div</span>></span> {{ appStore.shopInfo.money }} <span class="tag"></<span class="name">div</span>></span></span></span><br></pre></td></tr></table></figure><h3 id="方法2-store统一实例化并暴露使用"><a href="#方法2-store统一实例化并暴露使用" class="headerlink" title="方法2:store统一实例化并暴露使用"></a>方法2:store统一实例化并暴露使用</h3><ul><li>这种方法不需要全局注册pinia,当然,你为了美观也可以全局注册下</li><li>原理是因为<code>defineStore</code>返回值<code>useStore</code>函数,这个<code>useStore</code>函数第一个参数可以接收一个<code>pinia</code>对象,如果有传入则使用传入的<code>pinia</code>对象,如果没有传入<code>pinia</code>对象,那么会去全局寻找,否则就会报错,报错内容大概如下</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">`[🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?\n`</span> +</span><br><span class="line"><span class="string">`\tconst pinia = createPinia()\n`</span> +</span><br><span class="line"><span class="string">`\tapp.use(pinia)\n`</span> +</span><br><span class="line"><span class="string">`This will fail in production.`</span></span><br></pre></td></tr></table></figure><h4 id="创建总路口和其他仓库-1"><a href="#创建总路口和其他仓库-1" class="headerlink" title="创建总路口和其他仓库"></a>创建总路口和其他仓库</h4><ul><li>store/index.ts</li></ul><figure class="highlight js"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> {createPinia} <span class="keyword">from</span> <span class="string">"pinia"</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">UseInfo</span> <span class="keyword">from</span> <span class="string">"./user.ts"</span>;<span class="comment">//引入其他仓库</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> type { <span class="title class_">App</span> } <span class="keyword">from</span> <span class="string">'vue'</span>;<span class="comment">//或者import {App} from "vue";</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//创建唯一的store</span></span><br><span class="line"><span class="keyword">const</span> store = <span class="title function_">createPinia</span>();</span><br><span class="line"></span><br><span class="line"><span class="comment">//如果需要美观下,想要在main.js全局注册,可以添加下面方法</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (<span class="attr">app</span>:<span class="title class_">App</span>) => {</span><br><span class="line"> app.<span class="title function_">use</span>(store);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//注意大小写和传参</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> useInfo = <span class="title class_">UseInfo</span>(store);</span><br><span class="line"><span class="comment">//如果有多个也是这样子调用,名字什么的你随意</span></span><br><span class="line"> <span class="comment">//export useList = UseList(store);</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><ul><li>store/user.ts</li></ul><figure class="highlight js"><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></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">export</span> <span class="keyword">default</span> <span class="title function_">defineStore</span>(<span class="string">'shopInfo'</span>,{</span><br><span class="line"> <span class="comment">//...</span></span><br><span class="line"> <span class="attr">state</span>:{</span><br><span class="line"> <span class="attr">money</span>:<span class="string">''</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//...</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure><h4 id="全局注册-方法2可以不全局注册"><a href="#全局注册-方法2可以不全局注册" class="headerlink" title="全局注册(方法2可以不全局注册)"></a>全局注册(方法2可以不全局注册)</h4><ul><li>main.js</li></ul><figure class="highlight js"><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"><span class="keyword">import</span> {createApp} <span class="keyword">from</span> <span class="string">"vue"</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">App</span> <span class="keyword">from</span> <span class="string">"./App.vue"</span>;</span><br><span class="line"><span class="keyword">import</span> pinia <span class="keyword">from</span> <span class="string">"./store"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">bootstrap</span> = (<span class="params"></span>) => {</span><br><span class="line"> <span class="keyword">const</span> app = <span class="title function_">createApp</span>(<span class="title class_">App</span>)</span><br><span class="line"> <span class="comment">// ... </span></span><br><span class="line"> <span class="comment">// 注册TDesignUI()组件库</span></span><br><span class="line"> app.<span class="title function_">use</span>(<span class="title class_">TDesignUI</span>)</span><br><span class="line"> <span class="comment">// 加载pinia(可选)</span></span><br><span class="line"> app.<span class="title function_">use</span>(<span class="function">(<span class="params">app</span>) =></span> {</span><br><span class="line"> app.<span class="title function_">use</span>(pinia)</span><br><span class="line"> })</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 class="keyword">void</span> <span class="title function_">bootstrap</span>()</span><br></pre></td></tr></table></figure><h4 id="组件使用-1"><a href="#组件使用-1" class="headerlink" title="组件使用"></a>组件使用</h4><ul><li>src/pages/A.vue</li></ul><figure class="highlight js"><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> { useInfo } <span class="keyword">from</span> <span class="string">"../store"</span>;</span><br><span class="line"></span><br><span class="line"><span class="language-xml"><span class="tag"><<span class="name">div</span>></span> {{ useInfo.money }} <span class="tag"></<span class="name">div</span>></span></span></span><br></pre></td></tr></table></figure><h2 id="参考文章"><a href="#参考文章" class="headerlink" title="参考文章"></a>参考文章</h2><ul><li><a target="_blank" rel="noopener" href="https://juejin.cn/post/7057439040911441957">Pinia进阶:优雅的setup(函数式)写法+封装到你的企业项目</a></li><li><a target="_blank" rel="noopener" href="https://blog.csdn.net/web220507/article/details/125900040">pinia源码-defineStore源码解析</a></li></ul></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://www.dreamlove.top">梦洁</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://www.dreamlove.top/43093808.html">https://www.dreamlove.top/43093808.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外,均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://www.dreamlove.top" target="_blank">梦洁小站-属于你我的小天地</a>!</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/vue/">vue</a><a class="post-meta__tags" href="/tags/pinia/">pinia</a></div><div class="post_share"><div class="social-share" data-image="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202304101624410.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://lib.baomitu.com/butterfly-extsrc/1.1.3/sharejs/dist/css/share.min.css" media="print" onload='this.media="all"'><script src="https://lib.baomitu.com/butterfly-extsrc/1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/116b0940.html" title="Vant组件当中van-list的使用在自定义列表当中"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202304091809510.png" onerror='onerror=null,src="/img/404.png"' alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Vant组件当中van-list的使用在自定义列表当中</div></div></a></div><div class="next-post pull-right"><a href="/fa4383.html" title="微信小程序模块化、组件传值、添加data,menthods类型等-持续更新"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202304091840130.png" onerror='onerror=null,src="/img/404.png"' alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">微信小程序模块化、组件传值、添加data,menthods类型等-持续更新</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/ef46a99b.html" title="mockjs生成假数据的基本使用"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202207311537489.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-31</div><div class="title">mockjs生成假数据的基本使用</div></div></a></div><div><a href="/75bf0d7.html" title="webpack构建工具的学习"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202205291452280.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-28</div><div class="title">webpack构建工具的学习</div></div></a></div><div><a href="/e031a88b.html" title="前端面试可能会问到的知识点记录"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202207181536689.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-18</div><div class="title">前端面试可能会问到的知识点记录</div></div></a></div><div><a href="/7429306f.html" title="js正则匹配获取分组和正向反向的区别"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/20220228215037.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-11-13</div><div class="title">js正则匹配获取分组和正向反向的区别</div></div></a></div><div><a href="/be489729.html" title="navigator.clipboard.readtext开发的时候有用,编译后测试却不生效"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202210190919962.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-10-19</div><div class="title">navigator.clipboard.readtext开发的时候有用,编译后测试却不生效</div></div></a></div><div><a href="/cb27eea3.html" title="移动端前端的适配和rem,vm其他的一些的复习"><img class="cover" src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202205242024668.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-23</div><div class="title">移动端前端的适配和rem,vm其他的一些的复习</div></div></a></div></div></div><hr class="custom-hr"><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="artalk-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://oss.dreamlove.top/i/2024/02/02/nw24wu.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="avatar"></div><div class="author-info__name">梦洁</div><div class="author-info__description">小小的字,有大大的梦想~分享我的前端学习过程,经历,错误,和一些其他折腾过程</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">170</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">94</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">70</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/superBiuBiuMan"><i class="fab fa-github"></i><span>关注下我(* ̄▽ ̄*)</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="mailto:[email protected]" target="_blank" title="Email"><i class="fas fa-envelope" style="color:#4a7dbe"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">不断更新中,有问题请留言回复(会通过邮箱提醒~)</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8%E7%BA%BF%E4%BB%A3%E7%A0%81%E6%BC%94%E7%A4%BA"><span class="toc-number">1.</span> <span class="toc-text">在线代码演示</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E6%B3%A8%E6%84%8F%E7%82%B9"><span class="toc-number">2.</span> <span class="toc-text">使用注意点</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%8D%E8%83%BD%E7%9B%B4%E6%8E%A5%E7%BB%93%E6%9E%84%E8%B5%8B%E5%80%BC"><span class="toc-number">2.1.</span> <span class="toc-text">不能直接结构赋值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%9A%E6%AC%A1%E4%BD%BF%E7%94%A8%E4%BE%9D%E6%97%A7%E6%98%AF%E7%9B%B8%E5%90%8C%E7%9A%84%E5%AF%B9%E8%B1%A1"><span class="toc-number">2.2.</span> <span class="toc-text">多次使用依旧是相同的对象</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E6%96%B9%E5%BC%8F-%E5%A4%9A%E7%A7%8D"><span class="toc-number">3.</span> <span class="toc-text">创建方式(多种)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%950-%E5%AE%98%E6%96%B9%E7%A4%BA%E4%BE%8B%E5%86%99%E6%B3%95"><span class="toc-number">3.1.</span> <span class="toc-text">方法0:官方示例写法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%88%E7%AE%80%E5%8D%95%E4%BA%86%E8%A7%A3%E4%B8%8Bapp-use%E6%96%B9%E6%B3%95"><span class="toc-number">3.2.</span> <span class="toc-text">先简单了解下app.use方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%951-store%E7%BB%9F%E4%B8%80%E5%AE%9E%E4%BE%8B%E5%8C%96%E5%B9%B6%E6%9A%B4%E9%9C%B2%E4%BD%BF%E7%94%A8"><span class="toc-number">3.3.</span> <span class="toc-text">方法1:store统一实例化并暴露使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E6%80%BB%E8%B7%AF%E5%8F%A3%E5%92%8C%E5%85%B6%E4%BB%96%E4%BB%93%E5%BA%93"><span class="toc-number">3.3.1.</span> <span class="toc-text">创建总路口和其他仓库</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E6%B3%A8%E5%86%8C"><span class="toc-number">3.3.2.</span> <span class="toc-text">全局注册</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%84%E4%BB%B6%E4%BD%BF%E7%94%A8"><span class="toc-number">3.3.3.</span> <span class="toc-text">组件使用</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%952-store%E7%BB%9F%E4%B8%80%E5%AE%9E%E4%BE%8B%E5%8C%96%E5%B9%B6%E6%9A%B4%E9%9C%B2%E4%BD%BF%E7%94%A8"><span class="toc-number">3.4.</span> <span class="toc-text">方法2:store统一实例化并暴露使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E6%80%BB%E8%B7%AF%E5%8F%A3%E5%92%8C%E5%85%B6%E4%BB%96%E4%BB%93%E5%BA%93-1"><span class="toc-number">3.4.1.</span> <span class="toc-text">创建总路口和其他仓库</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E6%B3%A8%E5%86%8C-%E6%96%B9%E6%B3%952%E5%8F%AF%E4%BB%A5%E4%B8%8D%E5%85%A8%E5%B1%80%E6%B3%A8%E5%86%8C"><span class="toc-number">3.4.2.</span> <span class="toc-text">全局注册(方法2可以不全局注册)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%84%E4%BB%B6%E4%BD%BF%E7%94%A8-1"><span class="toc-number">3.4.3.</span> <span class="toc-text">组件使用</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E6%96%87%E7%AB%A0"><span class="toc-number">4.</span> <span class="toc-text">参考文章</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/bbed5cd2.html" title="大家好我是夯大力,这是我的影视,大力金刚等系列"><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202410091600476.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="大家好我是夯大力,这是我的影视,大力金刚等系列"></a><div class="content"><a class="title" href="/bbed5cd2.html" title="大家好我是夯大力,这是我的影视,大力金刚等系列">大家好我是夯大力,这是我的影视,大力金刚等系列</a><time datetime="2024-10-21T08:12:21.000Z" title="发表于 2024-10-21 08:12:21">2024-10-21</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/91ff580f.html" title="Pycharm搭配miniConda(Anaconda的轻量版本)建立虚拟环境管理不同项目不同依赖">Pycharm搭配miniConda(Anaconda的轻量版本)建立虚拟环境管理不同项目不同依赖</a><time datetime="2024-10-18T21:22:02.000Z" title="发表于 2024-10-18 21:22:02">2024-10-18</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/50575b3e.html" title="独角数卡,打开商品列表出现Undefined variable form的解决办法"><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202408241535616.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="独角数卡,打开商品列表出现Undefined variable form的解决办法"></a><div class="content"><a class="title" href="/50575b3e.html" title="独角数卡,打开商品列表出现Undefined variable form的解决办法">独角数卡,打开商品列表出现Undefined variable form的解决办法</a><time datetime="2024-08-24T15:31:38.000Z" title="发表于 2024-08-24 15:31:38">2024-08-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/6ace453b.html" title="前端常用npm库大全-vue,react,通用(持续更新)"><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202408241528979.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="前端常用npm库大全-vue,react,通用(持续更新)"></a><div class="content"><a class="title" href="/6ace453b.html" title="前端常用npm库大全-vue,react,通用(持续更新)">前端常用npm库大全-vue,react,通用(持续更新)</a><time datetime="2024-08-24T15:24:59.000Z" title="发表于 2024-08-24 15:24:59">2024-08-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/f4cb1979.html" title="基于vue-pdf-embed的二开PDF预览的通用组件"><img src="https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202408241539662.png" onerror='this.onerror=null,this.src="/img/404.png"' alt="基于vue-pdf-embed的二开PDF预览的通用组件"></a><div class="content"><a class="title" href="/f4cb1979.html" title="基于vue-pdf-embed的二开PDF预览的通用组件">基于vue-pdf-embed的二开PDF预览的通用组件</a><time datetime="2024-08-11T08:53:00.000Z" title="发表于 2024-08-11 08:53:00">2024-08-11</time></div></div></div></div></div></div></main><footer id="footer" style="background-image:url(https://dreamos.oss-cn-beijing.aliyuncs.com/gitblog/202304101624410.png)"><div id="footer-wrap"><div class="copyright">©2021 - 2024 By 梦洁</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="https://lib.baomitu.com/fancyapps-ui/5.0.33/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"><script>(()=>{let t=null;const e=()=>{if(t=Artalk.init(Object.assign({el:"#artalk-wrap",server:"https://artalk.dreamlove.top",site:"梦洁小站",pageKey:location.pathname,darkMode:"dark"===document.documentElement.getAttribute("data-theme")},null)),"null"===GLOBAL_CONFIG.lightbox)return;t.on("list-loaded",()=>{t.ctx.get("list").getCommentNodes().forEach(t=>{const e=t.getRender().$content;btf.loadLightbox(e.querySelectorAll("img:not([atk-emoticon])"))})});btf.addGlobalFn("pjax",()=>{t.destroy()},"destroyArtalk")},a=async()=>{"object"==typeof Artalk||(await getCSS("https://lib.baomitu.com/artalk/2.8.2/Artalk.min.css"),await getScript("https://lib.baomitu.com/artalk/2.8.2/Artalk.min.js")),e()};btf.addGlobalFn("themeChange",e=>{const a=document.getElementById("artalk-wrap");if(!a||!a.children.length)return;const l="dark"===e;t.setDarkMode(l)},"artalk"),a()})()</script></div><script id="canvas_nest" defer color="0,0,255" opacity="0.7" zindex="-1" count="99" mobile="false" src="https://lib.baomitu.com/butterfly-extsrc/1.1.3/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div><div class="algolia-poweredBy"></div></div></div></div></div><div id="search-mask"></div><script src="https://lib.baomitu.com/algoliasearch/4.22.1/algoliasearch-lite.umd.min.js"></script><script src="https://lib.baomitu.com/instantsearch.js/4.65.0/instantsearch.production.min.js"></script><script src="/js/search/algolia.js?v=4.13.0"></script></div></div></body></html>