watch : ๊ฐ์ด ๋ณํ๋ ๋๋ง๋ค ์์์ฑ์ ์ฌ์ฉํ๋ ๊ธฐ๋ฅ.
์ด๋ค ์์ฑ์ ๊ฐ์ํ ๊ฒ์ธ์ง ์ ์ํด์ผ ํจ.
watch: {
// inputText ๊ฐ์
inputText: function () {
//console.log(this.forbiddenText);
var pos = this.inputText.indexOf(this.forbiddenText);
if (pos >= 0) {
alert(this.forbiddenText + "๋ ์
๋ ฅํ ์ ์์ต๋๋ค.");
// ์
๋ ฅ๋ฌธ์์์ ๊ธ์ง ๋ฌธ์๋ฅผ ์ญ์ ํ๋ค.
this.inputText = this.inputText.substr(0, pos);
}
},
์ฌ๊ธฐ์๋ inputText๋ฅผ ๊ฐ์ํ๋ค. ๋ณํํ ๋๋ง๋ค ํด๋น function์ด ์ํ๋๋ค.
methods: {
cleanToDo: function () {
this.todos = this.todos.filter(function (val) {
return val.done == false;
})
}
}
val.done์ด false์ธ ์ ๋ค๋ง ๋จ๊ฒจ๋์..
v-if : ํ๊ทธ ์์ฒด๊ฐ ๋ ๋๋ง ์๋จ
v-show : ํ๊ทธ๋ ๋ ๋๋ง ๋จ
computed๋ ๋ณํ ์ ๊ฐ๊ณผ ํ ๊ฐ์ ๋น๊ตํ๋ ์ฒ๋ฆฌ๋ ํ ์ ์๋๋ฐ watch์์๋ ๊ฐ๋ฅํ๋ค.
watch: {
someObj: {
handler: function (val, oldVal) {
console.log(`[someObj] ${val}-${oldVal}`)
},
},
'someObj.a' : {
hadler: function (val, oldVal) {
console.log(`[someObj.a] ${val}-${oldVal}`)
},
},
์ด๋ฐ์์ผ๋ก ' '๋ฅผ ์ด์ฉํด ์๋ฐฐ๋์ง ์๊ฒ ์ธ ์ ์๋ค.
์ด๋ someObj.a๊ฐ ๋ณํ ๋ someObj๋ ์ํ๋์ง ์์. ์๋ํ๋ฉด ์ฐธ์กฐํ๋ ๊ฐ์ฒด๊ฐ ๋ณํ๋ ๊ฑด ์๋๊ธฐ๋๋ฌธ์.
computed๋ ๋น๋๊ธฐ์ฒ๋ฆฌ ์๋จ. ์๊ท๋จผํธ ๋ฐ์ ์ ์์. ์ธ์ ๋๋ ์ง ๋ชจ๋ฅด๋ ๊ฑฐ ๋ถ๊ฐ๋ฅ. -> ๊ทธ๋ด๋ watch ์ด๋ค.
๊ทธ๋ฐ๋ฐ watch๋ ๊ฐ์ด ๋ฐ๋๋๋ง ์ํ๋๊ธฐ ๋๋ฌธ์ ์ด๋๋ ์ฆ์ ์คํ watch๋ฅผ ์ฌ์ฉํ๋ค.
watch: {
someStr: {
handler: function (val, oldVal) {
console.log(`[someStr] ${val}-${oldVal}`)
},
immediate:true
},
์ด๋ฐ์์ผ๋ก immediate:true๋ฅผ ์จ์ ์ฆ์ ์คํ watch๋ฅผ ์จ์ค๋ค. ์ฆ์ ์คํ watch๋ ์ฒ์ ๋ ๋๋ง ๋ ๋๋ ์คํ๋๋ค.
methods๋ ํธ์ถํ ๋๋ง๋ค ๋ถ๋ฌ์์ง๋๋ฐ computed๋ ํธ์ถํด๋ ๊ฐ์ด ๋ณํ๊ฐ ์์ผ๋ฉด ํธ์ถ ์ํ๋ค.
v-clock์ ์ฌ์ฉํด์ ํ๋ฉด์ ์๊ฐ์ด ์ง๋จ์ ๋ฐ๋ผ ๋ถ๋ฌ์ฌ์ ์๋ค.
<style>
[v-cloak]::before {
content: '๋ก๋ฉ์ค...'
}
[v-cloak]>* {
display: none;
}
</style>
์ด๋ฐ์์ผ๋ก ์๊ฐ์ด ๋๊ธฐ ์ ์๋ '๋ก๋ฉ์ค'์ด๋ผ๋ ๋ฉ์ธ์ง๊ฐ ๋จ๊ฒํ๊ณ
<div id="app">
<h1>v-cloak ์ ์ฌ์ฉํ์ง ์์ ํ๊ทธ - {{msg}}</h1>
<div v-cloak>
<h1>v-cloak ์ ์ฌ์ฉํ ํ๊ทธ - {{msg}}</h1>
</div>
</div>
<script>
setTimeout(function () {
Vue.createApp({
data() {
return {
msg: "hello"
}
}
}).mount("#app")
}, 3000);
</script>
div์ v-clock์ ์ค์ ์๊ฐ์ด ์ง๋์ผ ํ๊ทธ๊ฐ ๋ณด์ด๋๋ก ํ ์ ์๋ค. ์์ css์ ์ฐ๋๋ก ํด์ผํ๋ค.
์๊ฐ์ด ๋๊ธฐ์ ์๋ ์์ ๊ฐ์ ํ๋ฉด์ด ๋์ค๊ณ
์๊ฐ์ด ๋๋ฉด ์์ ๊ฐ์ ํ๋ฉด์ผ๋ก ๋ฐ๋๊ฒ ๋๋ค.
methods: {
getAll: function () {
axios.get("http://localhost:8088/boards")
.then(response => {
return response.data;
}).
then(boards => {
let str = "";
for (let elem of boards) {
str += `<h3>${elem.boardNo}. ${elem.title} ${elem.content}</h3>`;
}
this.result = str;
})
.catch(err => console.error(err));
},
์ด๋ฐ์์ผ๋ก ajax๊ธฐ์ ๋ก rest api๋ฅผ ์์ฒญํ ์ ์๋ค.
ajax์ฐ๋ ค๋ฉด
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
์ด๋ฐ์์ผ๋ก ๋์ด์์ผ ํ๋ค. ์ด๋ฐ ๋ฐฉ๋ฒ์ cdn๋ฐฉ๋ฒ์ด๋ค. cdn๋ก๋ฉ์ผ๋ก ๋์ด์จ๋ค.
component
<div id="app">
<my-comp-tag1></my-comp-tag1>
<my-comp-tag2></my-comp-tag2>
<my-comp-tag3></my-comp-tag3>
<hr>
<my-comp-tag1></my-comp-tag1>
</div>
<script>
const { createApp } = Vue;
const MyComponent1 = {
template: '<p class="my-comp">์๋
? ๋๋ฆฌ~~</p>'
}
const MyComponent2 = {
template: '<p class="my-comp">์๋
? ๋์ฐ๋~~</p>'
}
const MyComponent3 = {
template: '<p class="my-comp">์๋
? ๋์น~~</p>'
}
createApp({
components: {
'my-comp-tag1': MyComponent1,
'my-comp-tag2': MyComponent2,
'my-comp-tag3': MyComponent3,
}
}).mount('#app');
</script>
์ปดํฌ๋ํธ ์ธ๋ ์ด๋ฐ์์ผ๋ก ์ ์ํด์ค๋ค.
createApp({
components: {
'my-component': MyComponent
}
๊ด๋ก์ ์ผ๋ก๋, html์์ ์ฌ์ฉํ ๋๋ ์ค๋ค์ดํฌ ํ๊ธฐ๋ฒ('my-component'), ๋ค์๊ฑฐ๋ ์นด๋ฉ์ผ์ด์ค(MyComponent)๋ก ํ๊ธฐํ๋ค.
<div id="app">
<my-component my-name="์๊ธ"></my-component>
<my-component my-name="๊ธ๊ธ"></my-component>
<my-component></my-component>
</div>
<script>
const {createApp} = Vue;
const MyComponent = {
template: '<p class="my-comp">๋๋ {{ myName }} ์
๋๋ค.</p>',
props: {
myName: String
}
}
createApp({
components: {
'my-component': MyComponent
}
}).mount('#app');
</script>
์ด๋ ์ฌ์ฉ๋๋ ๊ฒ props๋ผ๋ ์ค์ . ์ด๋ค ํ์ ์ ๋ฐ์ดํฐ ๋ฐ๊ฒ ๋ค.. ํ๊ณ ์ค๋ค. ์ฌ๊ธฐ์๋ String..
ํ๊ทธ๋ฅผ ํตํด์ ์ด ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ ๊นจ myName์ ์ด์ฉํด์ ๊ฐ์ ์ ๋ฌํ๊ฒ ๋ค ๋ผ๋ ๋ป. ์ ๋ฌ๋ฐ๊ณ ์ถ์ ๋ฐ์ดํฐ๋งํผ ์ ์ํ ์ ์๋ค. ์ฌ๊ธฐ์ ์ด๋ฆ๊ท์น์ ์นด๋ฉ์ผ์ด์ค๋ฅผ ์ค๋ค์ดํฌ ์ผ์ด์ค๋ก ๋ฐ๊ฟ์ผํ๋ค. ์ฌ์ฉํ ๋๋ myName์ด๋ฐ์์ผ๋ก ์ฐ์ฌ์ผ ํ๋ค.
{{}}๋ฅผ ์ด์ฉํด์ ์ถ๋ ฅํ๋ค.
๋ธ๋ผ์ฐ์ ๋ฅผ ํตํด์ ๋ณธ๋ค๋ฉด
์ด๋ฐ์์ผ๋ก ๋ณด์ธ๋ค.
'๐ผ Full-Stack Study โก (feat. KOSA)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Vue.js (4) (0) | 2023.11.10 |
---|---|
Vue.js (3) (0) | 2023.11.09 |
Vue.js (0) | 2023.11.06 |
ECMAScript6 (0) | 2023.11.06 |
Spring Security, ES6 (1) | 2023.11.03 |