Tsuyoshi Higuchi
    • 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: Vueハンズオンの前の基礎の基礎@Kamakura MokMok Hack tags: KamakuraDev, Kamakura MokMok hack, handson, react, vue, basic description: View the slide with "Slide Mode". --- <!-- .slide: data-background="#020202" --> ### Vueハンズオンの前の基礎の基礎 in [Kamakura MokMok Hack](https://kamkura-mokmok.connpass.com/) [@tyshgc](https://twitter.com/tyshgc) <!-- Put the link to this slide here so people can follow --> <span style="font-size: 0.3em;">slide: https://hackmd.io/@tyshgc/kamakura-mokmok-vue-react-handson/</span> --- Vue.js? --- Vue.jsはWebフロントエンドの<br />フレームワークの一つ --- 簡単に言うと… <span style="font-size: 0.8em;">データの<strong style="color:#cc3300;">表示</strong>と<strong style="color:#cc3300;">入力</strong>を<br />うまいことやってくれるもの</span> --- ### Vueの基本のキ --- <p style="font-size:0.8em;">Webアプリケーションなので<br />HTMLの所定のDOM内にアプリケーションを<br />構築していきます。</p> ```html= <div id="app"> <!-- ここにアプリを構築する --> </div> ``` --- <p style="font-size:0.8em;">Vueインスタンスを作成</p> ```javascript= new Vue({ el: "#app", //HTMLのどこに表示するか data() { // メッセージのデータ return { message: "御用でしょうか?" }; }, methods: { // 入力処理 onCancel() { this.message = "かしこまりー!!"; } }, template: ` <div> <p>{{message}}</p> <button @click="onCancel">用はない</button> </div> ` }); ``` --- <iframe src="https://codesandbox.io/embed/vuenoshuomingyongsono1-i3pm1?fontsize=14" title="Vueの説明用その1" allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe> --- しかし… <span style="font-size: 0.8em;">templateにアプリケーションの構造を全部書くのはツライ…</span> ```javascript= template: ` <div> <p>{{message}}</p> <button @click="onCancel">用はない</button> </div> ` ``` --- ## コンポーネントを使おう! --- コンポーネントとは? <span style="font-size: 0.8em;">👉アプリを構成する部品</span> --- 🧩御用聞きのメッセージ ```html= <p>{{message}}</p> ``` 🧩返事をするボタン ```html= <button @click="onCancel">用はない</button> ``` --- それぞれ部品 = コンポーネントとして扱えばいい --- 🧩御用聞きのメッセージ Servant.vue を作成 ```javascript= <template> <p>{{message}}</p> </template> <script> export default { name: "Servant", // コンポーネント名 props: { // このコンポーネントに渡すことが可能な値 message: String } } </script> ``` --- Vueインスタンスにコンポーネントを登録する ```javascript= import Servant from "./Servant.vue" new Vue({ el: "#app", //HTMLのどこに表示するか //...<同じなので省略>... components: { //コンポーネントを登録 Servant }, template: ` <div> <servant :message="message" /> <button @click="onCancel">用はないよ</button> </div> ` }); ``` --- 🧩キャンセルボタン CancelButton.vue を作成 ```javascript= <template> <button @click="onCommand">用はないよ</button> </template> <script> export default { name: "CancelButton", methods: { onCommand() { // $emitで呼び出し元のイベントを登録して // ここで発火できる this.$emit("do-command"); } } }; </script> ``` --- Vueインスタンスにコンポーネントを登録する ```javascript= //...<同じなので省略>... import CancelButton from "./CancelButton.vue" new Vue({ el: "#app", //HTMLのどこに表示するか //...<同じなので省略>... components: { //コンポーネントを登録 Servant, CancelButton }, template: ` <div> <servant :message="message" /> <cancel-button @do-command="onCancel" /> </div> ` }); ``` --- <iframe src="https://codesandbox.io/embed/vuenoshuomingyongsono2-uubof?fontsize=14" title="Vueの説明用その2" allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe> --- 御用聞きが文章だけだと寂しい…<br /> 絵文字で工夫してみましょう! --- 御用聞きのシーンごとに<br />絵文字をコンポーネントにする |シーン|絵文字| |---|---|---| |用件をたずねる時|💁‍♀️| |承知した時|🙆‍♀️| --- Emoji.vue ```javascript= <template> <i v-if="isAsk">💁‍♀️</i> <i v-else>🙆‍♀️</i> </template> <script> export default { name: "Ask", props: { isAsk: Boolean, isOk: Boolean } }; </script> ``` --- ### Point 🌟 |ディレクティブ|役割| |---|---| |v-if|<span style="font-size: 0.5em;">値の真偽を見て条件を満たした場合DOMを表示</span>| |v-else|<span style="font-size: 0.5em;">ifの条件を満たさなかった場合にDOMを表示</span>| |v-else-if|<span style="font-size: 0.5em;">別の条件の場合</span>| |v-for|<span style="font-size: 0.5em;">繰り返し</span>| --- 詳しくは公式ドキュメントを読もう [Vue 2.0 ドキュメント - ディレクティブ](https://jp.vuejs.org/v2/api/#%E3%83%87%E3%82%A3%E3%83%AC%E3%82%AF%E3%83%86%E3%82%A3%E3%83%96) --- 御用聞きのメッセージに絵文字を<br />差し込めるようにします。 ```javascript= new Vue({ el: "#app", //HTMLのどこに表示するか data() { return { message: "御用でしょうか?", isAsk: true //用件をたずねているか? }; }, methods: { // 入力処理 onCancel() { this.message = "かしこまりー!!"; this.isAsk = false; } }, components: { //コンポーネントを登録 //...<同じなので省略>... Emoji, }, template: ` <div> <servant :message="message"> <emoji :is-ask="isAsk" /> </servant> <cancel-button @do-command="onCancel" /> </div> ` }); ``` --- :exclamation: コンポーネントを入れ子にすると… ```html= <servant><Component></servant> ``` このままではエラーになる --- Servant.vueをこうする ```javascript= <template> <p> <slot /> <span>{{message}}</span> </p> </template> ``` --- ### ここまでで気になる点 - <span style="font-size: 0.5em;">用件たずねているか否かをisAskで現したプロパティが適していないのでは?</span> - <span style="font-size: 0.5em;">正確には「用件をたずねる」「了解をする」というモードであるべき</span> - <span style="font-size: 0.5em;">アプリケーション内では御用聞き〈Servant〉のモードを表現した方がわかりやすい</span> --- Pure JavascriptではEnum使えないので… ぽいものをつくってみる ```javascript= export const SERVANT_MODE = { ASK: Symbol(), OK: Symbol() }; ``` それを踏まえてなやんかんやいじって… --- <iframe src="https://codesandbox.io/embed/vuenoshuomingyongsono3-fi4t5?fontsize=14" title="Vueの説明用その4" allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe> --- ### ReactとVueの違いは? - <span style="font-size: 0.5em;">ReactはViewライブラリ</span> - <span style="font-size: 0.5em;">ざっくり言うとコンポーネントの考え方は基本的に同じ</span> - <span style="font-size: 0.5em;">Reactのコンポーネントにもpropsで値が渡せる</span> - <span style="font-size: 0.5em;">VueのdataがReactのstateと理解すれば良い(正確には仕組みが違うけど…)</span> - <span style="font-size: 0.5em;">Vueは子から親へのデータ参照やイベント発火が可能</span> - <span style="font-size: 0.5em;">Reactは親から子への一方向(イベントもpropsで渡す)</span> --- ### 状態管理を扱う ==状態(=State)を一箇所に集中させて管理== --- ### メリット - <span style="font-size: 0.5em;">複数のインスタンスやコンポーネント間でStateを共有できる</span> - <span style="font-size: 0.5em;">アプリケーションが大きくなると表示・状態・入力など役割を分けた方が扱いやすい</span> --- ### デメリット - <span style="font-size: 0.5em;">インスタンスが一つ、扱うアプリケーションが小さい場合は冗長的になる</span> - <span style="font-size: 0.5em;">コンポーネントのどこでも同じ状態を扱えるので書き方次第で変更範囲が広くなりバグの温床に…</span> --- さっきのコードをVuexにしてみる <span style="font-size: 0.5em;">この規模だと逆に冗長的になので本当は使わない</span> --- <iframe src="https://codesandbox.io/embed/vuenoshuomingyongsono5-r4ntk?fontsize=14" title="Vueの説明用その5" allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe> --- ### Reactの場合は? - <span style="font-size: 0.5em;">Reactの場合はReduxで状態管理できる。ただしこちらはもっと冗長的。</span> - <span style="font-size: 0.5em;">そのほかにMobxやUnduxなどある。</span> - MobXが比較的Vuexと似ている。 --- ## ハンズオンする --- ### つくるもの 簡単なストップウォッチアプリを作ります。 基礎の基礎のさらにつっこんだ基礎の部分や状態管理Vuexまでできればがんばりましょう! --- ### リポジトリ https://github.com/kamakura-mok-mok-hack/vue-handson-stopwatch

    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