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
Was this helpful?