01_Vue.js_Intro

01_Vue.js_Intro

Start

<script src="<https://cdn.jsdelivr.net/npm/vue/dist/vue.js>"></script>

Intro

  • el ( Vue ์˜ ์ธ์Šคํ„ด์Šค๋กœ ์–ด๋Š ์š”์†Œ์— ๋งˆ์šดํŠธํ• ์ง€ ๊ฒฐ์ • )

    <body>
        <div id='app' >  <!-- ํ•˜๋‚˜์˜ ๋ถ€๋ชจ์š”์†Œ๋ฅผ ๊ฐ–์Œ -->
    
        </div>
    
        <script src="<https://cdn.jsdelivr.net/npm/vue/dist/vue.js>"></script>
    
        <script>
            // el ์€ Vue ์ธ์Šคํ„ด์Šค์˜ ์†์„ฑ์œผ๋กœ ์–ด๋–ค ์š”์†Œ์— ๋งˆ์šดํŠธํ• ์ง€ ๊ฒฐ์ •
             const app = new Vue ({
                el: '#app', 
             })
             console.log(app) => Vue
             console.log(app.$el) => <div id="app"></div>
        </script>
    
    </body>
  • data ( ํ˜ธ์ถœ ์‹œ, app.data.message ( X ), app.message ( O ) )

    <body>
        <div id="app">
        </div>
    
        <script src="<https://cdn.jsdelivr.net/npm/vue/dist/vue.js>"></script>
        <script>
            const app = new Vue({
                el: '#app', //์–ด๋–ค ์š”์†Œ์— ๋งˆ์šดํŠธํ• ์ง€
                data: {    // MVVM => Model ์ด๋‹ค. data ๊ฐ€ ํ•ต์‹ฌ!
                    message: 'Hello Vue'
                },
            })
            console.log(app.message) => Hello Vue
    
        </script>
    </body>
  • interpolation ( ) ๋ณด๊ฐ„๋ฒ•

    <body>
        <div id="app">
            {{ message }} 
        </div>
    
        <script src="<https://cdn.jsdelivr.net/npm/vue/dist/vue.js>"></script>
        <script>
            const app = new Vue ({
                el: '#app',
                data: {
                    message : 'Hello Vue',
                }
            })
    
        </script> 
    
    </body>
  • v-text / if / if-elseif-else / for / bind ( v- ์ ‘๋‘์‚ฌ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋””๋ ‰ํ‹ฐ๋ธŒ ( ๋ช…๋ นํ•˜๋Š” ๊ฒƒ ) )

    <body>
    
        <div id="app">
            text, if, if-elseif-else, for ๊นŒ์ง€๋Š” ์‰ฌ์šฐ๋‹ˆ ํŒจ์Šค
                    <a href="{{ googleUrl }}">Bad Google link</a>
            <!-- v-bind: ํ‘œ์ค€ HTML ์†์„ฑ๊ณผ Vue ์ธ์Šคํ„ด์Šค๋ฅผ ์—ฐ๋™ํ•  ๋•Œ. (+a) -->
            <a v-bind:href="googleUrl">Good Google link</a>
            <a href="{{ naverUrl }}">Bad Naver link</a>
            <a v-bind:href="naverUrl">Good Naver link</a>
            <img v-bind:src="randomImageUrl" v-bind:alt="altText">
        </div>
    
        <script src="<https://cdn.jsdelivr.net/npm/vue/dist/vue.js>"></script>
        <script>
            const app = new Vue ({
                el: '#app',
                data: {
                                    googleUrl: '<https://google.com>',
                    naverUrl: '<https://naver.com>',
                    randomImageUrl: '<https://picsum.photos/200>',
                }})
        </script>
    </body>

