How Vue's event can change const variable is possible?
The 'state' is the primitive const variable. Of course, it is impossible to change the value.
error: state ++
However, there is no error when increasing the state through the inline event.
Why is that?
https://codepen.io/quietjun/pen/gOvEjvq?editors=1111
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>모두의 코딩</title>
</head>
<body>
<div id="app">
<span>{{state}}</span><br />
<button @click="state++">inline</button><br />
<button @click="increase">function</button><br />
</div>
</body>
</html>
<script src="https://unpkg.com/vue@3"></script>
<script>
const { createApp } = Vue;
createApp({
setup() {
const state = 1;
// of course below line is error
//state++;
const increase = () => {
state++;
};
return {
state,
increase,
};
},
}).mount("#app");
</script>
Of course state is not reactive. However, using the developer tool, you can see that the value has changed.
