<?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 Stephen Lai on Medium]]></title>
        <description><![CDATA[Stories by Stephen Lai on Medium]]></description>
        <link>https://medium.com/@stephenlaichaowen?source=rss-fb23eeaad5df------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZnG69RUpRgjRN4UDr_Uexw.png</url>
            <title>Stories by Stephen Lai on Medium</title>
            <link>https://medium.com/@stephenlaichaowen?source=rss-fb23eeaad5df------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 03:07:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@stephenlaichaowen/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[在 Nuxtjs 設置並使用多國語言]]></title>
            <link>https://stephenlaichaowen.medium.com/%E5%9C%A8-nuxtjs-%E8%A8%AD%E7%BD%AE%E4%B8%A6%E4%BD%BF%E7%94%A8%E5%A4%9A%E5%9C%8B%E8%AA%9E%E8%A8%80-ea3d801dcee2?source=rss-fb23eeaad5df------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea3d801dcee2</guid>
            <category><![CDATA[i18n]]></category>
            <category><![CDATA[nuxtjs]]></category>
            <category><![CDATA[localstorage]]></category>
            <category><![CDATA[vuex]]></category>
            <category><![CDATA[nuxtjs多語言]]></category>
            <dc:creator><![CDATA[Stephen Lai]]></dc:creator>
            <pubDate>Wed, 10 Mar 2021 11:04:45 GMT</pubDate>
            <atom:updated>2021-03-10T11:04:45.242Z</atom:updated>
            <content:encoded><![CDATA[<p>Nuxtjs: i18n + Vuex + 下拉選單</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*jCAorj4gi9eX54AYqd1Sbg.gif" /><figcaption>選擇語言後, 切換頁面仍然維持語言狀態</figcaption></figure><p>我正在開發個人部落格網站, 要有多國語言切換的功能 (中文/英文), 看了網路上許多的技術文 (中英文皆有), 多數是針對 vue-cli 專案設置的, 我沒有看到一篇中文文章 (有簡體中文的, 但是沒有詳細說明) 談到 Nuxtjs + i18n 這個主題的, 英文的倒是不少, 不過要做不少修正才能達到我要的需求和功能. 以下就來談談我是怎麼在 Nuxtjs 專案使用下拉式選單做到中英文切換網頁語言並且在切換頁面的情形下仍然保持狀態.</p><p>相信有訪問過 <a href="https://zh.wikipedia.org/wiki/%E9%BE%8D%E5%85%84%E8%99%8E%E5%BC%9F_(%E9%9B%BB%E8%A6%96%E7%AF%80%E7%9B%AE)">wiki</a> 的人都會發現當我們切換語言選項 (繁體中文 &lt;=&gt; 簡體中文), 假設我們選擇了繁體中文, 當我們切換到另一個頁面 (比如左邊的 &quot;討論&quot; 選項), 字體會自動恢復到 default 值 (簡體中文), 很明顯這個網站並沒有紀錄我們在下拉選單選擇的項目. 這是很不好的使用者體驗. 我的網站要使用者選擇合適的語言後, 不論切換到哪個頁面, 或是重整網頁 (選擇性), 仍然維持設定, 提供最佳的使用者體驗.</p><h3>Tech Stack</h3><ul><li>NuxtJS (功能最強大的 VueJS 框架)</li><li>下拉式選單 (select+option 標籤)</li><li>Vuex (狀態管理系統, 所以的組件都可以直接存取資料)</li><li>(選擇性) Local Storage (瀏覽器的內存, 儲存語言選項 (英文/中文)</li></ul><h3>專案架構</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*h4msrmY7xkHtxVJ5isELug.png" /><figcaption>VSCode 專案結構</figcaption></figure><h3>步驟</h3><p>安裝 vue-i18n 套件 npm i vue-i18n</p><p>在 components目錄下建立 LangSwitcher.vue 和 Navbar.vue</p><p><em>LangSwitcher.vue (下拉選單組件)</em></p><pre>&lt;template&gt;<br>  &lt;select <br>    v-model=&quot;$store.state.lang&quot; <br>    @change=&quot;setLang($store.state.lang)&quot;<br>  &gt;&lt;option <br>    v-for=&quot;item in optionLang&quot; <br>      :value=&quot;item.value&quot; <br>      :key=&quot;item.id&quot;<br>    &gt;{{ item.text }}<br>   &lt;/option&gt;<br>  &lt;/select&gt;<br>&lt;/template&gt;</pre><pre>&lt;script&gt;<br>export default {<br>  data() {<br>    return {<br>      optionLang: [<br>        { text: &#39;English&#39;, value: &#39;en&#39;, id: 1 },<br>　　　　　{ text: &#39;中文&#39;, value: &#39;zh&#39;, id: 2 }<br>　　　　]<br>　　　}<br>　　},<br>　methods: {<br>　　setLang(value) {<br>　　　this.$store.commit(&#39;setLang&#39;, value)<br>　　　this.$i18n.locale = value<br>　　}<br>　},<br>}<br>&lt;/script&gt;</pre><h4>說明:</h4><ul><li>下拉選單由 select 和 option 標籤組成.</li><li>v-model 綁定 store 的 lang state</li><li>選單改變時會觸發 setLang 函式, setLang 做兩件事:</li></ul><ol><li>把選單值 (en/zh) 賦值給 store 的 lang</li><li>把選單值賦值給 i18n.locale</li></ol><p><em>Navbar.vue (菜單組件)</em></p><pre>&lt;template&gt;<br>  &lt;div class=&quot;main-header&quot;&gt;<br>    &lt;div class=&quot;navbar&quot;&gt;<br>      &lt;NuxtLink to=&quot;/&quot;&gt;<br>        &lt;div class=&quot;logo&quot;&gt;{{ $t(&#39;title&#39;) }}&lt;/div&gt;<br>      &lt;/NuxtLink&gt;<br>    &lt;div class=&quot;menu-right&quot;&gt;<br>      &lt;div class=&quot;menu&quot;&gt;<br>        &lt;NuxtLink to=&quot;/&quot;&gt;{{ $t(&#39;navbarHome&#39;) }}&lt;/NuxtLink&gt;<br>        &lt;NuxtLink to=&quot;/blog&quot;&gt;{{ $t(&#39;navbarBlog&#39;) }}&lt;/NuxtLink&gt;<br>        &lt;NuxtLink to=&quot;/about&quot;&gt;{{ $t(&#39;navbarAbout&#39;) }}&lt;/NuxtLink&gt;  <br>        &lt;NuxtLink to=&quot;/work&quot;&gt;{{ $t(&#39;navbarWork&#39;) }}&lt;/NuxtLink&gt; <br>        &lt;LangSwitcher /&gt;<br>      &lt;/div&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/div&gt;<br>&lt;/template&gt;</pre><pre>&lt;style scoped&gt;<br>.menu-right {<br>  display: flex;<br>  align-items: center;<br>}<br>.main-header {<br>  border-bottom: var(--bottom);<br>}<br>.navbar {<br>  height: 112px;<br>  padding: 0 15px;<br>  display: flex;<br>  justify-content: space-between;<br>  align-items: center;<br>  position: relative;<br>}<br>.logo {<br>  font-weight: 400;<br>  font-size: 38px;<br>  color: var(--primary);<br>  text-transform: uppercase;<br>}<br>.menu {<br>  width: 525px;<br>  display: flex;<br>  justify-content: space-between;<br>}<br>@media (max-width: 992px) {<br>  .navbar {<br>    height: 90px;<br>  }<br>  .logo {<br>    font-size: 32px;<br>  }<br>}<br>a {<br>  text-decoration: none;<br>  color: var(--item);<br>  font-size: 22px;<br>  font-weight: 300;<br>}<br>a.nuxt-link-exact-active {<br>  color: var(--primary);<br>  font-weight: 400;<br>}<br>&lt;/style&gt;</pre><h4>說明:</h4><p>i18n 套件會根據 i18n.locale物件的內容 (中文或英文) 把值設定給 store 的 langstate, 然後我們在組件內使用 {{$t(variable)}} 把值動態渲染出來</p><p>在 layouts\default.vue 貼上下列內容</p><pre>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;Navbar /&gt;<br>    &lt;Nuxt class=&quot;style&quot; /&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><pre>&lt;style&gt;<br>@import url(https://medium.com/&#39;https://fonts.googleapis.com/css2?family=Rubik:wght@300;400;500;600;700&amp;display=swap&#39;);</pre><pre>:root {<br>  --primary: #278f6e;<br>  --item: #707070;<br>  --subtitle: #313131;<br>  --bottom: 1px solid #efefef;<br>  --brDefault: 30px;<br>}<br>.style {<br>  display: flex;<br>  height: 30vh;<br>  justify-content: center;<br>  align-items: center;<br>}<br>*,<br>*::before,<br>*::after {<br>  box-sizing: border-box;<br>  margin: 0;<br>  padding: 0;<br>}<br>html, <br>body {<br>  font-family: &#39;Rubik&#39;, sans-serif;<br>  background: var(--bg-color);<br>}<br>&lt;/style&gt;</pre><p>建立 locales 資料夾, 新增 en.json 和 zh.json</p><p><em>en.json 內容</em></p><pre>{<br>  &quot;title&quot;: &quot;LOGO&quot;,<br>  &quot;navbarHome&quot;: &quot;Home&quot;,<br>  &quot;navbarBlog&quot;: &quot;Blog&quot;,<br>  &quot;navbarAbout&quot;: &quot;About&quot;,<br>  &quot;navbarWork&quot;: &quot;My Work&quot;<br>}</pre><p><em>zh.json</em></p><pre>{<br>  &quot;title&quot;: &quot;標識&quot;,<br>  &quot;navbarHome&quot;: &quot;首頁&quot;,<br>  &quot;navbarBlog&quot;: &quot;部落格&quot;,<br>  &quot;navbarAbout&quot;: &quot;關於我&quot;,<br>  &quot;navbarWork&quot;: &quot;作品集&quot;<br>}</pre><h4>說明:</h4><p>語言的資料存放在 locales 資料夾內, 如果要新增其他語言, 比如法文, 就新增一個 fr.json 檔案, 然後加入法文資料.</p><p>在 pages 資料夾下建立 about.vue , blog.vue , work.vue</p><p><em>index.vue</em> 內容</p><pre>&lt;template&gt;<br>  &lt;div class=&quot;home-page&quot;&gt;<br>    &lt;h1&gt;{{ $t(&#39;homepage&#39;) }}&lt;/h1&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><p><em>about.vue</em> 內容</p><pre>&lt;template&gt;<br>  &lt;div class=&quot;about-page&quot;&gt;<br>    &lt;h1&gt;{{ $t(&#39;navbarAbout&#39;) }}&lt;/h1&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><p><em>blog.vue 內容</em></p><pre>&lt;template&gt;<br>  &lt;div class=&quot;blog-page&quot;&gt;<br>    &lt;h1&gt;{{ $t(&#39;navbarBlog&#39;) }}&lt;/h1&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><p><em>work.vue</em> 內容</p><pre>&lt;template&gt;<br>  &lt;div class=&quot;work-page&quot;&gt;<br>    &lt;h1&gt;{{ $t(&#39;navbarWork&#39;) }}&lt;/h1&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><p>在 plugins 資料夾建立 i18n.js</p><p><em>i18n.js</em> 內容</p><pre>import Vue from &#39;vue&#39;<br>import VueI18n from &#39;vue-i18n&#39;</pre><pre>Vue.use(VueI18n)</pre><pre>export default ({ app, store }) =&gt; {<br>  app.i18n = new VueI18n({<br>    locale: store.state.locale,<br>    fallbackLocale: &#39;en&#39;,<br>    messages: {<br>      en: require(&#39;~/locales/en.json&#39;),<br>      zh: require(&#39;~/locales/zh.json&#39;),<br>    },<br>  })</pre><pre>  app.i18n.path = (link) =&gt; {<br>    if (app.i18n.locale === app.i18n.fallbackLocale) {<br>      return `/${link}`<br>    }<br>    return `/${app.i18n.locale}/${link}`<br>  }<br>}</pre><h4>說明:</h4><p>i18n 插件的檔案, 1. 把 i18n.locale 和 store 做綁定; 2. 引入 語言資料 (en.json/zh.json)</p><p>在 store 資料夾建立 index.js , state.js , mutations.js</p><p><em>index.js</em> 內容</p><pre>import Vuex from &#39;vuex&#39;<br>import { state } from &#39;./state.js&#39;<br>import { mutations } from &#39;./mutations.js&#39;</pre><pre>const createStore = () =&gt; {<br>  return new Vuex.Store({<br>    state,<br>    mutations,<br>  })<br>}</pre><pre>export default createStore</pre><p><em>state.js</em> 內容</p><pre>export const state = {<br>  lang: &#39;en&#39;<br>}</pre><p><em>mutations.js</em> 內容</p><pre>export const mutations = {<br>  setLang(state, data) {<br>    state.lang = data<br>  }<br>}</pre><h4>說明:</h4><ul><li>index.js 設定並且註冊 vuex</li><li>state.js 設置 lang 的初始值為英文</li></ul><p>在 nuxt.config.js 註冊 i18n</p><pre>export default {<br>  plugins: [<br>    &#39;~/plugins/i18n.js&#39;<br>  ]<br>}</pre><p>我們也可以把 lang 存到 localStorage, 這樣網頁重整語言設定也不會跑掉, 不過我覺得沒有這個必要, 因為現在是多語言時代, 英文/中文交替使用, 不需要說只要選擇了某個語言網頁就永久記住, 只要切換頁面維持狀態即可.</p><ul><li><a href="https://i18n-demo-nuxtjs.vercel.app/">DEMO</a></li><li><a href="https://github.com/stephenlaichaowen/i18n-demo-nuxtjs">Github</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea3d801dcee2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[在 Vue 專案中使用 Tailwindcss]]></title>
            <link>https://stephenlaichaowen.medium.com/%E5%9C%A8-vue-%E5%B0%88%E6%A1%88%E4%B8%AD%E4%BD%BF%E7%94%A8-tailwindcss-a28b1f965770?source=rss-fb23eeaad5df------2</link>
            <guid isPermaLink="false">https://medium.com/p/a28b1f965770</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[adam-wathan]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[tailwind-css]]></category>
            <dc:creator><![CDATA[Stephen Lai]]></dc:creator>
            <pubDate>Tue, 02 Mar 2021 10:13:04 GMT</pubDate>
            <atom:updated>2021-03-02T10:16:42.128Z</atom:updated>
            <content:encoded><![CDATA[<p>感覺不出有甚麼不同呢 😜</p><p>我喜歡 Vue 單一組件檔案的開發方式, html, css, javascript 都寫在一個檔案內, 方便開發管理, 不需要像 react 要另外引入 css 檔案 (現在 styled component 讓 react 可以做到單一檔案組件; Next 有 jsx 的 style 可以在 component 內寫 css), angular 更扯把 html, css, js 全部拆開. 單一組件檔案的好處是當我們在寫 css 不需要在 vscode 開兩個視窗. 組件的 html 標籤通常都不會很多, 所以把 html, css, js 寫在一起並不會造成開發上的困擾, 只會讓開發更便利.</p><p>最近用 tailwindcss 用上癮了, 因為 utility class 真的很方便, 功能強大且齊全. 最強大的地方是不需要寫 css, 全部在 html 標籤內作業即可. 不過我不喜歡寫落落長的 class, 所以我用到 `@apply` decorator 很多, 我通常都會把所有的 class 寫在 &#39;@apply`, 在 html 標籤只用到一個 class, 舉例來說:</p><pre>// HTML<br>&lt;template&gt;<br>  &lt;div class=&quot;card&quot;&gt;<br>    &lt;img src=path&gt;<br>    &lt;div class=&quot;content&quot;&gt;<br>      &lt;h3&gt;card title&lt;/h3&gt;<br>      &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing&lt;/p&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><pre>// Tailwindcss<br>&lt;style scoped&gt;<br>.card {<br>  @apply max-w-screen-lg mx-auto border border-green-500;<br>}<br>.content {<br>  @apply py-4 px-8 border shadow-lg;<br>}&lt;/style&gt;</pre><p>可是我發覺這樣的 tailwindcss 寫法和用純 css 寫差異並不大呀</p><pre>// CSS<br>&lt;style scoped&gt;<br>.card {<br>  max-width: 1024px;<br>  margin: auto;<br>  border: 1px solid green;<br>}<br>.content {<br>  padding: 1rem 2rem;<br>  border: 1px solid;<br>  box-shadow: 5px 10px;<br>}</pre><p>Tailwindcss 的推薦用法是把所有 class 寫在 html 標籤內, 像以下這樣, 才是 tailwind 的作者 Adam Watham 強調的 ~ <strong><em>Youn never leave your html</em></strong></p><pre>&lt;template&gt;<br>  &lt;div class=&quot;max-w-screen-lg mx-auto border border-green-500&quot;&gt;<br>    &lt;img src=path&gt;<br>    &lt;div class=&quot;py-4 px-8 border shadow-lg&quot;&gt;<br>      &lt;h3&gt;card title&lt;/h3&gt;<br>      &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing&lt;/p&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><p>所以對於喜歡乾淨程式碼的人來說, tailwindcss 和 pure css 其實沒有差異, 所以對於喜歡在標籤內寫落落長 class 的朋友來說, tailwindcss 就是最好的朋友 😄😄😄</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a28b1f965770" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[手刻 Figma Tailwind CSS UI / Design to Code]]></title>
            <link>https://stephenlaichaowen.medium.com/%E6%89%8B%E5%88%BB-figma-tailwind-css-ui-design-to-code-4dc0b9057ebb?source=rss-fb23eeaad5df------2</link>
            <guid isPermaLink="false">https://medium.com/p/4dc0b9057ebb</guid>
            <dc:creator><![CDATA[Stephen Lai]]></dc:creator>
            <pubDate>Tue, 02 Mar 2021 08:42:22 GMT</pubDate>
            <atom:updated>2021-03-02T08:46:15.187Z</atom:updated>
            <content:encoded><![CDATA[<p>把 Figma Tailwind CSS Ui Kit 的組件轉換成程式碼</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*8X6BleF2A6TpW0ns5imb4A.png" /></figure><p>自從學習了 tailwindcss 後就深深的愛上這個工具, 他和 bootstrap 很像, 有很多 uility class 可以用, 不同的是 bootstrap 除了 utility class 還有許多現成的組件可以套用, 而 tailwindcss 則是全部是 utility class, 讓開發者可以不用寫一行 css 的情況下完全在 html 標籤內作業. 我的心得是沒有設計稿要刻出漂亮的 ui 元件, 本身要對設計有一定的認識和了解, 比方說要懂 Design Principle, 要懂得運用 font (family, weight, color), padding, margin, space, grid, alignment, negative space, 還有非常重要的 color. 如果有這方面的概念, 用 tailwindcss 可以很自然的手刻出漂亮的元件.</p><p>我正在找部落格的範例, 網海茫茫, portfolio / blog template 多倒數不清, 一直都找不到滿意的範例, 於是想到在 Figma Community 找靈感, 順便練習 UI 設計, 結果看到了 Tailwind CSS UI Kit, 裡面有一些很漂亮的 UI 元件, 就有衝動刻出來. 我使用 vue/vite 框架, 原因是 vite 有 hmr (hot module reload) 的功能, 我們在修改部分程式碼儲存後, 網頁會立即更新變動的原件, 立刻渲染出來. 比起 vue-cli, nuxt, next, create-react-app 在開發上便利且快速許多. 因為沒有 hmr 的功能每一次修改程式碼儲存後整個專案都要重新打包編譯, 非常不合理而且耗費 CPU 資源.</p><p>我看了設計稿後發覺並不需要用到 tailwindcss, 原因是 Figma 提供元件所有的 css 資料, 在 inspect 項目下還有 css 代碼, 有了這麼豐富的資訊, 用純粹的 css 來刻 UI 元件是最好最方便最快的方法. 這份 UI Kit 只有 Nabvar, Alerts, Card, Form (Signin, Signup), 所以我一鼓作氣一次完成. 全部響應式. 有興趣的朋友可以下載我的 Github 直接套用.</p><p>P.S. 我把專案部屬到 Vercel, 可是顯示的 font family 是預設值, 並不是我引入的 Quicksand 字體, 我猜可能是 Vite 在打包編譯過程中出了問題, 我後來有把 &lt;link&gt; 插入 index.html 並重新 build 過, 可是結果還是一樣, 我沒有深入探究. 我認為換個框架, vue-cli, nuxt 可能就沒有這個問題, 畢竟 vite 還不到完全成熟的階段.</p><ul><li><a href="https://tailwindcss-components-collections.vercel.app/">Demo</a></li><li><a href="https://www.figma.com/community/file/768809027799962739">Tailwind CSS UI Kit</a></li><li><a href="https://github.com/stephenlaichaowen/tailwindcss-components-collections">Github</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4dc0b9057ebb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NuxtJS + animate.css 超入門]]></title>
            <link>https://stephenlaichaowen.medium.com/nuxtjs-animate-css-%E8%B6%85%E5%85%A5%E9%96%80-40bed5e9946c?source=rss-fb23eeaad5df------2</link>
            <guid isPermaLink="false">https://medium.com/p/40bed5e9946c</guid>
            <category><![CDATA[transitions]]></category>
            <category><![CDATA[cofig]]></category>
            <category><![CDATA[animatio]]></category>
            <category><![CDATA[nuxtjs]]></category>
            <dc:creator><![CDATA[Stephen Lai]]></dc:creator>
            <pubDate>Wed, 08 Jul 2020 01:38:08 GMT</pubDate>
            <atom:updated>2020-07-08T01:46:50.025Z</atom:updated>
            <content:encoded><![CDATA[<p>使用 <a href="https://animate.style/">animate.css</a> 程式庫讓你的 NuxtJS 元件輕鬆動起來</p><p>animate.css 是一個強大的動畫函式庫, 我們可以輕鬆地為元件加入酷炫, 複雜的動畫. 如果要讓 h1 標籤在網頁重整時做淡入效果, 只需要在元件的標籤加上動畫的 class 名稱即可</p><pre>&lt;h1 class=&quot;animated__animated animate__fadeIn&quot;&gt;This is title&lt;/h1&gt;</pre><p>首先要安裝 animte.css 模組, 在 Google 打關鍵字 nuxtjs animate.css, 找到了 <a href="https://github.com/neneos/nuxt-animate.css">nuxt-animate.css</a> 這個套件</p><ul><li>安裝套件 : npm i @neneos/nuxt-animate.css</li><li>在 nuxt.config.js 的 modules 屬性註冊 animte.css</li></ul><pre>module.exports = {<br>  ...<br>  /*<br>  ** Nuxt.js modules<br>  */<br>  modules: [<br>    &#39;@neneos/nuxt-animate.css&#39;<br>  ]<br>  ...<br>}</pre><p>在 VueJS 專案要讓元件動起來的方法是在元件外層加入 &lt;transition&gt; 標籤, 假設我們要讓 h1 標籤做淡入/淡出的動畫</p><pre>&lt;transition<br>  mode=&quot;out-in&quot;<br>  enter-active-class=&quot;animte_animated animate__fadeIn&quot;<br>  leave-active-class=&quot;animte_animated animate__fadeOut&quot;<br>&gt;<br>  &lt;h1&gt;<br>&lt;/transition&gt;</pre><p>好了, 照理講頁面重整時應該可以看到淡入效果, 切換頁面可以看到淡出效果. 可是沒有任何反應. 所以第二個想到的方法就是在 nuxt.config.js 引入 animate.css 的 cdn 連結</p><pre>module.exports = {<br>  ...<br>  /*<br>  ** Headers of the page<br>  */<br>  head: {<br>    link: [<br>      { rel: &#39;stylesheet&#39;, href: &#39;https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.0.0/animate.min.css&#39; }<br>    ...<br>}</pre><p>元件還是沒有動作. 所以拿掉外層的 &lt;transition&gt; 標籤, 直接在元件的 class 加入動畫 css</p><pre>&lt;h1 class=&quot;title animate__animated animate__slideInRight&quot;&gt;<br>  My Project !!!<br>&lt;/h1&gt;</pre><p>這樣就可以動作了. 至於 &lt;transition&gt; 為什麼在 nuxtjs 沒有作用就是另外一個要研究的題目.</p><h3>補充說明</h3><p>我們可以在 Nuxtjs 專案輕鬆設定切換頁面時要產生的動畫效果.</p><p>首先在 assets 資料夾建立 style/transition.css, 加入下列代碼</p><pre>.fade-enter-active, .fade-leave-active {<br>  <em>transition</em>: opacity 2000ms;<br>}</pre><pre>.fade-enter, .fade-leave-to {<br>  <em>opacity</em>: 0;<br>}</pre><p>然後在 nuxt.config.js 做以下設置</p><pre>module.exports = {<br>  css: [<br>    &#39;~assets/style/transition.css&#39;<br>  ],<br>  transition: {<br>    name: &#39;fade&#39;,<br>    mode: &#39;out-in&#39;<br>  }<br>}</pre><p>這樣就行了. 切換頁面就可以看到淡入淡出的效果囉 !!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40bed5e9946c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CSS 動畫 & 漸變超入門]]></title>
            <link>https://stephenlaichaowen.medium.com/css-%E5%8B%95%E7%95%AB-%E6%BC%B8%E8%AE%8A%E8%B6%85%E5%85%A5%E9%96%80-c126b5c5c036?source=rss-fb23eeaad5df------2</link>
            <guid isPermaLink="false">https://medium.com/p/c126b5c5c036</guid>
            <category><![CDATA[transitions]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[design-to-code]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[moder-css]]></category>
            <dc:creator><![CDATA[Stephen Lai]]></dc:creator>
            <pubDate>Tue, 07 Jul 2020 14:26:23 GMT</pubDate>
            <atom:updated>2020-07-07T15:29:34.967Z</atom:updated>
            <content:encoded><![CDATA[<p>設計師和開發者都需要熟悉的知識和技能</p><p>動畫 &amp; 漸變效果是現代網頁/APP 不可或缺的元素, 不管是設計師還是開發者/工程師, 都需要曉得怎樣讓組件動起來. 主流框架比如 VueJS, ReactJS 也都自己產生動畫的方式; 比如在 VueJs 中用 &lt;transistion&gt; 標籤把要動的組件包起來; 至於 ReactJS, 現在有太多先進的動畫程式庫專門為其量身打造, 比如現在最火紅的 Framer Motion, 以及 React Spring, GSAP 等動畫函式庫.</p><h3>要讓元件動起來的方式就是在元件的樣式中加入 animation / transition 屬性</h3><h4>設置元件 animation 屬性</h4><ul><li>animation-name : 動畫名稱, 例如 myanimation</li><li>animation-duration : 動畫持續時間, 1000ms 表示 1 秒</li><li>animation-iteration-count : 動畫播放次數</li><li>animation-fill-mode : forwards; 動畫結束維持最後狀態</li><li>animation-delay : 延後播放動畫時間</li><li>animation-direction: alternate; 逆向播放動畫</li><li>animation-timing-function : 動畫模式, 有 ease-in, ease-out, linear, cubic-bezier 等</li></ul><h4>範例: 在 h1 標籤設置動畫屬性</h4><pre>h1 {<br>  animation-name: myanimation;      <br>  animation-duration: 1000ms; <br>  animation-timing-function: ease-in;<br>  animation-iteration-count: 1;<br>}</pre><h4>使用 keyframes 撰寫動畫內容</h4><pre>@keyframes myanimation {<br>  0% { background-color: white; left: 0px; top: 0px; }<br>  50% { background-color: green; left: 300px; top: 300px; }<br>  100% { background-color: white; left: 0px; top: 0px; }<br>}</pre><h4>設置元件 transition 屬性</h4><p>和 animation 不同之處在於 transition 需要被觸發, 最常看到的例子是 button hover 效果,</p><h4>在 button 元件設置 transition 屬性</h4><ul><li>transition-property : 設置要漸變的 css 屬性</li><li>transition-duration : 漸變持續時間, 比如 1000ms 等於 1 秒</li><li>transition-timing-function: 漸變模式, 同 animation</li><li>transition-delay : 延後漸變時間</li></ul><h4>範例: 在 button 標籤設置漸變屬性</h4><pre>button {<br>  transition-property: background, border-radius, transform;<br>  transition-duration: 1000ms;<br>  transition-timing-function: ease-out;<br>}</pre><h4>當 button 是 hover 的狀態設置要漸變的 css 屬性</h4><pre>button:hover {<br>  background: red;<br>  border-radius: 50%;<br>  transform: rotateY(180deg) scale(0.8) translate(100px, 200px);<br>}</pre><p>基本上就是這樣, 現在的原型都加入很多很酷炫, 很複雜的動畫, 所以身為開發者/工程師必須熟悉動畫/漸變製作才能符合變動超級快速的前端世界 !!!</p><h3>Demo 網址 : <a href="https://css-animation-transition-demo.netlify.app/">https://css-animation-transition-demo.netlify.app/</a></h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c126b5c5c036" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nuxtjs 超入門]]></title>
            <link>https://stephenlaichaowen.medium.com/nuxtjs-%E8%B6%85%E5%85%A5%E9%96%80-762d17f7302b?source=rss-fb23eeaad5df------2</link>
            <guid isPermaLink="false">https://medium.com/p/762d17f7302b</guid>
            <dc:creator><![CDATA[Stephen Lai]]></dc:creator>
            <pubDate>Thu, 02 Jul 2020 05:58:46 GMT</pubDate>
            <atom:updated>2020-07-09T16:05:07.685Z</atom:updated>
            <content:encoded><![CDATA[<p>VueJs 開發者必須會的全端框架</p><p>Nuxt.js 的精華在提供單頁式應用程序(SPA) SSR (Server Side Rendering)的功能, 也就是在後端預先渲染網頁, 再回傳到前端顯示. 如果用 vue-cli 或者引入 vue.js cdn 的方式開發, 在網頁上按滑鼠右鍵檢查網頁原始碼只會看到 &lt;div id=”app”&gt;&lt;/div&gt; 空白內容. 這樣對於搜尋引擎爬文非常的不友善. Nuxtjs 就是為了解決這個問題而生的 Vuejs 框架, 並且整合了後端功能例如 expressjs 可以讓我們開發全端產品.</p><h4>Nuxtjs 的優點如下</h4><ul><li>整合了 vuex, vue router, 和 vue-meta, 不需要做額外設定就可以直接使用. 比如 router, 只要將頁面放在 pages 資料夾內自動生成路由, 不需要寫手動設置路由</li><li>內建的資料夾讓我們可以很好的規畫專案內的檔案, 每個資料夾都有特定功能, 比如在 pages 資料夾的組件會自動生成路由; layout 資料夾下設置 layout 組件; assets 資料夾內放置全域檔案如 css 樣式, 圖片, 聲音, 文字檔</li><li>Nuxtjs 有以下功能資料夾<br>- assets : 存放 css, 圖片, 聲音, 文字檔<br>- components : 存放會重複使用到的組件<br>- layouts : 存放默認 layout 以及自定義 layout<br>- middleware : 存放中介軟體 (函示)<br>- pages : 存放頁面組件<br>- plugins : 設定第三方插件或自訂義插件<br>- static : 和 assets 資料夾功能相同, ~官方不推薦使用~<br>- store : Vuex 檔案存放區</li><li>對搜尋引擎非常友善, NuxtJs 提供的 SSR 讓 Vuejs SPA 很容易被搜尋引擎找到, 這是用 vue-cli 或者引入 cdn 開發辦不到的, 這也是 nuxtjs 最大的特色</li><li>使用 universal 或者 static 模式會先在後端預先渲染好網頁, 讓 nuxtjs app 在第一次向後端請求頁面快速得到渲染好的畫面, 補足 spa 模式第一次載入時要花費很長時間的缺點</li></ul><h3>開發 Vuejs 專案重點摘要</h3><h4>父組件傳屬性給子組件</h4><p>在 vue-cli 或 nuxtjs 專案中建立組件, 決定那些屬性要由外部(父組件)傳入, 例如在 PostPreview.vue 組件中 設置 id, title, thumbnail, previewText 四個屬性</p><p>在 &lt;script&gt; 設置 props 屬性,</p><pre>&lt;script&gt;<br>export default {<br>  props: {<br>    id: {<br>      type: Number,<br>      default: 0,<br>      required: true,<br>      validator(value) {<br>        // 如果傳入的值小於 10, console 印出 &#39;validate vaule&#39; 字樣; 否則印出 &#39;illegal value&#39; 字樣<br>        const message = (value &lt;= 10) ? &#39;legal value&#39; : &#39;illegal value&#39;<br>        console.log(message)      <br>      }<br>    },<br>    title: {<br>      type: String,<br>      required: true<br>    },<br>    previewText: {<br>      type: String,<br>      required: true<br>    },<br>    thumbnail: {<br>      type: String,<br>      required: true<br>    }<br>  }<br>}<br>&lt;/script&gt;</pre><pre>設置 props 屬性相當於用 typescript 設置每個變數的資料格式, 考慮相當周全</pre><p>在 &lt;template&gt; 渲染出來</p><pre>&lt;template&gt;<br>  &lt;nuxt-link :to=&quot;`/posts/${id}`&quot; class=&quot;post-preview&quot;&gt;<br>    &lt;article&gt;<br>      &lt;div class=&quot;post-thumbnail&quot;<br>        :style=&quot;{backgroundImage: `url(https://medium.com/$%7Bthumbnail%7D)`}&quot;<br>      &gt;<br>      &lt;/div&gt;<br>      &lt;div class=&quot;post-content&quot;&gt;<br>        &lt;h1&gt;{{ title }}&lt;/h1&gt;<br>        &lt;p&gt;{{ previewText }}&lt;/p&gt;<br>      &lt;/div&gt;<br>    &lt;/article&gt;<br>  &lt;/nuxt-link&gt;<br>&lt;/template&gt;</pre><p>父組件 Post.vue 傳 props 給子組件 PostPreview, 注意: 如果傳入的不是字串, 屬性欠要加上 ‘:’ 符號, 例如 :id=”12&quot; 等於傳入值 12; 如果 id=”12&quot; 等於傳入字串 ‘12’</p><pre>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;PostPreview <br>      :id=&quot;1&quot;<br>      title=&quot;This is Post 1&quot;<br>      thumbnail=&quot;https://pixel.com/images/1&quot;<br>      previewText=&quot;This is first post!&quot;<br>    &gt;<br>    &lt;PostPreview <br>      :id=&quot;2&quot;<br>      title=&quot;This is Post 2&quot;<br>      thumbnail=&quot;https://pixel.com/images/2&quot;<br>      previewText=&quot;This is second post!&quot;<br>    &gt;<br>    &lt;PostPreview <br>      :id=&quot;3&quot;<br>      title=&quot;This is Post 3&quot;<br>      thumbnail=&quot;https://pixel.com/images/3&quot;<br>      previewText=&quot;This is third post!&quot;<br>    &gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><h4>設置活躍菜單(active link) 樣式</h4><p>在 Nuxtjs 專案中如果我們希望點選到的菜單(menu)維持樣式(例如文字變成紅色, 滑鼠點選網頁任何地方樣式都不會消失), 在 style 加入下列代碼</p><pre>a:hover,<br>a:active,<br>a.nuxt-link-active {<br>  color: red;<br>}</pre><h4>讓元素動起來</h4><p>在 Nuxtjs 專案中要讓元素產生動畫, 方法和 vuejs 一樣, 用 &lt;transition name=”動畫名稱”&gt; 標籤把元素包起來, 然後再組件的作用域樣式(scoped style)內設定動畫內容. 假設我們要做一個左右滑動(slide-side)效果</p><pre>&lt;template&gt;<br>  &lt;transition name=&quot;slide&quot;&gt;<br>    &lt;h1&gt;This is title&lt;/h1&gt;<br>  &lt;/transition&gt;<br>&lt;/template&gt;</pre><pre>&lt;style scoped&gt;<br>// 滑入後和滑出前的狀態(x=0)<br>.slide-side-enter-active,<br>.slide-side-leave-active {<br>  transition: transform 0.3s ease-out;<br>}</pre><pre>// 滑入前和滑出後的位置在(x=-100%)<br>.slide-side-enter,<br>.slide-side-leave-to {<br>  transform: translateX(-100%);<br>}<br>&lt;/style&gt;</pre><h4>link(連結)</h4><ul><li>在 html 檔案中連結用 &lt;a href=&quot;路徑&quot;&gt;&lt;/a&gt;表示, 點擊連結會發送請求到服務器, 服務器會根據請求發送渲染好的網頁到前端.</li><li>在 vue-cli 專案中連結用 &lt;router-link to=&quot;路徑&quot;&gt;&lt;/router-link&gt;表示</li><li>在 nuxtjs 專案中連結用 &lt;nuxt-link to=&quot;路徑&quot;&gt;&lt;/nuxt-linke&gt; 表示</li><li>也可以用 javascript 執行連結, 在函式內加入 this.$router.push(`/users/${id}`) 切換到指定使用者頁面, vue-cli 和 nuxtjs 專案皆通用</li></ul><h4>建立 Global CSS 檔案</h4><ul><li>在 assets 資料夾下建立 `style/main.css`</li><li>在 `nuxt.config.js` 的 css 陣列內引入 <br>css: [ &#39;~/assets/style/main.css’]</li></ul><h4>加入客製化 Error Page</h4><ul><li>在 layoutout 資料夾下建立 error.vue</li><li>在 &lt;template&gt; 導向特定頁面用 &lt;a&gt; 因為產生錯誤 &lt;nuxt-link&gt; 不能正常動作</li></ul><h4>加入 Google Fonts</h4><ul><li>在 nuxt.config.js 的 head 物件內加入 <br>head: {<br> link: [ { rel: ‘stylesheet’, href: ‘https://fonts.googleapis.com/css?..’ } ]<br>}</li><li>然後在 component, layout 引入</li></ul><h4>加入靜態資產(圖片, 聲音, 影片)</h4><ul><li>在 assets 資料夾下加入 images/xxx.jpg</li><li>組件內引入 ‘~assets/images/xxx.jpg’</li></ul><h4>加入客製化 layout</h4><ul><li>在 layout 資料夾下建立 admin.vue</li><li>在組件內引入 export default { layout: ‘admin’ }</li></ul><h4>asyncData(context) { }</h4><ul><li>在 client 和 server 端作用, 可以用 console.log() 檢查前後端 console 輸出信息</li><li>處理 Server-Side 傳過來的資料,</li><li>asyncData 在組件還沒渲染前作用, 所以 this 不能用, 必須用 context 存取屬性; 比如 this.$route.params.id 要改為 context.route.params.id</li><li>只能作用在 pages 資料夾下, components, layout 都不能用 !!!</li></ul><p>範例: 在 asyncData() 內讀取 firebase 資料</p><pre>import axios from &#39;axios&#39;</pre><pre>asyncData(context) {<br>  return axios<br>    .get(`https://nuxt-blog.firebaseio.com/posts/${context.params.postId}.json`)<br>    .then(res =&gt; return { loadedPosts: res.data }})<br>    .catch(e =&gt; context.error())<br>}</pre><h4>Vuex Store</h4><ul><li>在 store 資料夾建立 index.js</li><li>有兩種模式: Classic Mode, Modules Mode</li></ul><h4>fetch(context) { }</h4><ul><li>只作用在 pages 資料夾內</li><li>fetch 到 vuex store 的資料要用 computed 讀取</li></ul><h4>nuxtServerInit(vuexContext, contect) { }</h4><p>在 store 內的 actions 物件內加入 actions: { nuxtServerInit(vuexContext, context) { }</p><p>在 nuxtServerInit() fetch 遠端 api 資料, 例如從 firebase 讀取資料</p><pre>actions: {<br>  nuxtServerInit(vuexContext, context) {<br>    return axios.get(&#39;https://nuxt-blog.firebaseio.com/posts.json&#39;)<br>      .then(res  =&gt; {<br>        const postsArray = []<br>        for (const key in res.data) {<br>          postsArray.push({ ...res.data[key], id: key }]<br>        }<br>        vuexContext.commit(&#39;setPosts&#39;, postsArray)<br>      })<br>      .cactch(e =&gt; context.error(e))<br>  }<br>}</pre><h4>非常重要的檔案 ~ nuxt.config.js</h4><p>nuxt.config.js 屬性作用於全域</p><ul><li>mode : 可以設定 ‘universal’ 或 ‘spa’ 模式</li><li>head : 設定 html 的 &lt;head&gt;, 可以被 pages 下的路由組件覆寫</li></ul><pre>head: {<br>  title: &#39;&#39;,<br>  meta: [<br>    { name: &#39;&#39;, content: &#39;&#39; },<br>    { hid: &#39;&#39;, name: &#39;&#39;, content: &#39;&#39; }<br>  ],<br>  link: [<br>    { rel: &#39;&#39;, type: &#39;&#39;, href: &#39;&#39; }<br>  ]<br>}</pre><ul><li>loading : 客制化進度條 (progress-bar)</li></ul><pre>// 設置進度條顏色, 高度, 時間<br>loading: { color: &#39;#fa923f&#39;, height: &#39;4px&#39;, duration: 5000 }</pre><pre>// 禁用進度條<br>loading: false</pre><ul><li>css : 設置全域 css 檔案</li></ul><pre>// assets/styles 資料夾下的 main.css 適用於所有組件, &#39;~&#39; 符號後不須加上 &#39;/&#39;, 直接連接資料夾名稱</pre><pre>css: [  <br>  &#39;~assets/styles/main.css&#39;<br>]</pre><ul><li>env : 設置環境變數</li></ul><pre>// 組件非同步存取遠端資料可以用 process.env.baseUrl 取代寫死的網址<br>env: {<br>  baseUrl: process.env.BASE_URL || &#39;https://nuxt-blog.firebase.com&#39;<br>}</pre><ul><li>rootDir : 設置根路徑</li></ul><pre>// 默認路徑<br>rootDir: &#39;/&#39;</pre><pre>// 設置 my-app 為根目錄<br>rootDir: &#39;/my-app/&#39;</pre><ul><li>router :</li><li>srcDir : 設置專案路徑</li></ul><pre>// 設置專案在 client-app 路徑下<br>srcDir: &#39;client-app/&#39;</pre><ul><li>transition : 設置切換頁面時的漸變效果</li></ul><pre>transition: {<br>  name: &#39;fade&#39;, // css 檔案中 class 的開頭名稱, 相當於 &lt;transition name=&quot;fade&quot;&gt;<br>  mode: &#39;out-in&#39; // 漸變模式, page 先 fadeOut, 然後 fadeIn<br>}</pre><p>在 assets/styles/main.css 加入下列代碼</p><pre>.fade-enter-active, .fade-leave-active {<br>  transition: opacity .5s;<br>}</pre><pre>.fade-enter, .fade-leave-to {<br>  opacity: 0;</pre><ul><li>plugins : 全域設置常用功能 (例如自訂義或者第三方組件), 使用該組件不需要被引入, 直接調用即可</li></ul><p>範例一 : 在 plugins 資料夾建立 core-component.js, 加入下列代碼</p><pre>import Vue from &#39;vue&#39;</pre><pre>import AppButton from &#39;@/components/UI/AppButton&#39;</pre><pre>Vue.component(&#39;AppButton&#39;, AppButton)</pre><p>範例二 : 在 在 plugins 資料夾建立 date-filer.js, 加入下列代碼</p><pre>import Vue from &#39;vue</pre><pre>const months = [<br>  &#39;Janaury&#39;, &#39;February&#39;, &#39;March&#39;, &#39;April&#39;, &#39;May&#39;, &#39;June&#39;,<br>  &#39;July&#39;, &#39;August&#39;, &#39;September&#39;, &#39;October&#39;, &#39;November&#39;, &#39;December&#39;<br>]</pre><pre>const dateFilter = value =&gt; {<br>  return formatData(value)<br>}</pre><pre>function formatData(inputDate) {<br>  const date = new Date(inputDate)<br>  const year = date.getFullYear()<br>  const month = date.getMonth()<br>  const day = date.getDate()<br>  const formattedDate = `${day}. ${months[month] ${year}}`<br>  return formattedDate<br>} </pre><pre>Vue.filter(&#39;date&#39;, dateFilter)</pre><p>在 nuxt.config.js 註冊 &lt;AppButton&gt; 和 dateFilter</p><pre>plugins: [<br>  // 方法一 : 直接引入路徑<br>  &#39;~plugins/core-components.js&#39;,<br>  &#39;~plugins/date-filter.js&#39;</pre><pre>  // 方法二 : 如果方法一行不通, 用方法二<br>  { src: &#39;~plugins/core-components.js&#39;, ssr: false }<br>  { src: &#39;~plugins/date-filter.js&#39;, ssr: false }<br>]</pre><p>組件中直接調用 &lt;AppButton /&gt;, 不需要額外引入</p><p>在日期後面加上 | date 就可以使用 dateFilter</p><ul><li>modules : 設置第三方程式庫</li></ul><p>首先安裝套件, 例如安裝 axios</p><p>在 nuxt.config.js 加入下列程式碼</p><pre>modules: [<br>  &#39;@nuxtjs/axios&#39;<br>],<br>axios: {<br>  baseURL: process.env.BASE_URL || &#39;https://nuxt-blog.firebase.com/&#39;,<br>  credentials: false<br>}</pre><p>在組件中調用 axios 不需額外引入, 直接使用即可, 例如在 methods 屬性中 使用 this.$axios. $get(&#39;/posts/&#39;) 或者在 asyncData 或 nuxtServerInit 中使用 context.app.$axios.$get(‘/posts/’)</p><h4>第 8 節 : The Server Side</h4><p>Adding Server Side Middleware</p><ul><li>在 nuxt.config.js 加入下列代碼</li></ul><pre>const bodyParser = require(&#39;body-parser&#39;)</pre><pre>serverMiddleware = [<br>  bodyParser.json()<br>  &#39;~/api&#39;<br>]</pre><ul><li>在 root directory 下建立 api/index.js</li><li>安裝 express, body-parser : npm i express body-parser</li><li>在 api/index.js 加入下列代碼</li></ul><pre>const express = require(&#39;express&#39;)<br>const router = express.Router()</pre><pre>// 轉換成 expressjs 格式<br>const app = express()<br>router.use((req, res, next) =&gt; {<br>  Object.setPrototypeOf(req, app.request)<br>  Object.setPrototypeOf(res, app.response)<br>  req.res = res<br>  res.req = req<br>  next()<br>})</pre><pre>router.post(&#39;/track-data/&#39;, (req, res) =&gt; {<br>  console.log(&#39;Store data&#39;, req.body.data)<br>  res.status(200).json({ message: &#39;Success!&#39; })<br>})</pre><pre>module.exporst = {<br>  path: &#39;/api&#39;,<br>  handler: router<br>}</pre><h4>Testing the Middleware</h4><h4>asyncData() vs. fetch() vs. nuxtServerInit()</h4><p>Nuxtjs 在組件還沒渲染前, 讀取後端資料, 傳到前端渲染頁面的行為稱為 Server Side Render (SSR) 或者 pre-render, 有 3 種方法可以做到, 分別是 asyncData, fetch 和 nuxtServerInit; asyncData 和 fetch 一樣, 只作用在 pages 資料夾內,</p><h3>參考資料</h3><p>[Nuxt.js : Vue.js on Steroids / Udemy](<a href="https://www.udemy.com/course/nuxtjs-vuejs-on-steroids/">https://www.udemy.com/course/nuxtjs-vuejs-on-steroids/</a>)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=762d17f7302b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[設計師必備能力 : 規劃並設計一商務網站 (Part 2)]]></title>
            <link>https://stephenlaichaowen.medium.com/%E8%A8%AD%E8%A8%88%E5%B8%AB%E5%BF%85%E5%82%99%E8%83%BD%E5%8A%9B-%E8%A6%8F%E5%8A%83%E4%B8%A6%E8%A8%AD%E8%A8%88%E4%B8%80%E5%95%86%E5%8B%99%E7%B6%B2%E7%AB%99-part-2-955216fd3629?source=rss-fb23eeaad5df------2</link>
            <guid isPermaLink="false">https://medium.com/p/955216fd3629</guid>
            <dc:creator><![CDATA[Stephen Lai]]></dc:creator>
            <pubDate>Tue, 21 Apr 2020 05:39:28 GMT</pubDate>
            <atom:updated>2020-04-21T05:39:28.771Z</atom:updated>
            <content:encoded><![CDATA[<h3>設計師必備能力 : 規劃並設計一商務網站 (Part 2)</h3><p>User Story 撰寫示範</p><ul><li>確定你的主題</li><li>搜尋相關的資源</li><li>有沒有餐廳訂位的服務</li><li>這些服務, 有沒有欠缺可以加強的</li></ul><h3>User Story</h3><h4><strong><em>[ 用戶 ]</em></strong></h4><h4>搜尋餐廳</h4><ul><li>分類, 文字搜尋</li><li>可以使用時間, 地點, 人數</li><li>地圖查找</li></ul><h4>餐廳的細節</h4><ul><li>名稱, 評比, 地點 …</li><li>使用地點來提供地圖</li></ul><h4>[ 管理者 ]</h4><h4>省略</h4><h4>網站地圖</h4><ul><li>首頁</li><li>分類, 搜尋頁面</li><li>地圖搜尋</li><li>註冊登入</li><li>用戶個人頁面</li></ul><h4>行動版</h4><ul><li>首頁</li><li>註冊登入 (必須包含表單)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DIGXweIDAf1sfzIYs5KGSw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/943/1*1hrWSu_LLQjQ-LnhaYDn6A.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=955216fd3629" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[設計師必備能力 : 規劃並設計一商務網站 (Part 1)]]></title>
            <link>https://stephenlaichaowen.medium.com/%E8%A8%AD%E8%A8%88%E5%B8%AB%E5%BF%85%E5%82%99%E8%83%BD%E5%8A%9B-%E8%A6%8F%E5%8A%83%E4%B8%A6%E8%A8%AD%E8%A8%88%E4%B8%80%E5%95%86%E5%8B%99%E7%B6%B2%E7%AB%99-part-1-84ebd1d45bed?source=rss-fb23eeaad5df------2</link>
            <guid isPermaLink="false">https://medium.com/p/84ebd1d45bed</guid>
            <dc:creator><![CDATA[Stephen Lai]]></dc:creator>
            <pubDate>Tue, 21 Apr 2020 05:18:34 GMT</pubDate>
            <atom:updated>2020-04-21T05:18:34.167Z</atom:updated>
            <content:encoded><![CDATA[<h3>設計師必備能力 : 規劃並設計一商務網站 (Part 1)</h3><p>透過 User Story、網站地圖、單色稿規劃設計方向</p><h4>如何了解需求</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1c_4V4KO0nUCM6fb3TxLlw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wXdqxyKVe33R0bkahqybbA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4tKBwlM6BWbSLQ8uSBYG_A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a3l9GYGKqqPgzrWgzAxppA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7C_3FBcR5SVqfMnzJWAK5w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VbWqrPaS_kLYihMWKJ7JTA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ND5yE9bErqwBFeXa9FO6Fg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Oq5eBYLD3kDyYSQjduVvWw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WK2Lmn26eoTz5QCgJsSgmw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MxDDSi1XnlyN4Zuxxy-lSA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YU3Mb8EjxTS_UW4nVjkR6Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7vwvW9flFrBgqfwV1Kw2sQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SQeZ6tbLHLJ-2Hw0RH2KUQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KVyBcJfGvCLyy9AQrmecoQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u1UL4QLZlVrfK_wT8YPAbw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1HdymgPWiHQNlQePdJjcEA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AxAcMP2IxY-mt8iYs194tg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*chl9sVUE2POe4xFaUN8Qlg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jXlodcoVUzZ0XI9TR-uuvw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RZEtH08KMoMdPGSOsFtiqA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mmJfVkouPv68QlyZDRTuOQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hoqhG4zRbc7dVy5mTB93-w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eHmkyX0H01ZoueFbK1UHNA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*99bF5wew_XYZ_bTaQkUDYQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*go5ONHUSxk1XPMrcxZMRcw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84ebd1d45bed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Svelte : works with Bootstrap]]></title>
            <link>https://stephenlaichaowen.medium.com/svelte-works-with-bootstrap-31c8d225c6c5?source=rss-fb23eeaad5df------2</link>
            <guid isPermaLink="false">https://medium.com/p/31c8d225c6c5</guid>
            <category><![CDATA[sapper]]></category>
            <category><![CDATA[bootstrap]]></category>
            <category><![CDATA[getbootstrap]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[svelte]]></category>
            <dc:creator><![CDATA[Stephen Lai]]></dc:creator>
            <pubDate>Sun, 19 Apr 2020 04:06:51 GMT</pubDate>
            <atom:updated>2020-04-19T05:13:13.390Z</atom:updated>
            <content:encoded><![CDATA[<h3>Svelte : works with Bootstrap</h3><blockquote><strong>Use UI library makes our life much easier while we developing user interface, save tons of time for prototyping !!!</strong></blockquote><p>It makes your development much easier if you install ui library like bootstrap in your sapper project.</p><ul><li>First download sapper template to your local drive - npx degit &quot;svelte/sapper-template#webpack&quot; my-app</li><li>Then install bootstrap library - npm i bootstrap</li><li>Copy node_modules/bootstrap/dist/css/bootstrap.min.css and paste under static directory</li><li>Then include it in template.html &lt;link href=&quot;bootstrap.min.css&quot; rel=&quot;style&quot; /&gt;</li></ul><p>Now we are good to to. To test it, we go to getbootstrap.com copy a form component and paste into our index.svelte file.</p><pre>&lt;form&gt;<br>  &lt;div class=&quot;form-group&quot;&gt;<br>    &lt;label for=&quot;exampleInputEmail1&quot;&gt;Email address&lt;/label&gt;<br>    &lt;input type=&quot;email&quot; class=&quot;form-control&quot; id=&quot;exampleInputEmail1&quot; aria-describedby=&quot;emailHelp&quot;&gt;<br>    &lt;small id=&quot;emailHelp&quot; class=&quot;form-text text-muted&quot;&gt;We&#39;ll never share your email with anyone else.&lt;/small&gt;<br>  &lt;/div&gt;<br>  &lt;div class=&quot;form-group&quot;&gt;<br>    &lt;label for=&quot;exampleInputPassword1&quot;&gt;Password&lt;/label&gt;<br>    &lt;input type=&quot;password&quot; class=&quot;form-control&quot; id=&quot;exampleInputPassword1&quot;&gt;<br>  &lt;/div&gt;<br>  &lt;div class=&quot;form-group form-check&quot;&gt;<br>    &lt;input type=&quot;checkbox&quot; class=&quot;form-check-input&quot; id=&quot;exampleCheck1&quot;&gt;<br>    &lt;label class=&quot;form-check-label&quot; for=&quot;exampleCheck1&quot;&gt;Check me out&lt;/label&gt;<br>  &lt;/div&gt;<br>  &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;/button&gt;<br>&lt;/form&gt;</pre><h4>The results</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*o1xVf682x88Ne9TKOIHB3A.png" /></figure><p>Since bootstrap.min.css takes about 155KB, which is kinda big, we can remove unused css with purgecss</p><ul><li>First we install purgecss - npm i purgecss -D</li><li>Then we create a dist foler under static directory</li><li>In package.json we add scripts to execute purge command</li></ul><pre>// the command targets all .svelte files under `src` directory and remove all unused css, rebuild the `bootstrap.min.css` and put the production build under `src/dist` directory</pre><pre>&quot;scripts&quot;: {<br>  <em>&quot;purgecss&quot;</em>: &quot;purgecss --css static/bootstrap.min.css --content src/*.svelte --output static/dist&quot;<br>}</pre><ul><li>Finally in src/template.html we include the build css file</li></ul><pre>&lt;head&gt;<br>  &lt;!-- for development --&gt;<br>  &lt;link ref=&quot;bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;</pre><pre>  &lt;!-- for production --&gt;<br>  &lt;link ref=&quot;dist/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;  <br>&lt;/head&gt;</pre><p>If we want a production build we run npm run purgecss to generate a purged bootstrap.min.css file under static/dist directory. During development the bootstrap.min.css we use is under static directory !</p><p><strong><em>And now we are ready to get our project up and running !!!</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31c8d225c6c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Svelte : fetch image source]]></title>
            <link>https://stephenlaichaowen.medium.com/svelte-fetch-image-source-e5efe70d2f0d?source=rss-fb23eeaad5df------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5efe70d2f0d</guid>
            <category><![CDATA[svelte]]></category>
            <category><![CDATA[fetch]]></category>
            <category><![CDATA[repl]]></category>
            <category><![CDATA[random-image]]></category>
            <category><![CDATA[online-editor]]></category>
            <dc:creator><![CDATA[Stephen Lai]]></dc:creator>
            <pubDate>Sun, 19 Apr 2020 02:37:11 GMT</pubDate>
            <atom:updated>2020-04-19T02:47:00.043Z</atom:updated>
            <content:encoded><![CDATA[<h3>Svelte : fetch image source</h3><p>Svelte has an online editor so that you don’t need to open VSCode to develop your project which is pretty handy. Today we are gonna get random image from picsum.photos and display in our page.</p><p>First create RandomImage.svelte which is our child component. In script section we declare src , alt variables for img tag. Then we use fetch() api to get the image source</p><pre>&lt;script&gt;<br>  let src = &#39;is loading...&#39;<br>  let alt = &#39;Random Image&#39;<br>  let API = &#39;<a href="https://picsum.photos/300&#39;">https://picsum.photos/300&#39;</a></pre><pre>  // use promise to get image source with fetch api<br>  fetch(API).then(res =&gt; res.url)</pre><pre>  // If you prefer async/await method, you can do that also<br>  async function loadImage() {<br>    const res = await fetch(API)<br>    src = await res.url<br>  } </pre><pre>  loadImage()</pre><pre>&lt;/script&gt;</pre><p>In html section we put a h3 tag indicating the image source and display the image with img tag</p><pre>&lt;h3&gt;<br> The src is {src}<br>&lt;/h3&gt;</pre><pre>// If attribute and value are the same name, we can use shorthand expression like {src} {alt}<br>&lt;img {src} {alt} /&gt;</pre><p>In our App.svelte , we import RandomImage.svelte and display the image with RandomImage component. We use display:gridto make the layout look organized</p><pre>&lt;script&gt;<br> import RandomImage from &#39;./RandomImage.svelte&#39;<br>&lt;/script&gt;</pre><pre>&lt;!-- we can display as many images as we want --&gt;<br>&lt;RandomImage /&gt;<br>&lt;RandomImage /&gt;<br>&lt;RandomImage /&gt;</pre><pre>&lt;style&gt;<br>  .box {<br>    <em>display</em>: grid;<br>    <em>grid-template-columns</em>: repeat(2, 1fr);<br>  }<br>&lt;/style&gt;</pre><h4>The results</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cjHllRCKAaHPio1zHxY-yA.png" /></figure><p>And that’s it. You can also download the project to your local drive and start the local server to run and test it, ain’t it amazing !!!</p><blockquote><strong>Svelte Rocks !!!</strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5efe70d2f0d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>