v-bind: : : ์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

  • method / v-on

    <div id="app">
            {{ message }}
        </div>
        <script src="<https://cdn.jsdelivr.net/npm/vue/dist/vue.js>"></script>
        <script>
            const app = new Vue({
                el: '#app',
                data: { // ์ธ์Šคํ„ด์Šค
                    message: 'Hello Vue'
                },
                methods: { // ํ•จ์ˆ˜๋“ค์˜ ์ง‘ํ•ฉ, data๋ฅผ ๊ฐ€์ง€๊ณ  ์›€์ง์ผ ์ˆ˜ ์žˆ๊ฒŒ ๋จ
                    alertWarning: function () {
                        alert('WARNING')
                    },
                    alertMessage () { // Syntactic Sugar: ์œ„์™€ ์•„๋ž˜ ์™„์ „ํžˆ ๊ฐ™์Œ
                        alert(this.message)
                    },
                    changeMessage () {
                        this.message = 'CHANGED MESSAGE'
                    },
                }
            })
        </script>
    </body>

    ```jsx

        <button v-on:click="alertWarning">Alert Warning</button>
        <button v-on:click="alertMessage">Alert Message</button>
        <button v-on:click="changeMessage">Change Message</button>
        <hr>
        <input @keyup.enter="onInputChange" type="text" name="" id="">
    </div>
    
    <script src="<https://cdn.jsdelivr.net/npm/vue/dist/vue.js>"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                message: 'Hello Vue'
            },
            methods: {
                alertWarning: function () {
                    alert('WARNING')
                },
                alertMessage () {
                    alert(this.message)
                },
                changeMessage() {
                    this.message = 'CHANGED MESSAGE'
                },
                onInputChange(event) {
                    // if (event.key == "Enter") {
                        this.message = event.target.value
                    },
    
            }
        })
    
    </script>

</body>

`v-on:`  : `@` ์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

- v-model

  ```jsx
  <body>
      <div id="app">
          <h1>{{ message }}</h1>
          <!-- ์‚ฌ์šฉ์ž ์ž…๋ ฅ <=> data ๋ฅผ ์™„์ „ํžˆ ๋™๊ธฐํ™” ์‹œํ‚ค๊ณ  ์‹ถ๋‹ค -->
          <!-- v-model => input, select, textarea ์— ์–‘๋ฐฉํ–ฅ ๋ฐ”์ธ๋”ฉ -->

          <hr>
          <!-- ๋‹จ๋ฐฉํ–ฅ ๋ฐ”์ธ๋”ฉ (input => data) -->
          1way:
          <input v-on:keyup="onInputChange" type="text">
          <hr>
          <!-- ์–‘๋ฐฉํ–ฅ ๋ฐ”์ธ๋”ฉ (input <=> data) -->
          2way:
          <input v-on:keyup="onInputChange" type="text" :value="message">
  value ๊ฐ€ ํ‘œ์ค€์†์„ฑ์ด๊ธฐ ๋•Œ๋ฌธ์— v-bind ํ•ด์ค˜์•ผํ•จ
          <hr>
          <!-- v-model -->
          v-model/2way:
          <input v-model="message" type="text">
      </div>
      <script src="<https://cdn.jsdelivr.net/npm/vue/dist/vue.js>"></script>

      <script>
          const app = new Vue({
              el: "#app",
              data: {
                  message: 'hi',
              },
              methods: {
                  onInputChange(event) {
                      this.message = event.target.value
                  }
              }
          })
      </script>

  </body>
  • v-show

    <body>
        <div id="app">
            <button @click="changeF" >changeF</button>
            <!-- v-if ๋Š” ํ‰๊ฐ€(t/f)๊ฐ€ ์ž์ฃผ ๋ฐ”๋€Œ์ง€ ์•Š์„๋•Œ ์œ ๋ฆฌํ•˜๋‹ค => ์ดˆ๊ธฐ ์ฝ”์ŠคํŠธ๊ฐ€ ์ ๋‹ค. -->
            <p v-if="t">
                This is v-if
            </p>
    
            <p v-if="f">
                This is v-if with false
            </p>
    
            <!-- v-show ๋Š” ํ‰๊ฐ€(t/f) ๊ฐ€ ์ž์ฃผ ๋ฐ”๋€” ๋•Œ ์œ ๋ฆฌํ•˜๋‹ค => ํ† ๊ธ€ ์ฝ”์ŠคํŠธ๊ฐ€ ์ ๋‹ค -->
            <p v-show="t">
                This is v-show with true
            </p>
    
            <p v-show="f">
                This is v-show with false
            </p>
    
        </div>
        <script src="<https://cdn.jsdelivr.net/npm/vue/dist/vue.js>"></script>
        <script>
            const app = new Vue({
                el: "#app",
                data: {
                    t: true,
                    f: false,
                },
                methods: {
                    changeF () {
                        this.f = !this.f
                    }
                }
            })
        </script>
    
    </body>
  • lodash

    <body>
        <div id="app">
            <button @click="getLuckySix">GET LUCKY 6</button>
            <ul >
                <li v-for="number in myNumbers">
                    {{ number }}
                </li>
            </ul>
        </div>
        <script src="<https://cdn.jsdelivr.net/npm/vue/dist/vue.js>"></script>
        <script src="<https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js>"></script>
        <script>
            const app = new Vue({
                el: "#app",
                // [1..45] ์—์„œ 6๊ฐœ๋ฅผ ๋žœ๋คํ•˜๊ฒŒ ๋ฝ‘๋Š”๋‹ค.
                data: {
                    allNumbers: _.range(1,46),
                    myNumbers: []
                },
                methods: {
                    getLuckySix() {
                        this.myNumbers = _.sampleSize(this.allNumbers, 6)
                        // this.myNumbers.sort((a,b) => a-b)
                        this.myNumbers.sort(function(a,b){
                            return a-b
                        })
                    }
                },
            })
        </script>
    </body>
  • computed

    ์–ด๋–ค ํ•จ์ˆ˜๋“ค์„ computed ์— ์ •์˜ํ•˜๋‚˜์š”? โ†’ Data ๋ฅผ Create Update Delete ํ•˜์ง€ ์•Š๊ณ , Read(return) ํ•˜๋Š” ํ•จ์ˆ˜๋“ค! (SQL WHERE)

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>VueJS</title>
    </head>
    <body>
      <div id="app">
        <h1>Poor people</h1>
        {{ bankruptPeople }}
      </div>
      <script src="<https://cdn.jsdelivr.net/npm/vue/dist/vue.js>"></script>
      <script>
        const app = new Vue({
          el: '#app',
    
          data: {
            accounts: [
              { name: 'neo', balance: 500, isBankrupt: true },
              { name: 'tak', balance: 700, isBankrupt: false },
              { name: 'john', balance: 350, isBankrupt: false },
              { name: 'justin', balance: 200, isBankrupt: true },
            ],
          },
    
          methods: {},
    
          /*
            ์–ด๋–ค ํ•จ์ˆ˜๋“ค์„ computed ์— ์ •์˜ํ•˜๋‚˜์š”?
            Data ๋ฅผ Create Update Delete ํ•˜์ง€ ์•Š๊ณ , 
            Read(return) ํ•˜๋Š” ํ•จ์ˆ˜๋“ค! (SQL WHERE)
          */ 
          computed: {
            // ํ•จ์ˆ˜์ง€๋งŒ, ์ด๋ฆ„์€ ๋ช…์‚ฌํ˜•์œผ๋กœ ์ง“๋Š”๋‹ค.
            numOfAccounts: function() {
              return accounts.length
            },
    
            bankruptPeople: function() {
              // filter ๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•œ๋‹ค.
              const newArr = this.accounts.filter((account) => {
                return account.isBankrupt
              })
              // filter ๊ฐ€ ๋ฆฌํ„ดํ•œ ๋ฐฐ์—ด์„ ๋ฆฌํ„ด
              return newArr
            },
    
          }
        })
    
      </script>
    </body>
    </html>

Last updated