林鏡棠
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: 工作上的點點滴滴 --- # 前端初期架構規劃 > 這邊想來紀錄一下自己第一次規劃架構的經驗當作一個里程碑。在專案規劃的前期有很多需要考量的地方,但礙於自己沒有真的規劃過一個大型專案,所以只能先參考大神們的文章來思考哪些東西是有加入的必要。 ### 專案構思 思路流程: 1. 是否需要 SEO 2. 打包效率 3. 編碼檢查工具、自動編排工具 4. CSS 框架與 UI 框架的選擇 5. 有沒有需要將 Sass 模組化 ### 專案所需功能 - i18 多國語系切換 => [後續優化](https://hackmd.io/kC-5K27NSrWTu00SY427rQ) - 提升打包效率 - Vite - coding style 檢查工具(<span class="red">Eslint</span>)、自動編排工具(<span class="red">prettier</span>) - 路由控制器 - Vuex 作為狀態管理及應用程序中所有組件的集中存儲區 => 這邊後續優化更改成使用 Pinia 套件,因為 Vuex 在使用變數內容時要打的 code 太多了(<span class="red">store.state.isLoading</span>, 很長一串XD) - API 路徑 module - UI 框架 - <span class="red">Element^+^</span> - CSS 框架 - <span class="red">Tailwind</span> - 單元測試套件 - Jest ([踩坑心得分享](https://hackmd.io/uHyz0jaMTXaJT19LwOBrxw)) - Sass 模組化 - mock API - 全域的錯誤處理([middleware 異常狀態處理](https://hackmd.io/UCtXtiRfQJOfjA0Sx1K6TA)) ### Vite + Vue3 [Vite 官方中文文檔](https://cn.vitejs.dev/guide/#scaffolding-your-first-vite-project) 安裝指令: ``` npm create vite@latest my-vue-app -- --template vue ``` ### eslint + prettier 配置 [一篇文章搞定vite+vue3+eslint+prettier](https://juejin.cn/post/7020653715363217445) [eslint 和 prettier 配置文件不能是 .js 文件,必须是.cjs 文件怎么回事 ?](https://segmentfault.com/q/1010000042298464) :::warning 上述的配置問題因為 vite 新版在 package.json 裡面會配置 type: "module", 於是 .js 被默認為使用了 ES module 規範,如果自動生成的配置文件使用了 CommonJS,就會報錯。.cjs 的檔案會告诉 node.js 它使用了 CommonJS 規範,所以就不會報錯。 ::: :::danger 解決方法: 1. 檔案名稱更改為 *.cjs 2. 必須使用 module.exports 來匯出設定檔 3. 必須至擴充設置那邊將檔案名稱一併調整為 *.cjs, 不然會一直報「找不到檔案」的錯誤 ::: ```javascript= // .prettierrc 檔案 { /* * 這邊將 semi 改為 true,eslint 會跳 warning。 * 後續將 Vscode 的 Vetur 關掉之後,改用 Volar 擴充元件就沒問題 */ "semi": true, "singleQuote": true, "endOfLine": "auto", "printWidth": 100 } ``` ### Router [使用Vite构建Vue3项目,对路由Vue Router 4.x的设置](https://blog.csdn.net/xjtarzan/article/details/119736309) :::warning 在這篇文章中,讓我覺得有趣的地方是**步驟4的 alias 設置**,讓我了解了以往我們為什麼用 @ 這個符號,它會直接編譯為 src 資料夾的路徑。 原來是因為有特別針對這個符號去做一些路徑的處理 ::: [Router 集成設置參考](https://www.cnblogs.com/liuqin-always/p/14690044.html) 透過這篇文章了解到如何將路由模組化,範例如下: :::spoiler 路由模組化代碼 ```javascript= // modules/home.js export default { path: '/', name: 'index', title: '首頁', component: () => import('@/views/HomeView.vue'), }; ``` ```javascript= // routes.js import home from './modules/home'; const routes = [home]; export default routes; ``` ```javascript= // index.js import { createRouter, createWebHashHistory } from 'vue-router'; import routes from './routes'; const router = createRouter({ history: createWebHashHistory(), routes, }); // 這邊的想法是: // 1. 有 token 的情況下,只要路由想要到 login 都會被導回首頁。 // 2. 沒有 token 的情況下,只要不是 login 路由,都會自動導回到 login router.beforeEach((to, from) => { const access = Cookies.get('token'); if (access && to.path === '/login') { return { name: 'HomeView' }; } else if (!access && to.path !== '/login') { return { name: 'login' }; } }); export default router; ``` ::: #### 步驟流程: 1. 在 modules 裡面定義各頁面路由 2. 接著透過 routes.js 導入 3. 最後由 index.js 去建立一個 Router 實體,並把它匯出 4. 方便於 main.js 去引用。 ![](https://i.imgur.com/L9rrm7b.png) [路由的後續優化](https://hackmd.io/XGnLB8CfTwi8UAMRvJOPsg) ### Tailwind CSS [Install Tailwind CSS with Vue 3 and Vite](https://tailwindcss.com/docs/guides/vite) ### Vuex [Vue3 的資料狀態管理,provide / inject、vuex、props](https://penueling.com/%E6%8A%80%E8%A1%93%E7%AD%86%E8%A8%98/vue3-%E7%9A%84%E8%B3%87%E6%96%99%E7%8B%80%E6%85%8B%E7%AE%A1%E7%90%86%EF%BC%8Cprovide-inject%E3%80%81vuex/) => 參考 Vuex 集成設置 :::spoiler Vuex 相關代碼 ```javascript= // modules/auth.js export const state = { user: JSON.parse(sessionStorage.getItem('user')), password: '123', }; export const actions = {}; export const mutations = { setUser(state, payload) { sessionStorage.setItem('user', JSON.stringify(payload)); state.user = payload; }, }; export const getters = { isAuthenticated: (state) => !!state.user || !!sessionStorage.getItem('user'), }; export default { state, actions, mutations, getters, namespaced: true, }; ``` ```javascript= import { createStore } from 'vuex'; import auth from './modules/auth'; // Create a new store instance. export default createStore({ modules: { auth }, }); ``` ::: ![](https://i.imgur.com/9CLprIF.png) ### axios 集成設置 - [從 0 開始手把手帶你搭建一套規範的 Vue3 項目工程環境](https://www.readfog.com/a/1635089631515086848) ==這裡只有先寫一些註解讓大家清楚每個區塊所負責的功能是什麼,這樣未來如果要擴充或是新增功能的話,比較知道在哪個地方添加。== ```javascript= // api/axios.js import Axios from 'axios'; import { ElMessage, ElLoading } from 'element-plus'; const baseURL = 'https://desolate-gorge-38580.herokuapp.com'; const service = Axios.create({ baseURL, timeout: 20000, // 請求超時 20s }); service.interceptors.request.use( config => { // 發送請求前會做的事情 // 這邊目前想到2件事可以先做: // 1. 加入全域的 loading 效果在每次發請求前 // 2. 將 token 帶入 config.headers.Authorization 裏面 const loadingInstance = ElLoading.service({ lock: true, text: 'Loading', background: 'rgba(0, 0, 0, 0.7)' }); const token = Cookies.get('token'); if (token) { config.headers.common.Authorization = `Bearer ${token}`; loadingInstance.close(); } return config; }, (error) => { // 請求錯誤要做的事情 return Promise.reject(error); } ); // 後置攔截器(獲取到響應時的攔截) service.interceptors.response.use( // 狀態碼為 2xx 時觸發的 function (response) => { return response; }, // 狀態碼非 2xx 時觸發的 function (error) => { if (error.response && error.response.data) { const code = error.response.status; const msg = error.response.data.message; ElMessage.error(`Code: ${code}, Message: ${msg}`); console.error(`[Axios Error]`, error.response); } else { ElMessage.error(`${error}`); } return Promise.reject(error); } ); export default service; ``` - [Vue3.x中集成axios的方法](https://shawnzhou.world/2021/07/28/vue3-axios/) ```javascript= // api/home.js import service from './axios'; export function successTest() { return service({ url: '/posts', method: 'get', }); } export function errorTest() { return service({ url: '/rooms', method: 'get', }); } ``` ```javascript= // 這邊在 Vite 的文件配置失效,所以後續在各個 .vue 檔案引入 api 時 // 就直接沿用前面 Vite.config 裡面所設置的 alias proxy: { "/desktop": { target: "http://你的ip:你的端口/", changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, ""), }, }, ``` ```javascript= // views/HomeView.vue import { successTest, errorTest } from '@/api/home'; ``` ![](https://i.imgur.com/YY3DPSp.png) ### i18n [i18n什麼的交給前端來處理吧](https://ithelp.ithome.com.tw/articles/10262689) [將 i18n Global](https://blog.flycode.com/step-by-step-how-to-create-a-vue-multi-language-app-with-vue-i18n) ```javascript= export default createI18n({ legacy: false, globalInjection: true, locale: process.env.VUE_APP_I18N_LOCALE || "en", fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || "en", messages: loadLocaleMessages(), }); ``` > 需要添加 globalInjection: true,然後系統會自動將其全域化,透過 $t 就可以獲得內容。 ```htmlmixed= <!-- template 獲取 i18n value 方式 --> <template> <h1>{{ $t('title') }}</h1> <p>{{ $t('description') }}</p> </template> ``` ==這邊紀錄一下,後續可能會需要處理關於切換語系後,畫面需要 reload 的部分== ### 最終資料夾內容呈現樣貌 ![](https://i.imgur.com/fuS7z6Q.png) <style> .red{ color: red } </style>

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully