<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by PeerOne Front-End Director on Medium]]></title>
        <description><![CDATA[Stories by PeerOne Front-End Director on Medium]]></description>
        <link>https://medium.com/@peerone.frontend.director?source=rss-16e410c6a165------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1SMXpWTUGPndicz9cKa5Jg@2x.jpeg</url>
            <title>Stories by PeerOne Front-End Director on Medium</title>
            <link>https://medium.com/@peerone.frontend.director?source=rss-16e410c6a165------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 09:28:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@peerone.frontend.director/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Flutter Cupertino Modal Bottom Sheet]]></title>
            <link>https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/flutter-cupertino-modal-bottom-sheet-60b41f43c9ff?source=rss-16e410c6a165------2</link>
            <guid isPermaLink="false">https://medium.com/p/60b41f43c9ff</guid>
            <category><![CDATA[bottomsheet]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[PeerOne Front-End Director]]></dc:creator>
            <pubDate>Sun, 21 Feb 2021 12:02:18 GMT</pubDate>
            <atom:updated>2021-02-21T12:02:18.086Z</atom:updated>
            <content:encoded><![CDATA[<h3>Flutter-Cupertino Modal Bottom Sheet</h3><p>這篇要教大家如何在 Flutter 實作 iOS13 開始支援的 Bottom Sheet 效果，如下圖 Uber Eats 就有使用這個效果</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*R11r77I44HjrUfljbEouIA.gif" /></figure><h3>安裝 Plugin</h3><p>首先要先安裝 modal_bottom_sheet</p><p><strong>pubspec.yaml</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*nObUE4zHXcbpt64i2QD5-w.png" /></figure><h3>調整 Route</h3><p>為了要有動畫效果我們還要在需要使用此效果的 route 做一些處理</p><ol><li>如果你使用 Navigator.of(context).push 寫法，需要改成</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/917/1*Y94xZb241Vl_kwiEe_Vuog.png" /></figure><p>2. 如果你使用 onGenerateRoute 寫法，需要改成</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/934/1*HXFEyDNeey1_2vohV8Y_Fw.png" /></figure><p>MaterialWithModalsPageRoute 這個是 modal_bottom_sheet 提供的 class，作者有說明為何不能直接使用。</p><blockquote>MaterialPageRoute and CupertinoPageRoute do not allow animated translation to/from routes that are not the same type.</blockquote><h3>實作 Bottom Sheet 內容</h3><p>Bottom sheet 必須是 Navigator 或 CupertinoTabScaffold ，這邊用 Navigator 舉例。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*Pwc4GIinfpNs1dXBI3Fkug.png" /></figure><p>最後我們使用 showCupertinoModalBottomSheet 就可以打開 bottom sheet</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*r-jWxsr6eJWmcLsLEKEMoQ.png" /></figure><h3>流程說明</h3><p>以 Uber Eats 的 App 為例子，我們在首頁點選篩選器，篩選頁會被 Navigator push 到 stack 中，此時首頁則在篩選頁下面堆疊。再利用 modal_bottom_sheet 達到前後視差效果( Android 也可以達到此效果 )。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*la2-rWydgGg4DjEAKkYwbw.png" /></figure><h3><strong>結語</strong></h3><p>利用 modal_bottom_sheet 可以輕鬆達到頁面前後視差效果，但使用後發現目前第一次觸發 bottom sheet 會有畫面卡一下的問題（之後皆不會），使用時可以考慮自己實作動畫效果 ( secondAnimation 參數可自定義 push/pop 動畫 )，以上若有錯誤也請歡迎指正唷。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=60b41f43c9ff" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/flutter-cupertino-modal-bottom-sheet-60b41f43c9ff">Flutter Cupertino Modal Bottom Sheet</a> was originally published in <a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80">PeerOne Technology 皮偶玩互動科技</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nuxt — 網站建置與佈署 Part 2]]></title>
            <link>https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/nuxt-%E7%B6%B2%E7%AB%99%E5%BB%BA%E7%BD%AE%E8%88%87%E4%BD%88%E7%BD%B2-part-2-e59b7b32d972?source=rss-16e410c6a165------2</link>
            <guid isPermaLink="false">https://medium.com/p/e59b7b32d972</guid>
            <category><![CDATA[google-compute-engine]]></category>
            <category><![CDATA[nuxtjs]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[PeerOne Front-End Director]]></dc:creator>
            <pubDate>Tue, 17 Nov 2020 14:01:36 GMT</pubDate>
            <atom:updated>2020-11-17T14:01:36.937Z</atom:updated>
            <content:encoded><![CDATA[<h3>Nuxt — 網站建置與佈署 Part 2</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XUSqVVVabODnhYrE.png" /></figure><p>上一篇 <a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/nuxt-%E7%B6%B2%E7%AB%99%E5%BB%BA%E7%BD%AE%E8%88%87%E4%BD%88%E7%BD%B2-part-1-55059a6e50b4">Nuxt — 網站建置與佈署 Part 1</a> 已經準備好環境和必要的工具，這次我們要讓網站可以運作起來！</p><p>環境：Ubuntu</p><p>版控：Bitbucket</p><h4>利用 git 下載程式碼到 vm</h4><p>首先我們需要新增一組給 Bitbucket 使用的 SSH Key，這樣子我們才有權限下載 code 到 vm 上。</p><ol><li>連進 vm 後，我們使用 sudo -s 使用超級用戶來執行後面的指令</li><li>新增 ssh key ssh-keygen -t rsa -C “你的信箱” ，接著到 /root 目錄 cd /root</li><li>新增 key 到 ssh-agent</li></ol><ul><li>eval ssh-agent</li><li>ssh-add ~/.ssh/&lt;private_key_file&gt;</li></ul><p>4. cat ~/.ssh/&lt;public_key_file&gt; 複製公鑰到 Bitbucket（個人）新增 SSH key</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zR7diDKyaKU46La8B2cC7A.png" /></figure><p>5. 驗證配置及用戶 ssh -T git@bitbucket.org</p><blockquote>conq: logged in as &lt;username&gt;. You can use git or hg to connect to Bitbucket. Shell access is disabled.</blockquote><p>看到此訊息代表驗證就成功了</p><p>6. 最後我們回到使用者目錄下載專案到 vm 上</p><ul><li>git clone <a href="/service/mailto:git@bitbucket.org">git@bitbucket.org</a>:xxxx/yyyy.git</li></ul><p>7. cd 到專案目錄後執行yarn install 和 yarn build 看看是否可以成功執行，若之後有新的 code 要上版，只需要執行 git pull 就可以了，當然有更動 package.json 的話也要記得再跑一次 yarn install 。</p><h4>設定 Nginx</h4><ol><li>cd /etc/nginx/conf.d新增 nginx 配置檔案 (使用 nano 來操作)。</li><li>nana {你要的名稱}.conf ， 配置內容可以參考 <a href="https://nuxtjs.org/faq/nginx-proxy">官網</a> 的範例，完成後重新啟動 nginx sudo nginx -s reload</li></ol><h4>啟動 PM2 讓 Nuxt 跑起來</h4><p>先前已經裝好了 PM2 ，配置好 ecosystem.config.js 後，我們接著執行 pm2 start，讓 Nuxt 跑起來。</p><p>再用 pm2 list 查看狀態，若 status 為 online 就代表成功囉！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1004/1*XGNNc4tDOhGJCeMgI0oWlA.png" /></figure><p>假如有問題的話也可以用 pm2 log 查看發生什麼事。</p><h4>設定 Https ssl 憑證</h4><ol><li>安裝 certbot</li></ol><pre>sudo apt-get install software-properties-common # 載入 certbot 的 ppa<br>sudo add-apt-repository ppa:certbot/certbot<br>sudo apt-get update<br>sudo apt-get install python-certbot-nginx # 安裝 python 的 certbot for nginx````</pre><p>2. sudo certbot --nginx 產生憑證</p><p>3. sudo nginx -s reload 重新整理 nginx</p><p>4. 最後到網站查看是不是可以用 https 就可以了</p><p>5. 若之後憑證快到期可以執行 sudo certbot renew 做憑證續約動作</p><h4>結語</h4><p>這篇快速介紹在 vm 環境讓網站整個跑起來的流程，但只要有更新程式碼就必須連進 vm 裡再跑一次指令，下一篇就會介紹使用 ci /cd 自動幫你做掉這些事情！</p><h4>參考</h4><ul><li><a href="https://blog.gtwang.org/linux/using-ssh-agent-forwarding-to-avoid-being-asked-passphrase/">https://blog.gtwang.org/linux/using-ssh-agent-forwarding-to-avoid-being-asked-passphrase/</a></li><li><a href="https://support.atlassian.com/bitbucket-cloud/docs/set-up-an-ssh-key/">https://support.atlassian.com/bitbucket-cloud/docs/set-up-an-ssh-key/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e59b7b32d972" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/nuxt-%E7%B6%B2%E7%AB%99%E5%BB%BA%E7%BD%AE%E8%88%87%E4%BD%88%E7%BD%B2-part-2-e59b7b32d972">Nuxt — 網站建置與佈署 Part 2</a> was originally published in <a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80">PeerOne Technology 皮偶玩互動科技</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nuxt - 網站建置與佈署 Part 1]]></title>
            <link>https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/nuxt-%E7%B6%B2%E7%AB%99%E5%BB%BA%E7%BD%AE%E8%88%87%E4%BD%88%E7%BD%B2-part-1-55059a6e50b4?source=rss-16e410c6a165------2</link>
            <guid isPermaLink="false">https://medium.com/p/55059a6e50b4</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[nuxtjs]]></category>
            <dc:creator><![CDATA[PeerOne Front-End Director]]></dc:creator>
            <pubDate>Mon, 05 Oct 2020 12:03:13 GMT</pubDate>
            <atom:updated>2020-10-05T12:03:13.865Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6K6uoku3z-s1DMieFbxqYw.png" /></figure><p>相信有用 Vue 做 SSR 的網站的工程師都對 Nuxt 都不會太陌生，但要如何建置網站發佈到線上呢？這篇會使用 GCP 介紹 Nuxt 網站的建置給前端新手參考。( 🚧 文章皆使用 macOS 作為範例 )</p><h4>Server 建立</h4><p>Server 的部分會使用 GCP 的 Compute Engine 建置。</p><p>1. 選擇「VM執行個體」並新增一個 VM 執行個體</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yedv9wYh9M5YoncBN1i4FA.png" /></figure><p>2. 「區域」可以選擇台灣、「名稱」及「機器設定」可依照個人需求調整。</p><p>3. 「開機磁碟」這篇教學會使用 Ubuntu</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*0TIUIUdSLcsnpNN6D_WKSg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xkl-_l-NXh2Vyucd0G4fSQ.png" /></figure><p>4. 完成後就會在列表看到剛剛建立的 VM 執行個體</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5CFz7dIMHDtgp_sycNh7Qg.png" /></figure><h4>連線 VM</h4><p>建立好 Server 後我們需要連線至 VM 安裝需要的軟體，接下來會教大家在本地用 <a href="https://zh.wikipedia.org/wiki/%E5%85%AC%E5%BC%80%E5%AF%86%E9%92%A5%E5%8A%A0%E5%AF%86">公開金鑰加密</a> 的方式連線至 VM 。</p><p><strong>Public</strong> Key：存放 Server 端</p><p><strong>Private</strong> Key：存放 Client 端，用來解密 Server 的 Public Key。</p><ol><li>新增 Public key 到 GCE</li></ol><ul><li>打開終端機 輸入ssh-keygen -t rsa -C &quot;你的信箱&quot;</li><li>cat ~/.ssh/id_rsa.pub 複製剛剛新增的 Public key</li></ul><p>2. 新增 Public key 到 GCE 上的「中繼資料」-「安全殼層金鑰」</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UTxOMmjG2xJBSI3pRsAQAA.png" /></figure><ul><li>在 Public key 最後手動加上你的信箱 (如下圖)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PmfCOArEh1vwLGh1.png" /></figure><p>3. 打開終端機測試連線至 VM</p><p>ssh &lt;user&gt;@&lt;ip&gt;</p><ul><li>user: 信箱的帳號，假如信箱是 peerone@gmail.com， user 就會是 peerone</li><li>ip: 即 VM 的對外 IP ，可以到 「VM執行個體」的 「外部 IP」取得</li></ul><p>4. 最後看到類似的畫面就代表登入成功了</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/831/1*K2E6T_aKScSFepT2twDOqA.png" /></figure><p><strong>補充</strong>: 如果有使用 VSCode 的話，推薦使用 Remote — SSH 來連線</p><h4>安裝 Node.js、PM2、Git、Nginx</h4><ol><li>安裝 Node.js 之前需要先裝 nvm (方便管理 Node.js)</li></ol><p>安裝 <strong>nvm:</strong> wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash</p><p>安裝完後需要重新連線至 VM 並輸入 nvm - -version 來確認有安裝成功。</p><p>2. 安裝 node.js nvm install 10.16.3 (<strong>可自行選擇版本號</strong>)</p><p>3. 安裝 yarn</p><ul><li>curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -</li><li>echo &quot;deb https://dl.yarnpkg.com/debian/ stable main&quot; | sudo tee /etc/apt/sources.list.d/yarn.list</li><li>sudo apt update</li><li>sudo apt install yarn</li></ul><p>4. 安裝 PM2 yarn global add pm2</p><p>5. 安裝 git</p><ul><li>sudo apt update</li><li>sudo apt install git</li></ul><p>6. 安裝 nginx</p><ul><li>sudo apt update</li><li>sudo apt install nginx</li></ul><p>7. 最後因為 CI/CD 跑指令時，可能會出現找不到 PM2、Nginx、npm 的錯誤訊息所以我們要先把 PM2、Nginx、npm 加到全域上。(也可以之後再做此步驟)</p><pre>把 PM2 Node npm 加到全域<br>sudo ln -s /home/username/.nvm/versions/node/v10.16.3/bin/node /usr/local/bin/node<br>sudo ln -s /home/username/.nvm/versions/node/v10.16.3/bin/npm /usr/local/bin/npm<br>sudo ln -s /home/username/.nvm/versions/node/v10.16.3/bin/pm2 /usr/bin/pm2</pre><h4>結語</h4><p>這次我們建立好 VM 並安裝必要的軟體，下一篇將會介紹如何把程式下載到 VM 上並使用 PM2 讓 Nuxt 網站可以跑起來以及 Nginx 和 Https 的配置。整個網站建置介紹完後也會教讀者使用 Bitbucket Pipeline &amp; Circle Ci 處理 CI/CD，有興趣的讀者可以持續關注，如有任何問題也歡迎留言喔！</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55059a6e50b4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/nuxt-%E7%B6%B2%E7%AB%99%E5%BB%BA%E7%BD%AE%E8%88%87%E4%BD%88%E7%BD%B2-part-1-55059a6e50b4">Nuxt - 網站建置與佈署 Part 1</a> was originally published in <a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80">PeerOne Technology 皮偶玩互動科技</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue 3 狀態管理其它方案！跟 Vuex 說再見 ?]]></title>
            <link>https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/vue-3-%E7%8B%80%E6%85%8B%E7%AE%A1%E7%90%86%E5%85%B6%E5%AE%83%E6%96%B9%E6%A1%88-%E8%B7%9F-vuex-%E8%AA%AA%E5%86%8D%E8%A6%8B-8ad01f34f4ed?source=rss-16e410c6a165------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ad01f34f4ed</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[vuex]]></category>
            <category><![CDATA[state-management]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[PeerOne Front-End Director]]></dc:creator>
            <pubDate>Sun, 02 Aug 2020 12:01:01 GMT</pubDate>
            <atom:updated>2020-08-17T16:28:03.408Z</atom:updated>
            <content:encoded><![CDATA[<h3>Vue 3 狀態管理其它方案！跟 Vuex 說再見 ?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eMRO_nsVTwCs2IzynaQ-TQ.png" /></figure><p>上一篇「<a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/%E5%8D%87%E7%B4%9A-vue-3-%E5%89%8D-%E4%BD%A0%E5%BF%85%E9%A0%88%E8%A6%81%E7%9F%A5%E9%81%93%E7%9A%84%E6%94%B9%E5%8B%95-5891a297dbe2">升級 Vue 3 前，你必須要知道的改動</a>」提到 Vue 3 移除了 $on、$off 、 $once，所以Vue 2 EventBus 做法已經不能使用，雖然官方有提到替代方案 mitt ，但除了使用套件以外有沒有其他做法呢？</p><p>答案是有的！</p><p>這篇就要來教大家如何在 Vue 3 實作狀態管理，除了可以達到 EventBus 效果，「或許」也可以替代 Vuex。</p><h3>簡介狀態管理並實作</h3><p>以 Vuex 為例，狀態管理除了需要可以「全域共用」還需要符合下面幾個條件</p><h4><strong>1. 即時響應</strong></h4><p>當狀態改變時，所有組件都能即時更新對應的數據。而 Vue 3 提供了reactive<strong> </strong>方法，可以即時響應值的改動( 底層是用 Proxy 實作的 )。</p><p><strong>實作：</strong></p><ol><li>建立一個 store.js，並新增一個 function: createStore</li><li>新增 reactive 物件，相當於 Vuex 的 state</li></ol><p>我們利用 reactive 可以即時響應的特性完成了我們的第一步！</p><pre>import { reactive } from &#39;vue&#39;</pre><pre>export const createStore = () =&gt; {<br>  const state = reactive({<br>    age: 0,<br>    name: &#39;&#39;,<br>  })</pre><pre>  return {<br>    state<br>  }<br>}</pre><h4><strong>2. 不可直接修改</strong></h4><p>在 Vuex 若要修改狀態會需要用 commit mutations 確保狀態不會直接被修改。而在 Vue3 提供了 readonly 方法可以讓 reactive 物件變成唯讀狀態。</p><p><strong>實作：</strong></p><ol><li>修改 store.js，使用 readonly 確保 state 只能被讀取。</li></ol><pre>import { reactive, <strong>readonly</strong> } from &#39;vue&#39;</pre><pre>export const  createStore = () =&gt; {<br>  const state = reactive({<br>    age: 0,<br>    name: &#39;&#39;,<br>  })</pre><pre>  return {<br>    state: <strong>readonly(</strong>state<strong>)</strong><br>  }<br>}</pre><p>直接修改 state 會發出警告訊息</p><pre>const { state } = createStore()</pre><pre>state.name = &#39;peerone&#39; // warning!</pre><p>2. 若要修改 state 我們可以這樣寫，fetchUserData<strong> </strong>相當於 Vuex 的 mutations 或 actions</p><pre>export const  createStore = () =&gt; {<br>  const state = reactive({<br>    age: 0,<br>    name: &#39;&#39;,<br>  })</pre><pre>  <strong>const fetchUserData = () =&gt; {<br>    return new Promise((resolve) =&gt; {<br>      setTimeout(_=&gt; {<br>        state.age = 18<br>        state.name = &#39;peerone&#39;<br>        resolve()<br>      }, 1000)<br>    })<br>  }</strong></pre><pre>  return {<br>    <strong>fetchUserData</strong>,<br>    state: readonly(state)<br>  }<br>}</pre><p>3. 可以搭配 computed，做到 Vuex getters 的效果</p><p>補充：Vuex getters 背後也是用 computed 做到緩存效果。</p><pre>const getName = computed(_=&gt; state.name)</pre><h4><strong>3. 全域可使用的 「命名」</strong></h4><p>Provide 和 Inject 其實在 Vue 2.x 就已經存在，但用的機會比較少。</p><p>我們用圖讓大家快速複習一下這兩個 function。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WItq0U23l0oC1E5BSGhpsA.png" /></figure><ul><li>在 <strong>A</strong> 使用 provide，<strong>B、C、D、E</strong> 可以 inject</li><li>在 <strong>B</strong> 使用 provide，<strong>D、E</strong> 可以 inject</li><li>在 <strong>C</strong> 使用 provide，<strong>沒有</strong>組件可以 inject</li><li>在 <strong>D</strong> 使用 provide，<strong>沒有</strong>組件可以 inject</li><li>在 <strong>E</strong> 使用 provide，<strong>沒有</strong>組件可以 inject</li></ul><p><strong>總結上述</strong></p><blockquote>只要確保「父」組件 provide「子」組件就可以使用 inject</blockquote><pre><strong>Ｂ組件提供 &#39;foo&#39;</strong></pre><pre>const Provider = {   <br>  <strong>provide</strong>: {     <br>    foo: &#39;bar&#39;   <br>  },   <br>}</pre><pre>...</pre><pre><strong>Ｄ和Ｅ组件注入 &#39;foo&#39;</strong></pre><pre>const Child = {   <br>  <strong>inject</strong>: [&#39;foo&#39;],<br>  created () {     <br>    console.log(this.foo) // =&gt; &quot;bar&quot;   <br>  }   <br>}</pre><p>Provide 可以使用 Symbol 作為 key 使用，確保 key 是唯一的，但要怎麼當作「全域的命名」來使用呢？</p><p><strong>實作：</strong></p><p>首先在 store.js 中 export 一個 Symbol 物件</p><pre>export const state = Symbol(&#39;state&#39;)</pre><p>因上述原理我們在 main.js 使用 state 作為 provide 的 key，這樣子 App.vue 底下的組件都可以 inject 到 state</p><pre>import { createApp } from &#39;vue&#39;</pre><pre>import App from &#39;./App.vue&#39;</pre><pre>import { <strong>state</strong>, createStore } from &#39;./store&#39; </pre><pre>createApp(App)<br>  <strong>.provide(state, </strong>createStore()<strong>)</strong><br>  .mount(&#39;#app&#39;)</pre><p>而 Inject 的部分我們需要在 store.js export 另一個 function</p><pre>import { reactive, readonly, <strong>inject</strong> } from &#39;vue&#39;</pre><pre>export const state = Symbol(&#39;state&#39;)</pre><pre><strong>export const useState = () =&gt; inject(state)</strong></pre><p>接著在想要取用狀態的組件使用 useState 就可以了！</p><pre>&lt;template&gt;<br>  &lt;div&gt;{{ state }}&lt;/div&gt;<br>&lt;/template&gt;</pre><pre>&lt;script&gt;<br>import  { useState } from &#39;../store&#39;</pre><pre>export default {<br>  setup() {<br>    const { state } = useState()</pre><pre>    return {<br>      state<br>    }<br>  }<br>}<br>&lt;/script&gt;</pre><p><strong>store.js 完整程式碼</strong></p><pre>import { reactive, readonly, inject } from &#39;vue&#39;</pre><pre>export const state = Symbol(&#39;state&#39;)</pre><pre>export const useState = () =&gt; inject(state)</pre><pre>export const createStore = () =&gt; {<br>  const state = reactive({<br>    age: 0,<br>    name: &#39;&#39;,<br>  })</pre><pre>  const fetchUserData = () =&gt; {<br>    return new Promise((resolve) =&gt; {<br>      setTimeout(()=&gt;{<br>        state.token = 18<br>        state.name = &#39;peerone&#39;<br>        resolve()<br>      }, 2000)<br>    })<br>  }</pre><pre>  return {<br>    fetchUserData,<br>    state: readonly(state)<br>  }<br>}</pre><h3>驗證結果</h3><p>基本的 store 已經完成了 🎉，接下來我們來驗證一下能不能達到狀態管理的功能吧！(不要忘記在 main.js 使用 provide)</p><p>在 App.vue 增加兩個 component</p><pre>&lt;template&gt;<br>  &lt;div class=&quot;flex&quot;&gt;<br>    &lt;Action /&gt;<br>    &lt;Result /&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><pre>&lt;script&gt;<br>import Action from &#39;./components/Action.vue&#39;<br>import Result from &#39;./components/Result.vue&#39;</pre><pre>export default {<br>  name: &#39;App&#39;,<br>  components: {<br>    Action,<br>    Result<br>  }<br>}<br>&lt;/script&gt;</pre><p>Action.vue 和 Result.vue 都用了 useState</p><pre><strong>Action.vue 會更新 state</strong></pre><pre>&lt;template&gt;<br>  &lt;div class=&quot;container&quot;&gt;<br>    &lt;h5&gt;我是 Action.vue&lt;/h5&gt;<br>    &lt;button <a href="http://twitter.com/click">@click</a>=&quot;<strong>fetchUserData</strong>&quot;&gt;Fetch User Data&lt;/button&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><pre>&lt;script&gt;<br>import  { useState } from &#39;../store&#39;</pre><pre>export default {<br>  setup() {<br>    return {<br>      ...useState()<br>    }<br>  }<br>}<br>&lt;/script&gt;</pre><pre><strong>Result.vue 會顯示 state 更新後的結果</strong></pre><pre>&lt;template&gt;<br>  &lt;div class=&quot;container&quot;&gt;<br>    &lt;h5&gt;我是 Result.vue&lt;/h5&gt;<br>    &lt;div&gt;{{ <strong>state</strong> }}&lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><pre>&lt;script&gt;<br>import  { useState } from &#39;../store&#39;</pre><pre>export default {<br>  setup() {<br>    return {<br>      ...useState()<br>    }<br>  }<br>}<br>&lt;/script&gt;</pre><p>來看看執行的結果 🎉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/826/1*mb0WLOeKoWSutZsUGjR6lg.gif" /></figure><h3>結語</h3><p>以上做法真的可以取代 Vuex 嗎？筆者認為是<strong>否定</strong>的！</p><p>( 所以開頭用了「或許」😂 )</p><p>Vuex 還有以下優點：</p><ol><li>可以在 dev tools 檢視紀錄，方便 debug。</li><li>可以模組化程式 (module handling)，想像一下如果有多個 Provide 你會怎麼寫呢？</li><li>省去重新造輪子的時間。</li></ol><p>Provide &amp; Inject 做法和 Vuex 都是一種「解決方案」，就看團隊/個人怎麼選擇了！但要在 Vue3 用 EventBus 的話，Provide &amp; Inject 仍然是個不錯的選擇 。</p><p>以上如有問題歡迎在底下留言一起討論分享 😄</p><p>覺得文章有幫助到你的話，請不要客氣地幫我們 clap 👏。若想要持續關注更多文章可以加入我們的<a href="https://www.facebook.com/peerone1/">臉書</a>或訂閱 <a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80">Publication</a>， 兩個都能即時收到新文章通知喔！</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ad01f34f4ed" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/vue-3-%E7%8B%80%E6%85%8B%E7%AE%A1%E7%90%86%E5%85%B6%E5%AE%83%E6%96%B9%E6%A1%88-%E8%B7%9F-vuex-%E8%AA%AA%E5%86%8D%E8%A6%8B-8ad01f34f4ed">Vue 3 狀態管理其它方案！跟 Vuex 說再見 ?</a> was originally published in <a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80">PeerOne Technology 皮偶玩互動科技</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[升級 Vue 3 前，你必須要知道的改動 !]]></title>
            <link>https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/%E5%8D%87%E7%B4%9A-vue-3-%E5%89%8D-%E4%BD%A0%E5%BF%85%E9%A0%88%E8%A6%81%E7%9F%A5%E9%81%93%E7%9A%84%E6%94%B9%E5%8B%95-5891a297dbe2?source=rss-16e410c6a165------2</link>
            <guid isPermaLink="false">https://medium.com/p/5891a297dbe2</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[migration]]></category>
            <category><![CDATA[vue-3]]></category>
            <dc:creator><![CDATA[PeerOne Front-End Director]]></dc:creator>
            <pubDate>Mon, 20 Jul 2020 01:28:23 GMT</pubDate>
            <atom:updated>2021-02-24T15:38:19.793Z</atom:updated>
            <content:encoded><![CDATA[<h3>升級 Vue 3 前，你必須要知道的改動</h3><p>就在前幾天 Vue3 RC 版本已經發佈啦 (撒花)！距離正式版越來越近了 🎉🎉🎉！ (預計是8月)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4bFuJ1qh6Rv3HDoFUirvHw.png" /></figure><p>這篇主要是幫讀者從官方文件整理 Vue3 的新功能以及和 Vue2 的差異，讓讀者快速了解升級 Vue3 前需要注意什麼！</p><h3>新增的功能</h3><h4>1. Composition API</h4><p>相信大家最常聽到 Vue3 的新功能就是 <a href="https://v3.vuejs.org/guide/composition-api-introduction.html">Composition API</a> 了。</p><p>這邊建議的升級重點就是優先用 Composition API 寫法<strong>取代舊版 mixins</strong></p><p><strong>改用 Composition API 的好處：</strong></p><ul><li>相關邏輯可以集中，且更容易重複使用 (反觀 Vue2 的 mixins)</li><li>不用因為莫名出現的變數或方法名稱找老半天 (結果在 mixins)</li><li>減少 this 指向的問題</li></ul><p>而 Vue 3 也相容部分舊的寫法，所以可以視專案情況再陸續修改完整！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*-T0tfW4wqHhPUC2dGrV06w.png" /><figcaption>用 Vue3 這樣寫可以相容，也可以取得 setup 的東西</figcaption></figure><h4>2. useCssVars</h4><p>可以在目前的組件加上自訂的 css 變數 var()</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/906/1*7XiCaQmchM4FkBbMFotYYw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9BmSCByjptyvLmide3rwpQ.png" /></figure><p>也可以動態的改變 var 值，我們改寫一下原本的 code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dY7ICj8dFzDojBkHwbPtGQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*U3NLDFp-3iqOm5AoOVpOUw.gif" /></figure><p>⚠️ 建議外面加上一層根元素，因為使用 useCssVars 會在根元素上加上 style，若不加根元素會在每個元素都加上 style 喔！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Dwf1vCoafraEOrS-m4-Fw.png" /></figure><h4>3. Teleport</h4><p>Teleport 可以將定義的內容轉移到目標元素。</p><p>以往我們在做 彈框、對話框、通知等，需要在每個頁面加入組件或是用 vuex 來控制顯示與否。Teleport 可以幫我們簡化這些動作並提高開發體驗！</p><p><strong>🚀 使用場景範例</strong></p><p>我們以 Modal 為例，先建立一個 Modal 組件，<strong>並給予 id !</strong></p><pre><strong>Modal.vue</strong></pre><pre>&lt;template&gt;<br>  &lt;div id=&quot;modal-wrapper&quot;&gt;&lt;/div&gt;<br>&lt;/template&gt;</pre><p>我們把這個 Modal 引入 App.vue</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*A6gR9t_XjlPKrvhZ3aVDPg.png" /></figure><p>加入打開彈窗的邏輯，teleport 需要在 to 參數指定元素選擇器</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MtPrijry2n1cF8JiB6JyGA.png" /></figure><p>以上這樣就完成了，teleport 中的內容都會被渲染在目標元素裡頭 🎉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*8pAUsXRBwgY5pKmMZayOLw.png" /><figcaption>渲染後的 HTML</figcaption></figure><p>當然也可以同時指定多個 teleport 到同一個元素上。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WbNCRhnqnXZG84mcT97Jzg.png" /></figure><p>渲染結果</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*ZgSG-pGgpTYWNV-IMVxMXg.png" /><figcaption>會自動疊加</figcaption></figure><h4>4. Fragments</h4><p>在 Vue 2 中在每個模板我們都要加上根元素，會多了許多不必要的元素在 HTML 裡 😅</p><pre>&lt;!-- Layout.vue --&gt;<br>&lt;template&gt;<br>  <strong>&lt;div&gt;  --&gt; 根元素</strong><br>    &lt;li&gt;...&lt;/li&gt;<br>    &lt;li&gt;...&lt;/li&gt;<br>    &lt;li&gt;...&lt;/li&gt;<br>  <strong>&lt;/div&gt;</strong><br>&lt;/template&gt;</pre><p>而在 Vue 3 終於可以省略根元素了，這對於 list 類型的組件非常方便 🎉</p><pre>&lt;!-- Layout.vue --&gt;<br>&lt;template&gt;<br>  &lt;li&gt;...&lt;/li&gt;<br>  &lt;li&gt;...&lt;/li&gt;<br>  &lt;li&gt;...&lt;/li&gt;<br>&lt;/template&gt;</pre><h4>5. Emits Component Option</h4><p>Vue 3 提供 emits<strong> </strong>的聲明，用法就像 props 一樣。</p><pre>&lt;template&gt;...&lt;/template&gt; <br>&lt;script&gt; <br>export default {   <br>  emits: [ &quot;myEvent&quot;, &quot;click&quot; ] <br>} <br>&lt;/script&gt;</pre><p>這樣寫的話可以快速知道這個組件含有什麼 emit 事件，一目瞭然。</p><p>🚀 Emits 也可以進行驗證，必須為 <strong>function</strong> 並返回 <strong>布林值</strong></p><pre>app.component(&#39;custom-form&#39;, {<br>  emits: {<br>    // 不會被驗證<br>    click: null,<br><br>    // 驗證 submit 事件<br>    submit: ({ email, password }) =&gt; {<br>      if (email &amp;&amp; password) {<br>        return true<br>      } else {<br>        console.warn(&#39;Invalid submit event payload!&#39;)<br>        return false<br>      }<br>    }<br>  },<br>  methods: {<br>    submitForm() {<br>      this.$emit(&#39;submit&#39;, { email, password })<br>    }<br>  }<br>})</pre><p>⚠️ 注意</p><p>假如自訂的事件名稱為 camelCased 命名方式</p><pre>this.$emit(&#39;customEvent&#39;)</pre><p>必須這樣寫才有效果！</p><pre>&lt;!-- <strong>Won&#39;t work</strong> --&gt;<br>&lt;my-component v-on:<strong>custom-event</strong>=&quot;doSomething&quot;&gt;&lt;/my-component&gt;</pre><pre>&lt;!-- <strong>Ｗork</strong> --&gt;<br>&lt;my-component v-on:<strong>customevent</strong>=&quot;doSomething&quot;&gt;&lt;/my-component&gt;</pre><p>所以官方建議直接一律使用 kebab-case 來命名，避免浪費時間處理這種命名問題。</p><blockquote>For these reasons, we recommend you <strong>always use kebab-case for event names</strong>.</blockquote><pre>this.$emit(&#39;custom-event&#39;)</pre><pre>&lt;my-component v-on:<strong>custom-event</strong>=&quot;doSomething&quot;&gt;&lt;/my-component&gt;</pre><h4>6. Suspense &amp; Error Boundary</h4><p>Suspense 是非同步載入的組件，在資料讀取完成以及渲染完成之前，會先顯示 <strong>#fallback</strong> 的內容。</p><p>有了 Suspense 可以不用特地加一個 loading 變數來處理加載資料時的判斷。</p><pre>&lt;!-- <strong>傳統做法</strong> --&gt;</pre><pre>&lt;div v-if=&quot;loading&quot;&gt; Loading ...&lt;/div&gt;<br>&lt;div v-else&gt; {{ data }} &lt;/div&gt;</pre><pre>&lt;!-- <strong>使用 suspense</strong> --&gt;</pre><pre>&lt;Suspense&gt;   <br>  &lt;template #default&gt;{{ data }}&lt;/template&gt;   <br>  &lt;template #fallback&gt; Loading... &lt;/template&gt; <br>&lt;/Suspense&gt;</pre><pre><strong>⚠️</strong> <strong>#default #fallback 其實是 v-slot 的簡寫</strong></pre><p>也可以搭配 <strong>onErrorCaptured </strong>來做錯誤的處理，以下為例，假如 <strong>Suspended-component </strong>call api 發生錯誤，onErrorCaptured<strong> </strong>方法就會捕捉到錯誤，並在畫面顯示錯誤訊息。</p><pre>&lt;template&gt;<br>  &lt;div v-if=&quot;error&quot;&gt;<br>    {{ error }}<br>  &lt;/div&gt;</pre><pre>  &lt;Suspense v-else&gt;<br>    &lt;template #default&gt;<br>      &lt;Suspended-component /&gt;<br>    &lt;/template&gt;<br>    &lt;template #fallback&gt;<br>      &lt;div&gt;Loading..&lt;/div&gt;<br>    &lt;/template&gt;<br>  &lt;/Suspense&gt;<br>&lt;/template&gt;</pre><pre>&lt;script&gt;<br>import { onErrorCaptured, ref } from &#39;vue&#39;</pre><pre>export default {<br>  setup() {<br>    const error = ref(null)<br>    <strong>onErrorCaptured</strong>(e =&gt; {<br>      error.value = e<br>      return true<br>    })<br>    return { error }<br>  }<br>}<br>&lt;/script&gt;</pre><p>除了以上用法，也可以搭配 Vue Router 使用 😎</p><pre>&lt;Suspense&gt;   <br>  &lt;template #default&gt;     <br>    &lt;router-view /&gt;   <br>  &lt;/template&gt;   <br>  &lt;template #fallback&gt;     <br>    &lt;span&gt;畫面讀取中...&lt;/span&gt;   <br>  &lt;/template&gt; <br>&lt;/Suspense&gt;</pre><h4>7. ::v-slotted &amp; ::v-global</h4><p>🚀 <strong>::v-slotted 使用情境</strong></p><p>一般我們都會在組件的 style 加上 scoped，就是為了要避免被其它頁面 style影響。</p><p>但假如我想要在 slot 中如果傳入 p元素，並將字體改為斜體，我們會這樣寫…</p><pre><strong>Modal.vue</strong></pre><pre>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;slot&gt;&lt;/slot&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><pre>&lt;style scoped&gt;<br>p {<br>  font-style: italic;<br>}<br>&lt;/style&gt;</pre><pre><strong>App.vue</strong></pre><pre>&lt;template&gt;<br>  &lt;Modal&gt;<br>    &lt;p&gt;我要斜體字&lt;/p&gt;<br>  &lt;/Modal&gt;<br>&lt;/template&gt;</pre><p>但你會發現<strong>沒變斜體</strong>，因為 slot 的內容是<strong>屬於父組</strong>件的！這時候我們可以使用 ::v-slotted 來達到效果！</p><pre>&lt;template&gt;<br>  &lt;div id=&quot;modal-wrapper&quot;&gt;<br>    &lt;slot&gt;&lt;/slot&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><pre>&lt;style scoped&gt;<br><strong>::v-slotted(p) {<br>  </strong>font-style: italic;<strong><br>}</strong><br>&lt;/style&gt;</pre><p>🚀 <strong>::v-global</strong></p><p>可以在 &lt;style scoped&gt; 宣告一個全域的樣式。</p><pre>&lt;style <strong>scoped</strong>&gt;<br><strong>::v-global(p) {<br>  </strong>font-style: italic;<strong><br>}</strong><br>&lt;/style&gt;</pre><pre><strong>同等於</strong></pre><pre>&lt;style&gt;<br>p {<br>  font-style: italic;<br>}<br>&lt;/style&gt;</pre><h3>棄用的功能</h3><h4>1. KeyCode</h4><p>Vue 3 將不支援 KeyCode 寫法</p><pre>&lt;!-- keyCode version --&gt;<br>&lt;input v-on:keyup.13=&quot;submit&quot; /&gt;<br><br>&lt;!-- alias version --&gt;<br>&lt;input v-on:keyup.enter=&quot;submit&quot; /&gt;</pre><h4>2. $on, $off $once</h4><p>移除了$on, $off 以及 $once 也就代表<strong>不能使用</strong> <strong>EventBus</strong> 😂。</p><p>但作者尤雨溪大大有提到，未來相容版本還是可以使用 (可能是 2.7 版)，或是改用 <a href="https://github.com/developit/mitt">mitt</a> 這類的套件</p><blockquote>In Vue 2, the most common usage of the event emitter API is using an empty Vue instance as an event hub. This can be replaced by using an external library implementing the event emitter interface, for example <strong>mitt</strong>.</blockquote><blockquote>These methods can also be <strong>supported in compatibility builds</strong>.</blockquote><h4><strong>3. Inline Template Attribute</strong></h4><p>這個比較少用，所以影響不大，<a href="https://github.com/vuejs/rfcs/blob/master/active-rfcs/0016-remove-inline-templates.md">官方的建議</a>是用 v-slot 作法來取代。</p><pre>&lt;!-- before --&gt;<br>&lt;my-comp inline-template :msg=&quot;parentMsg&quot;&gt;<br>  {{ msg }} {{ childState }}<br>&lt;/my-comp&gt;<br><br>&lt;!-- after --&gt;<br>&lt;my-comp v-slot=&quot;{ childState }&quot;&gt;<br>  {{ parentMsg }} {{ childState }}<br>&lt;/my-comp&gt;</pre><pre>&lt;!-- <strong>my-comp</strong> --&gt;<br>&lt;slot :childState=&quot;childState&quot; /&gt;</pre><h4>4. Filters</h4><p>其實 Filters 移除也蠻合理的，因為可以直接用 methods 或是 computed 來取代。</p><h4>5. $listeners</h4><p>在 Vue3 中 $listeners 已經整合到 $attrs</p><pre>Vue2: <br>&lt;input type=&quot;text&quot; v-bind=&quot;$attrs&quot; v-on=&quot;$listeners&quot; /&gt;</pre><pre>Vue3:<br>&lt;input type=&quot;text&quot; v-bind=&quot;$attrs&quot; /&gt;</pre><h3>變更的功能</h3><h4>1. 修飾符 .sync 移除，v-model 可以多個使用</h4><p>Vue 3 移除了 .sync 且合併到 v-model 裡頭，也因為這樣的關係可以同時使用多個 v-model 了！</p><pre>&lt;!-- Vue 2 --&gt; <br>&lt;CustomComponent <strong>v-bind:style.sync</strong>=&quot;style&quot; /&gt;</pre><pre>&lt;!-- Vue 3 --&gt; <br>&lt;CustomComponent <strong>v-model:style</strong>=&quot;style&quot; /&gt;</pre><p>多個 v-model 相當於以前的 .sync</p><pre>&lt;Form   <br> v-model:name=&quot;name&quot;   <br> v-model:email=&quot;email&quot; <br>/&gt;</pre><p>🚀 組件實作 v-model 的寫法也有些許改變 (真的就是與 .sync 的綜合寫法)</p><ul><li>props 的 <strong>value</strong> 改為 <strong>modelValue</strong></li><li>$emit <strong>input</strong> 改為 <strong>update:modelValue</strong></li></ul><pre>&lt;CustomComponent v-model=&quot;style&quot; /&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4N52i1KB-5TQPHfvIF7qQg.png" /></figure><h4>2. ::v-deep</h4><p>⚠️ 為了將樣式穿透至子元素，像是要改寫 element-ui 樣式常用到 ::v-deep，但 Vue 3 寫法稍有不同。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Kc1pR9m3Sgcz_PK-XmNhA.png" /></figure><h4>3. Directive</h4><p>主要是針對<strong>生命週期</strong>的名稱重新命名，確保名稱一致。</p><ul><li>⚠️ bind 改為 beforeMount</li><li>⚠️ inserted 改為 mounted</li><li>🚀 新增 beforeUpdate: 元素更新前執行</li><li>🚀 新增 beforeUnmount: 元素卸載前執行</li><li>⚠️ unbind 改為 unmounted</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*54eS1jnQ4565rSyDEeEwqg.png" /></figure><h4>4. 非同步組件</h4><p>🚀 在 Vue 2 建立非同步組件寫法是定義一個 function ，Vue 3 改為使用 defineAsyncComponent</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w6jdQhnxvCL4PownfMgivg.png" /></figure><h4>5. transition-class</h4><p>🚀 v-enter改為v-enter-from</p><p>🚀 v-leave改為 v-leave-from</p><pre>.v-enter-from, .v-leave-to { opacity: 0; } <br>.v-leave-from, .v-enter-to { opacity: 1 }</pre><h4>6. 全局 api</h4><p>全局 api 改動差異不大</p><p>🚀 新增 createApp</p><p>⚠️ 移除 Vue.config.productionTip</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JRlP76CxihqmR5GXLJmCwQ.png" /></figure><h3>Vue Router &amp; Vuex</h3><p>假如有用 <a href="https://github.com/vuejs/vue-router-next">Vue Router</a> 或 <a href="https://github.com/vuejs/vuex/tree/4.0#vuex-4">Vuex</a> 的話這兩個一定要升級新版本，目前皆還在 Beta 階段，但目前觀察已經不會有大規模改動了，還是可以先進行升級。</p><p><strong>Vue Router 新舊版差異</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ukrbia73RMCC2wwtpu4TeA.png" /></figure><p><strong>Vuex 新舊版差異</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fJCazo33s07A1FMkke3L3A.png" /></figure><p>其實 Vue Router 和 Vuex 的改動並不會差太多，這部分相對容易許多。</p><h3>檢測工具</h3><p>官方之後也會提供檢測工具 <a href="https://github.com/vuejs/vue-migration-helper">vue-migration-helper</a> 幫助工程師瞭解專案還有哪邊需要進行修改，也可以搭配使用。(目前 Vue 3 版本還在開發中)</p><h3>結語</h3><p>以上就是我們整理出需要注意的改動，詳細可以到官方文件查看。相信大家對於 Vue 3 也是迫不及待想要使用了，若要升級勢必免不了大量的重構，但相信長遠來看還是值得的，希望這篇可以幫助大家開心「玩」 Vue 3 🤣！</p><h3>參考</h3><ul><li>官方文件 <a href="https://v3.vuejs.org/">https://v3.vuejs.org/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5891a297dbe2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/%E5%8D%87%E7%B4%9A-vue-3-%E5%89%8D-%E4%BD%A0%E5%BF%85%E9%A0%88%E8%A6%81%E7%9F%A5%E9%81%93%E7%9A%84%E6%94%B9%E5%8B%95-5891a297dbe2">升級 Vue 3 前，你必須要知道的改動 !</a> was originally published in <a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80">PeerOne Technology 皮偶玩互動科技</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flutter — 使用 Lottie 製作 Splash Page]]></title>
            <link>https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/flutter-%E4%BD%BF%E7%94%A8-lottie-%E8%A3%BD%E4%BD%9C-splash-page-2841e43fe1ce?source=rss-16e410c6a165------2</link>
            <guid isPermaLink="false">https://medium.com/p/2841e43fe1ce</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[splash-screen]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[lottie]]></category>
            <dc:creator><![CDATA[PeerOne Front-End Director]]></dc:creator>
            <pubDate>Sun, 28 Jun 2020 15:14:14 GMT</pubDate>
            <atom:updated>2020-07-01T18:46:06.990Z</atom:updated>
            <content:encoded><![CDATA[<h3>Flutter — 使用 Lottie 製作 Splash Page</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C_q8yN73FGZW6rCePpYt1Q.png" /></figure><p>上一篇我們在 <a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/%E5%89%8D%E7%AB%AF%E5%8B%95%E7%95%AB%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%A1%88-lottie-15f6e19c3fca"><strong>前端動畫解決方案 — Lottie</strong></a> 介紹了在網頁使用 Lottie 製作動畫效果，這次要分享在 Flutter 使用 Lottie 製作一個 Splash Page</p><h4>安裝及配置</h4><p>首先我們要先安裝 Lottie</p><p>打開 <strong>pubspec.yaml </strong>在 dependencies 增加 lottie</p><pre>dependencies:<br>  lottie: ^0.4.0+1</pre><p><strong>執行 </strong>flutter pub get 安裝套件</p><p>前一篇我們有提到會使用 AE 導出的 JSON 檔案，在 flutter 中需要設定 JSON 檔案的位置，一樣在 <strong>pubspec.yaml </strong>增加資源路徑即可，並將 JSON 檔案放在指定路徑。</p><pre>flutter:<br>  assets:<br>    - assets/lottie/ </pre><h4>設定起始頁及路由</h4><p>在 main.dark 引入 home.dart 以及 splash.dart</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-BKZXgwAn47w3YfIIH2tug.png" /></figure><p><strong>MaterialApp</strong>：</p><ul><li><strong>home</strong> : App 進入後的第一個頁面，splash 做為打開 App 後先看到的載入頁面。</li><li><strong>routes</strong> : 動畫播放完畢需要跳轉到真正的首頁 home，所以在路由這邊設定首頁的路徑 ‘/home’</li></ul><h4>實作</h4><p>設定好路由和首頁後，接著我們開始來實作 Splash page</p><p>1. <strong>建立 StatefulWidge</strong>t</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TmeNiCYnnamWK4Php3okwA.png" /></figure><p><strong>2. AnimationController</strong></p><p>AnimationController 是 Flutter 中控制動畫的類別，包含的方法有 <strong>forward</strong>、<strong>stop</strong>、<strong>reverse。</strong>需要定義一個 AnimationController 來播放 AE 動畫。</p><p>在 initState 建立 一個 AnimationController</p><pre>_controller =<br>    AnimationController(vsync: this, duration: Duration(seconds: 4))</pre><p><strong>duration</strong>: 動畫持續時間。</p><p><strong>vsync</strong> : vsync 會綁定一個動畫計時器到 widget ，當這個 widget 不再顯示時，動畫會被暫停，一直到 widget 再顯示才會播放動畫，這樣可以避免消耗不必要的資源。</p><p>⚠️ 使用 <strong>vsync 要先繼承 SingleTickerProviderStateMixin</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eQnE5iJhGt72OV5MvsP6_A.png" /><figcaption>不要忘記在dispose銷毀 _controller，釋放資源</figcaption></figure><p>⚠️ 注意 AnimationController 被建立好後還不會馬上播放動畫，後面會使用 Lottie 來觸發動畫播放。</p><p><strong>3. 建立 Lottie StatelessWidget</strong></p><p>在 splash.dart 引入 lottie ，並新增一個 StatelessWidget</p><pre>import &#39;package:lottie/lottie.dart&#39;;</pre><p>使用 Lottie 很簡單 ，就和使用圖片差不多</p><pre>Lottie.asset(&#39;your path&#39;)</pre><p>但我們需要更多的設定，下面介紹幾個常用的參數</p><p><strong>composition</strong>: 動畫檔案 (必傳)</p><p><strong>repeat</strong>: bool 是否重複播放動畫</p><p><strong>reverse</strong>: bool 逆向播放動畫</p><p><strong>controller</strong>: 對應的 AnimationController</p><p><strong>width, height</strong>: double 動畫長寬</p><p><strong>fit</strong>: BoxFit 圖片填充方式</p><ul><li>BoxFit.fill，填滿</li><li>BoxFit.contain，原大小</li><li>BoxFit.cover，延伸填滿</li><li>BoxFit.fitWidth，填滿寬度</li><li>BoxFit.fitHeight，填滿高度</li><li>BoxFit.none，不做變化</li></ul><p><strong>alignment</strong>: Alignment 對齊方式</p><p><strong>onLoaded</strong>: Function 動畫載入後觸發的方法</p><p><strong>frameBuilder</strong>: 可以替動畫加上一些效果，像是放大縮小、淡化等</p><p>controller 的部分我們需要引用前面的 AnimationController 來播放動畫，所以用 <strong>findAncestorStateOfType </strong>拿到父級別的 state ， state._controller 就是先前我們先創建好的 AnimationController 。</p><p>在 Lottie 觸發 onLoaded 的時候播放動畫，所以用了 forward 方法。</p><p>而 onLoaded 會回傳參數 composition 可以拿到動畫的總時間，藉此綁在 controller 上，所以上面的 controller 的 duration 就可以拿掉了，除非你需要自訂動畫的持續時間再使用即可。</p><pre><strong>＊initState 的 _controller 拿掉 duration</strong><br>_controller = AnimationController(vsync: this)</pre><pre><strong>＊onLoaded 綁定 duration</strong><br>state._controller.duration = composition.duration<br>state._controller.forward();</pre><p><strong>補充</strong>：dart 提供鏈結式的寫法，程式碼會更簡潔一點 😆</p><pre><strong>onLoaded 可以改成這樣寫</strong></pre><pre>state._controller<br>  <strong>..</strong>duration = composition.duration<br>  <strong>..</strong>forward();</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fPKWalyFab0I2rL7-6a-JA.png" /><figcaption>Lottie 完整程式碼</figcaption></figure><p>接著在 _SplashPageState Widget build 回傳 LottieAnimate() 就可以了！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*f5vUtAOxxXR4EdLmItEIRw.png" /></figure><p><strong>4. 跳轉到真正的首頁</strong></p><p>最後要讓動畫播放完畢後 push 到 ‘/home’，這邊也很簡單，只要在 AnimationController 監聽動畫的播放狀態再 Navigator push 即可。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XbfZhLw1z3IY8XJ_LqoDrQ.png" /></figure><p>以上 Splash page 的實作就完成了！來看看實際的畫面 🎉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/386/1*kIFDdypu84QuhmuoMVxdfw.gif" /><figcaption>file from <a href="https://lottiefiles.com/19902-splash-screen">https://lottiefiles.com/19902-splash-screen</a></figcaption></figure><p>附上 splash 完整的程式碼</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yJD5tVo_jai6XI0Hoq0gpQ.png" /></figure><h4>結語</h4><p>Lottie 的網頁和 Flutter上的應用介紹結束了，這種做法設計師也需要學會 AE 等工具才可行，如果是個人需求或是沒辦法使用 AE 的話不妨也可以在 <a href="https://lottiefiles.com/marketplace">lottiefiles</a> 上面找到一些付費/免費的資源喔！🎉</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2841e43fe1ce" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/flutter-%E4%BD%BF%E7%94%A8-lottie-%E8%A3%BD%E4%BD%9C-splash-page-2841e43fe1ce">Flutter — 使用 Lottie 製作 Splash Page</a> was originally published in <a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80">PeerOne Technology 皮偶玩互動科技</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[前端動畫解決方案 - Lottie]]></title>
            <link>https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/%E5%89%8D%E7%AB%AF%E5%8B%95%E7%95%AB%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%A1%88-lottie-15f6e19c3fca?source=rss-16e410c6a165------2</link>
            <guid isPermaLink="false">https://medium.com/p/15f6e19c3fca</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[lottie]]></category>
            <dc:creator><![CDATA[PeerOne Front-End Director]]></dc:creator>
            <pubDate>Sun, 07 Jun 2020 12:01:01 GMT</pubDate>
            <atom:updated>2020-06-19T18:00:15.522Z</atom:updated>
            <content:encoded><![CDATA[<h4>工程師也能輕鬆實現 AE 動效</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Pi8xra1ZFXbSjQzKdVs52A.gif" /></figure><p>為了引導用戶了解產品內容，<strong>動畫</strong>是不可或缺的工具，讓產品更有活力也能吸引用戶目光。除了動畫外還有很多方式可以加強用戶第一印象，如果想瞭解更多可以參考 👉 <a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/%E7%B6%B2%E9%A0%81%E8%A8%AD%E8%A8%88-%E7%B6%B2%E7%AB%99%E7%9A%84%E7%AC%AC%E4%B8%80%E5%8D%B0%E8%B1%A1-601ae8043634"><strong>網頁設計-網站的第一印象</strong></a><strong>。</strong></p><p>下面我們會先探討前端實現動畫常用的幾種方式</p><p># 分析優缺點</p><p># 為什麼用 Lottie</p><p>最後我們會帶讀者用 Vue 來實做簡單的例子，更能體會 Lottie 帶來的方便性。</p><h4>製作動畫的方式</h4><ol><li><strong>CSS Sprite</strong></li></ol><p>原理是將需要用到的圖片合併成一張大圖也就是俗稱的雪碧圖，再利用background-position 來定位各個圖片位置。</p><p>以下圖為例子，從左至右偏移就會變的像在走路一樣。</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fimgur.com%2FBu9JKiB%2Fembed%3Fpub%3Dtrue%26ref%3Dhttps%253A%252F%252Fembed.ly%26w%3D900&amp;display_name=Imgur&amp;url=https%3A%2F%2Fimgur.com%2FBu9JKiB&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=imgur" width="900" height="303" frameborder="0" scrolling="no"><a href="https://medium.com/media/8117d5e8d6eb60451bece3e76dfe17d4/href">https://medium.com/media/8117d5e8d6eb60451bece3e76dfe17d4/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Frunkids%2Fembed%2Fpreview%2FyjZdBj%3Fheight%3D600%26slug-hash%3DyjZdBj%26default-tabs%3Dcss%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Frunkids%2Fdetails%2FyjZdBj&amp;image=https%3A%2F%2Fassets.codepen.io%2F1263506%2Finternal%2Fscreenshots%2Fpens%2FyjZdBj.default.png%3Fformat%3Dauto%26amp%3Bheight%3D400%26amp%3Bquality%3D75%26amp%3Bv%3D1%26amp%3Bversion%3D1591021484%26amp%3Bwidth%3D700&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/867c1ba81b39f55e5fbc65b0fd1c292e/href">https://medium.com/media/867c1ba81b39f55e5fbc65b0fd1c292e/href</a></iframe><p>優點：因為只有一張圖，所以可以減少網路請求數並加快網頁載入的速度</p><p>缺點：這種類影格動畫的作法局限性比較大，圖檔雖然只有一張但可能相對檔案變大了，另外就是在不同解析度下可能會失真。</p><p>2. <strong>Gif</strong></p><p>使用 Gif 是最簡單的，只要一張圖就可以搞定，也不用特別寫 CSS，但同樣會有失真的問題。</p><p>3. <strong>SVG</strong></p><p>SVG 解決了失真的問題，但對於初學者要使用 SVG 製作動畫還是有一定的難度，不要太複雜的話應該還是可以慢慢刻出來。</p><p>大部分的動畫都可以用上面三種方式達成，但假如有需求要實作下面的效果且又不會失真，我想工程師心都涼了一半了吧！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*j_xsfE7t9qUnBsL8EdmGWg.gif" /><figcaption>Gif from <a href="https://lottiefiles.com/23017-chat-platform-animation#">https://lottiefiles.com/23017-chat-platform-animation#</a></figcaption></figure><p>工程師:「恩…那個…請問是認真的嗎 😓？」</p><p>設計師 :「是的，很認真 🙆‍♀️」</p><h4>使用 Lottie</h4><p>Lottie 是由 Airbnb 開源的套件，可以將 AE 動畫套用在 Web 及 App 上。</p><p>設計師用 AE 把動畫效果做出來，再用 Bodymovin 導出 JSON 檔案給工程師，而工程師只要拿 JSON 檔使用 Lottie 就可做出動畫了。</p><p>設計師和工程師終於不用糾結理想與技術面的問題了。</p><h4>用 Vue 實作</h4><p>這邊開始帶大家用 Vue 來實作</p><p>首先需要先安裝 Lottie，並準備好設計師給的 JSON 檔</p><pre>yarn add lottie-web</pre><p><strong># 建立一個 Component</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*upDdtymNfiP73fEY8c_drA.png" /></figure><p><strong># 初始化 loadAnimation</strong></p><ul><li><strong>animationData </strong>: Import 的 JSON 檔</li><li><strong>path </strong>: JSON 檔案的相對路徑</li><li><strong>loop </strong>: 是否啟用循環播放 boolean / 循環次數 number</li><li><strong>autoplay </strong>: 是否自動播放</li><li><strong>renderer </strong>: 可以用SVG、Canvas、HTML</li><li><strong>container </strong>: 要顯示動畫的dom元素</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KYFr77HTxA24ec6AfGj8Dg.png" /></figure><pre><strong>🔥 注意 <br></strong>path 代表 JSON 檔案路徑，但我們範例是 import JSON 所以要用 animationData 才可以正常使用喔！</pre><p># 監聽動畫</p><p>Lottie 提供幾個事件讓我們可以在 Lottie 初始化或是結束時額外做一些處理</p><ul><li><strong>complete :</strong> 動畫播放完畢</li><li><strong>loopComplete : </strong>循環播放結束</li><li><strong>enterFrame :</strong> 開始播放時</li><li><strong>config_ready :</strong> 初始化配置完成</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*40mFrfwPcNCKM8HeKUVeYA.png" /></figure><p><strong># 控制動畫播放速度及進度</strong></p><p>Lottie 有基本的 <strong>play</strong>()、<strong>stop</strong>()、<strong>pause</strong>() 方法外，還提供了像是</p><p><strong>setSpeed：</strong>調整動畫速度</p><p><strong>goToAndStop：</strong>指定播放動畫區間</p><p><strong>totalFrames：</strong>進度百分比</p><p>等方法…這些都可以讓我們方便地在動畫上做額外的控制與操作。</p><p>👇 下方附上程式碼與完成的畫面，大家也可以實際點點看按鈕看看如何控制動畫。</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fcompassionate-tree-dl63j%3Fautoresize%3D1%26fontsize%3D14%26hidenavigation%3D1%26module%3D%252Fsrc%252Fcomponents%252FLottie.vue%26theme%3Ddark%26view%3Dpreview&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fcompassionate-tree-dl63j%3Fautoresize%3D1%26fontsize%3D14%26hidenavigation%3D1%26module%3D%252Fsrc%252Fcomponents%252FLottie.vue%26theme%3Ddark%26view%3Dpreview&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fdl63j%2Fscreenshot.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/68757c08a87c1a9d9bd60b8a0106c96f/href">https://medium.com/media/68757c08a87c1a9d9bd60b8a0106c96f/href</a></iframe><h4>結語</h4><p>相信看到這邊讀者多少有感受到 Lottie 的優點與便利性，不僅可以將設計師的動畫完整呈現，也大大提升工程師的生產力，但使用 Lottie 還是有幾點要注意一下</p><ol><li>AE 動畫須盡可能照規範設計，否則檔案會太大反而影響效能</li><li>官方文件不夠詳細，assetsPath 這個參數得要看原始碼才知道</li></ol><p>總結來說，注意設計和開發流程，Lottie 可以代替 Gif、CSS Sprite 來製作動畫，善用套件提供的方法控制播放流程，就可以在產品上展現生動的動畫了！</p><p>下一篇我們會教大家 Flutter 使用 Lottie 來製作 Splash Screen。請大家持續關注囉！</p><h4>參考</h4><ul><li><a href="https://github.com/airbnb/lottie-web">https://github.com/airbnb/lottie-web</a></li><li><a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/%E7%B6%B2%E9%A0%81%E8%A8%AD%E8%A8%88-%E7%B6%B2%E7%AB%99%E7%9A%84%E7%AC%AC%E4%B8%80%E5%8D%B0%E8%B1%A1-601ae8043634"><strong>網頁設計-網站的第一印象</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15f6e19c3fca" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/%E5%89%8D%E7%AB%AF%E5%8B%95%E7%95%AB%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%A1%88-lottie-15f6e19c3fca">前端動畫解決方案 - Lottie</a> was originally published in <a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80">PeerOne Technology 皮偶玩互動科技</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nuxt實作第三方登入(Google & Facebook & Line)]]></title>
            <link>https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/nuxt%E5%AF%A6%E4%BD%9C%E7%AC%AC%E4%B8%89%E6%96%B9%E7%99%BB%E5%85%A5-google-facebook-line-8132047ad349?source=rss-16e410c6a165------2</link>
            <guid isPermaLink="false">https://medium.com/p/8132047ad349</guid>
            <category><![CDATA[google-login]]></category>
            <category><![CDATA[line-login]]></category>
            <category><![CDATA[facebook-login]]></category>
            <category><![CDATA[nuxtjs]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[PeerOne Front-End Director]]></dc:creator>
            <pubDate>Sun, 17 May 2020 12:01:01 GMT</pubDate>
            <atom:updated>2020-06-05T03:30:33.740Z</atom:updated>
            <content:encoded><![CDATA[<h3>不使用 Nuxt Auth Module，實作第三方登入Google &amp; Facebook &amp; Line</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ClQ3URY7xzb6OqxA68C8wA.png" /></figure><p>網站透過第三方進行註冊和登入已經是各種網站服務的基本配備，第三方的綁定可以幫助用戶在註冊和登入時，省去繁瑣的帳密設定行為，增進用戶體驗流程的流暢性。此次文章將分享常見的第三方登入機制：Goolge、Facebook 以及 Line的透過Nuxt實作的過程。</p><h3>開發環境</h3><p>後端： Python / Django</p><p>前端： Vue / Nuxt</p><p>DeVops：GCP / Kubernetes</p><h3>開發前準備</h3><p>首先我們要先取得三個平台的Client Id，下面會簡單說明設定步驟</p><h4>Line:</h4><p>到<a href="https://developers.line.biz/console/">Line官方開發者後台</a>，並依照下列步驟完成前置設定</p><ol><li>選擇你的 provider 並建立一個 Login channel</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y59zpG_EivBwIu9OllWs_w.png" /></figure><p>2. 輸入完基本資料後，會看到下圖頁面，並到 Basic settings 查看資訊，前端主要會用到 Channel ID。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*nRExZwqqHVk1yrJ2ZPpsjg.png" /></figure><p>⚠️ 另外需要注意 Permissions 的部分目前只能取得使用者的 id , 頭貼及暱稱，若需要獲取使用者的 Email 則需要另外進行信箱的驗證。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*FXtGYI1z2IqfHeV_1sa7GQ.png" /></figure><p>⚠️ 在OpenID Connect 點選 Apply 並進行信箱的驗證，完成後 Permissions 會多一個「<strong>OC_EMAIL</strong>」就代表成功囉。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zk-q-tdHWo6TjNVV111D1A.png" /></figure><p>3. 接著到 Line login 這個分頁設定 Callback URL，若要設定多組的話，直接按 Enter 把新的 URL 寫在新的一行即可。以上 Line 的部分就完成設定了！</p><h4>Google:</h4><ol><li>到 <a href="https://cloud.google.com/">GCP</a> 選擇你的專案，選取左側菜單的 「API和服務」→ 「憑證」</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*qxSd0fxvwulTIj3Nrel_vw.png" /><figcaption>注意需要先完成「OAuth同意畫面」資料設定喔！</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*VgCaCqNx7hsnznuHawBgbQ.png" /><figcaption>點選「建立憑證」 選擇「OAuth用戶端ID」</figcaption></figure><p>2. 完成下方欄位的資料</p><p>⚠️ 已授權的兩的欄位都可以用 <a href="http://localhost/">http://localhost</a> 方便在測試環境上開發</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HSqcccB966D8kl8TfBQXvg.png" /></figure><p>3. 完成後會拿到「用戶端ID」和「用戶端密鑰」，前端須記好「用戶端ID」，google的設定就完成囉！</p><h4>Facebook</h4><ol><li>到臉書的<a href="https://developers.facebook.com/">開發者後台</a>建立一個新的應用程式，並在「Facebook登入」的地方做設定後即完成。</li><li>Client id 從左側菜單「設定」→「基本資料」→「應用程式編號」取得！</li></ol><p>⚠️ 臉書「有效的OAuth重新導向 URI」 只能輸入 https 網域。除了添加網域記得也要把 callback URI 也要加上去！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h8crCqwrv3k9nAOT_J-a6A.png" /></figure><h3>套件選擇</h3><p>在使用Nuxt實現第三方登入功能時，大部分開發者第一直覺就是使用 Nuxt 登入的套件 <a href="https://auth.nuxtjs.org/">Auth Module</a>，但考量到後端實現的方式，若使用 Auth Module 的話會欠缺彈性( 而且Line登入的部分也會卡關 )，因此以下內容，說明PeerOne如何不使用套件完成 Nuxt 第三方登入的功能</p><h3>實作流程</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*urTq9PSp6aNI-vqlJxA_5w.png" /><figcaption>整個流程就會像圖一樣，接下來會一步一步地說明怎麼實作</figcaption></figure><ol><li>首先需要在nuxt 專案設定好三個平台的client id</li></ol><p>nuxt.config.js</p><pre>const clientId = {<br>  fb: &#39;xxxxxxx&#39;,<br>  google: &#39;ooooo.apps.googleusercontent.com&#39;,<br>  line: &#39;zzzzzzzzz&#39;<br>}<br>const callback_uri = &#39;https://www.xxx.com/callback&#39;</pre><p>並增加導向的uri在nuxt.config.js 的 env 參數中</p><pre>env: {<br>  OAUTH_FACEBOOK: &#39;&#39;,<br>  OAUTH_GOOGLE: &#39;&#39;,<br>  OAUTH_LINE: &#39;&#39;<br>}</pre><p>OAUTH_<strong>FACEBOOK</strong>的設定</p><pre><a href="https://www.facebook.com/v4.0/dialog/oauth?">https://www.facebook.com/</a><a href="https://www.facebook.com/v4.0/dialog/oauth?">v4.0/dialog/oauth?</a><br>  client_id=${clientId.fb}<br>  &amp;redirect_uri=${callback_uri}<br>  &amp;response_type=token<br>  &amp;scope=public_profile,email</pre><p>OAUTH_<strong>GOOGLE</strong>的設定</p><pre><a href="https://accounts.google.com/o/oauth2/auth?">https://accounts.google.com/o/oauth2/auth?</a><br>  client_id=${clientId.fb}<br>  &amp;redirect_uri=${callback_uri}<br>  &amp;response_type=code<br>  &amp;scope=profile+email</pre><p>OAUTH_<strong>LINE</strong>的設定</p><pre><a href="https://access.line.me/oauth2/v2.1/authorize?">https://access.line.me/oauth2/v2.1/authorize?</a><br>  client_id=${clientId.fb}<br>  &amp;redirect_uri=${callback_uri}<br>  &amp;response_type=code<br>  &amp;scope=openid%20email%20profile<br>  &amp;bot_prompt=aggressive<br>  &amp;state=${自訂state}</pre><p>不難發現每個平台參數大同小異，不過Line有幾個參數需要注意一下</p><ul><li>response_type只能是code</li><li>state是必填參數</li><li>bot_prompt這個參數可以在取得使用者授權後順便讓使用者加為好友</li></ul><p>2. 設定好參數後，我們需要讓使用者點登入按鈕後導轉到剛剛設定的uri</p><pre>&lt;button @click=&quot;handleClick(&#39;google&#39;)&quot;&gt;Google登入&lt;/button&gt;</pre><pre>...</pre><pre>handleClick (platform) {<br>  //判斷平台，做一些額外處理，並導轉到第三方頁面</pre><pre>  const uri = process.env.OAUTH_GOOGLE</pre><pre>  window.location.href = uri<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fqKcEy8uclBL8q1cw9FDBg.png" /><figcaption>下面都會以google為例子，按下登入後使用者會看到這個畫面</figcaption></figure><p>3. 當使用者透過Google授權登入後，Google會導轉回我們之前設定的callback URI，會發現 URI 會長成下面這樣</p><pre><a href="https://www.xxxxx.com/callback?code=4%2FzQH6IYa9YQyXL6-l8Xkq2IkJQPN-rLAShvsr04cRiKEyV3GHpUUXMpkU_6ihr_VQYfSobngFH8sJBzWYFgoyk_8&amp;scope=email%20profile%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile%20openid&amp;authuser=0&amp;prompt=none">https://www.xxxxx.com/callback?<strong>code</strong>=4%2FzQH6IYa9YQyXL6-l8Xkq2IkJQPN-rLAShvsr04cRiKEyV3GHpUUXMpkU_6ihr_VQYfSobngFH8sJBzWYFgoyk_8&amp;scope=email%20profile%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile%20openid&amp;authuser=0&amp;prompt=none</a></pre><p>我們需要在 nuxt 專案的 pages 目錄新增一個 callback.vue 才能對應我們的 callback URI，callback.vue 要做的事情就是擷取 code 。</p><p>⚠️ 這邊臉書回傳的 URI 比較不一樣，要截取的是#access_token的值</p><pre><a href="https://www.xxxxx.com/callback?#access_token=EAAHfuAg0TDkBAOZB4yRkdZCb1dfrwSs9LYc0NTJhXk2RNZBi6xQfzMGfnZANJzVSThYIwO5zMsHHQfKhWBYFwDAd7Wfx9mXGVb9eDnmf53Mb2KnU7MGdVqU6RLivmzgXRKudH0wT3Xz6Gc7EKOnZCaZCER88PaX3nFrvQ4TVvAmB6lMZCL57ZBUhd8DJV4ZBqr83NKAQbNWIfyQZDZD&amp;data_access_expiration_time=1596251671&amp;expires_in=6329">https://www.xxxxx.com/callback?<strong>#access_token</strong>=EAAHfuAg0TDkBAOZB4yRkdZCb1dfrwSs9LYc0NTJhXk2RNZBi6xQfzMGfnZANJzVSThYIwO5zMsHHQfKhWBYFwDAd7Wfx9mXGVb9eDnmf53Mb2KnU7MGdVqU6RLivmzgXRKudH0wT3Xz6Gc7EKOnZCaZCER88PaX3nFrvQ4TVvAmB6lMZCL57ZBUhd8DJV4ZBqr83NKAQbNWIfyQZDZD&amp;data_access_expiration_time=1596251671&amp;expires_in=6329</a></pre><p>4. 實作 callback.vue</p><p>callback的畫面其實只是一個讀取中的圖片，背後要做的是拿到 token</p><p>我們會預先儲存使用者點了哪個平台的登入按鈕，因為臉書的key是access_token需要做不同的處理。</p><p>取到 token 之後我們就可以向後端發送登入請求了。後端會針對這個 token 做檢查，驗證成功後api會回傳使用者資料就代表登入成功囉。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98f7df8db8c45bd035d1be227a40bb4f/href">https://medium.com/media/98f7df8db8c45bd035d1be227a40bb4f/href</a></iframe><h3>綁定更多帳號</h3><p>大部分網站都會提供多個平台登入，實現的方式其實和上面介紹的一樣</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8cLh1uD1GMbW4qKquDmNhw.png" /><figcaption>實作流程與登入相同</figcaption></figure><ol><li>點擊連結帳號按鈕</li><li>打開分頁導轉 FB</li><li>FB 導轉回 /callback</li><li>callback.vue 拿到 token 打後端綁定帳號 api，關掉分頁</li><li>原本頁面同步更新資料</li></ol><p>這邊我們要稍微改寫一下callback.vue</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f6944a1814cb898909101eb40240d9d/href">https://medium.com/media/2f6944a1814cb898909101eb40240d9d/href</a></iframe><p>要做到不同分頁同步更新 vuex 資料我們需要用到 <a href="https://github.com/xanf/vuex-shared-mutations">vuex-shared-mutations</a> 這個套件。</p><p>在nuxt專案的plugins目錄下新增 vuex-shared-mutations.js</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p4uujXglbBEvi21Y_G3HhA.png" /></figure><p>並在 nuxt.config.js 的 plugins 增加</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zGhOkFpLF-R78Ug5NmHIPQ.png" /></figure><p>以上使用 Nuxt 實現第三方登入的解說即完成了，為了能夠相容不同專案上對於第三方登入的需求，因此 PeerOne Technology 前後端以不採用套件自行刻的方式完成，這篇文章對於找不到適合套件的開發者或許可以帶來點幫助囉！</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8132047ad349" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80/nuxt%E5%AF%A6%E4%BD%9C%E7%AC%AC%E4%B8%89%E6%96%B9%E7%99%BB%E5%85%A5-google-facebook-line-8132047ad349">Nuxt實作第三方登入(Google &amp; Facebook &amp; Line)</a> was originally published in <a href="https://medium.com/peerone-technology-%E7%9A%AE%E5%81%B6%E7%8E%A9%E4%BA%92%E5%8B%95%E7%A7%91%E6%8A%80">PeerOne Technology 皮偶玩互動科技</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>