grayshine
    • 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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # <font class="h2">ES模組Module</font> ###### tags: `Vue3` <style> .h2 { background: linear-gradient(135deg,#fff,#537479) ; color: #537479; display:block; padding: 6px 5px; border-radius: 4px; } .h3 { background: linear-gradient(180deg,#fff 50%,#c9d5d4) ; color: #537479; display:block; padding: 6px 5px; border-bottom: 3px solid #537479; } .h4 { color: #537479; font-weight:bold; font-size:1.1em; } </style> ### <font class="h3">ES module的import、commonJS的require</font> :::info **CommonJS:** CommonJS(建立模組的約定) 代表的僅僅只是 require 和 export 這樣宣告模組的語法 ::: ![](https://i.imgur.com/CeB94N3.png =600x) <br><br><br><br> :::info 單一檔案程式碼不要太長(200,至多不要出過300) 於是會使用元件、函式庫、模組 ::: <br> ![](https://i.imgur.com/rjVWP8E.png) <br><br><br><br><br><br> 每一個`<script type="module">`的==作用域都是獨立的== <br><br> ### <font class="h3">「預設」匯入(html),匯出(javascript)</font> :::info 常見的匯出方式,通常用於匯出物件,在Vue開發中可用來匯出元件 ==一個檔案能有多個具名匯出== ==一個檔案只能有一個預設匯出== ::: ![](https://i.imgur.com/Xlfw4Vw.png) <br><br><br> **html檔案:** ![](https://i.imgur.com/oG9VXew.png) <br><br> **javascript檔案:** ![](https://i.imgur.com/D40rYcF.png) <br><br><br><br> ### <font class="h3">「具名」匯入(html),匯出(javascript)</font> 匯出的同時就必須宣告一個名稱 :::info 可用於匯出已定義的變數、物件、函式,專案開發中通常用於"方法匯出" 第三方的框架、函式、套件很常使用具名定義"方法" ::: ### <font class="h4">➤單一匯入(建議寫法)</font> ==解構方式匯入== **html檔案:** ```htmlembedded <script type="module"> import { a, b } from './export2.js'; console.log(a) //1 b(); //1 </script> ``` <br><br> **javascript檔案:** ```javascript export const a = 1; export function b(){ console.log(1) } export function c(a,b){ return a+b; } ``` <br><br><br><br> ### <font class="h4">➤全部匯入(不建議,錯誤較難發現)</font> html檔案 ```htmlembedded <script type="module"> import * as all from './export2.js'; console.log(all.a) //1 all.b() //1 console.log(all.c(1,2)) //3 </script> ``` <br><br> javascript檔案 ```javascript export const a = 1; export function b(){ console.log(1) } export function c(a,b){ return a+b; } ``` <br><br><br><br> ### <font class="h3">sideEffect.js(較舊)</font> :::info sideEffect.js檔案並沒進行匯出,但是它可以直接進行匯入,而且匯入之後會立即執行。在舊的library常見的方法。 ::: html檔案 ```htmlembedded <script type="module"> import './sideEffect.js'; console.log($) //我是jQuery </script> ``` <br><br> javascript檔案 ```javascript (function (global){ global.$ = '我是jQuery' })(window); ``` 內容為立即函式 <br><br><br><br> ### <font class="h3">範例一</font> **html檔案:** ```htmlembedded <body> <script src="./js/index.js" type="module"></script> </body> ``` <br> **javascript檔案:** index.js檔案: ```javascript import config from './config.js' ``` config.js檔案: ```javascript export default { 'tdxClientId': 'xxxxxxxx-xxxxxxxx-xxxx-xxxx', 'tdxClientSecret': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx' } ``` <br><br><br><br> ### <font class="h3">範例二</font> text.js檔案: ```javascript export const deviceName = 'iPhone' export const mobilesOnSale = ['Samsung', 'Apple', 'Huawei'] export const offers = { priceCurrency: 'TWD', price: '26,900', } export const logPrice = (price) => { console.log('price: ', price) } export function logDeviceName(deviceName) { console.log(deviceName) } ``` index.js檔案: ```javascript import { deviceName, mobilesOnSale, offers, logPrice, logDeviceName } from './text.js' console.log(deviceName) //iPhone console.log(mobilesOnSale) //['Samsung', 'Apple', 'Huawei'] console.log(offers) //{priceCurrency: 'TWD',price: '26,900'} console.log(logPrice(600)) //price: 600 console.log(logDeviceName('galaxy')) //galaxy ``` <br><br><br><br><br><br><br><br><br><br><br><br> ### <font class="h3">ESM方式載入Vue</font> 網路上找到的「ESM」,如果條件允許是可以使用import方式載入 <br><br> ### <font class="h4">Vue的ESM</font> https://cdnjs.com/libraries/vue ![](https://i.imgur.com/PQoyBH0.png) <br><br><br><br> 比對語法:https://v3.vuejs.org/guide/introduction.html#getting-started ![](https://i.imgur.com/EvafADx.png) <br><br><br><br> ### <font class="h4">➤範例:</font> ```htmlembedded <body> <div id="app">{{counter}}</div> </body> ``` ![](https://i.imgur.com/JZx88ff.png) <br><br><br><br> ### 加入Vue的ESM ```htmlembedded <body> <div id="app">{{counter}}</div> <script type="module"> import { createApp } from 'https://cdnjs.cloudflare.com/ajax/libs/vue/3.2.26/vue.esm-browser.min.js'; const Counter = { data() { return { counter: 0 } } } createApp(Counter).mount('#app') </script> </body> ``` ![](https://i.imgur.com/V7Y091v.png) <br><br><br><br> 此時就可以更改數值了 ```htmlembedded <body> <div id="app">{{counter}}</div> <script type="module"> import { createApp } from 'https://cdnjs.cloudflare.com/ajax/libs/vue/3.2.26/vue.esm-browser.min.js'; const Counter = { data() { return { counter: 5 } } } createApp(Counter).mount('#app') </script> </body> ``` ![](https://i.imgur.com/4fpcEUT.png) <br><br><br><br> --- [[ES6] Javascript Import & Export 教學 | Modules | 模組化](https://www.youtube.com/watch?v=pbVlFc0bTt8&ab_channel=BigBoyCanCode) [用十分鐘瞭解JavaScript的模組 -- 《還有關於npm套件管理的那些事情》](https://www.youtube.com/watch?v=7QNi-XJq4bU&ab_channel=%E9%99%B3%E9%8D%BE%E8%AA%A0) [[ 想入門,我陪你 ] Re Vue 重頭說起|Day 9:基礎模組化](https://www.youtube.com/watch?v=KQRfZ07bfAQ&ab_channel=Alex%E5%AE%85%E5%B9%B9%E5%98%9B)

    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