[Vue.js] Vue.js๋ / Object.defineProperty() / div#id
Vue๋ ๋ฌด์์ธ๊ฐ?
MVVM ํจํด์ ๋ทฐ๋ชจ๋ธ(ViewModel) ๋ ์ด์ด์ ํด๋นํ๋ ํ๋ฉด(View)๋จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
View
๋ธ๋ผ์ฐ์ ์์ ์ฌ์ฉ์์๊ฒ ๋น์ณ์ง๋ ํ๋ฉด์ด๋ค.
์ ๋ ฅ๋ฐ์ค, ๋ฒํผ ๋ฑ์ด view์ ํด๋นํ๊ณ ,
html์ dom์ ์ด์ฉํด์ javascript๋ก ์กฐ์์ ํ ์ ์๊ฒ๋ ๊ตฌ์ฑ์ด ๋์ด ์๋ค.
Vue
ํน์ ์ฌ์ฉ์๊ฐ ํค๋ณด๋๋ฅผ ์ ๋ ฅํ๊ฑฐ๋ ๋ง์ฐ์ค๋ฅผ ํด๋ฆญํ๋ฉด ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๊ฒ ๋๋ค.
๊ทธ ์ด๋ฒคํธ๋ฅผ ์ค๊ฐ์ view์์ dom listeners๋ก ์ฒญ์ทจํ๊ฒ ๋๋ค.
๊ทธ๋ฐ ์ด๋ฒคํธ๋ฅผ์ก์์ javascript์ ์๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณ ์ ์ฃผ๊ฑฐ๋ ํน์
์ด javascript์ ์ง์ ํ๋ ํน์ ๋ก์ง์์ ์คํํ๊ฒ ๋๋ค.
๊ทธ๋์ javascript ๋ฐ์ดํฐ๊ฐ ๋ณํ์ ๋๋ data Bindings๋ฅผ ํ๊ฒ๋๋๋ฐ
๋ง์ฝ, ๋ฌธ์์ด์ด ๋ฐ๋๊ฑฐ๋ ์ซ์๊ฐ ๋ฐ๊ผ์ ๋ ๋ฐ๋ก data Bindings ์ด์ฉํด view์ ์ ์ฉํด ์ค๋ค.
๊ทธ๋ฆฌ๊ณ ์ ์ฝ๋๋ฅผ ์คํํ๋ฉด ์๋ก๊ณ ์นจ ํ์ ๋ ๋ฐ๋ก ๋ฐ์์ด ๋๋ ๊ฑธ ๋ณผ ์ ์๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
</div>
<script>
var div = document.querySelector('#app');
console.log(div);
div.innerHTML = 'hello world!!!';
</script>
</body>
</html>
DATA BINDINGS์ ์๋ฅผ ์ดํด๋ณด์.
Object.defineProperty()
๊ฐ์ฒด์ ๋์์ ์ฌ์ ์ ํ๋ API์ด๋ค.
(์ํ๋ ๊ฐ์ผ๋ก ์ฌ์ ์ ํ ์ ์๋ค.)
Object.defineProperty() - JavaScript | MDN
The Object.defineProperty() static method defines a new property directly on an object, or modifies an existing property on an object, and returns the object.
developer.mozilla.org
<script>
Object.defineProperty(๋์ ๊ฐ์ฒด, ๊ฐ์ฒด์ ์์ฑ, {
// ์ ์ํ ๋ด์ฉ
})
</script>
์ด๋ฐ ์์ผ๋ก ์์ฑํ๋ฉด ๋๋ค.
<script>
Object.defineProperty(viewModel, 'str', {
// get : ์์ฑ์ ์ ๊ทผํ์ ๋์ ๋์์ ์ ์
get: function () {
console.log('์ ๊ทผ');
},
// ์์ฑ์ ๊ฐ์ ํ ๋นํ์ ๋์ ๋์์ ์ ์
set: function (newValue) {
console.log('ํ ๋น', newValue);
}
})
</script>
์ด๋ฐ์์ผ๋ก ์ฌ์ฉํ ์ ์๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script>
var div = document.querySelector('#app');
var viewModel = {};
Object.defineProperty(viewModel, 'str', {
// get : ์์ฑ์ ์ ๊ทผํ์ ๋์ ๋์์ ์ ์
get: function () {
console.log('์ ๊ทผ');
},
// ์์ฑ์ ๊ฐ์ ํ ๋นํ์ ๋์ ๋์์ ์ ์
set: function (newValue) {
console.log('ํ ๋น', newValue);
div.innerHTML = newValue
}
})
</script>
</body>
</html>
์ฝ์์ฐฝ์ ๊ฐ์ ์ ๋ ฅํ ๋๋ง๋ค app๋ถ๋ถ์ ๊ฐ์ด ๋ณ๊ฒฝ๋๋ ๊ฑธ ๋ณผ ์ ์๋ค.
Reactivity ์ฝ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌํ ํ๊ธฐ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script>
var div = document.querySelector('#app');
var viewModel = {};
// ์ฆ์ ์คํํจ์
(function () {
function init() {
Object.defineProperty(viewModel, 'str', {
// get : ์์ฑ์ ์ ๊ทผํ์ ๋์ ๋์์ ์ ์
get: function () {
console.log('์ ๊ทผ');
},
// ์์ฑ์ ๊ฐ์ ํ ๋นํ์ ๋์ ๋์์ ์ ์
set: function (newValue) {
console.log('ํ ๋น', newValue);
render(newValue);
}
})
}
function render(value) {
div.innerHTML = value;
}
init();
})();
</script>
</body>
</html>
๋ทฐ ๊ฐ๋ฐ์ ๋๊ตฌ
๊ฐ๋ฐ์ ๋๊ตฌ > vue ํด๋ฆญ
data > message ์ ๊ฐ์ ์ ๋ ฅํ๋ฉด ํ์ด์ง์์๋ ๋ณํ๋ ๊ฑธ ๋ณผ ์ ์๋ค.
Visual Studio ๊ฟํ!
div#app ๋ฃ๊ณ ์ํฐ๋ฅผ ๋๋ฅด๋ฉด
์๋์ผ๋ก ์์ฑ์ด ๋๋ค.