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