VUE的事件如何可以更改const变量?
vue的 事件
可以更改const
变量吗?
“状态”是原始const
变量。当然,不可能改变价值。
错误:状态++
但是,通过内联事件增加状态时没有错误。
这是为什么?
<!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>
当然状态没有反应性。但是,使用开发人员工具,您可以看到该值已更改。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
换句话说,在
&lt; template&gt;
内部是缩短的,您不会从
&lt; script&gt;
中与const state
进行交互。您与模板上下文的属性进行交互。将其视为通过合并需要暴露于
&lt; template&gt;
的所有物体创建的对象(例如:data
的传播返回,所有代码>,所有
方法
,设置
,Allprops
等...),您可以安全地写入对象的属性,除非已定义为
可写的:false
,这显然不是这种情况。简单示例:
is short for
In other words, inside
<template>
you don't interact with theconst state
from<script>
.You interact with the template context's properties. Think of it as an object created by merging everything that needs to be exposed to the
<template>
(e.g: spread return ofdata
, allcomputed
, allmethods
, spread return ofsetup
, allprops
, etc...)And you can safely write to an object's property, unless it has been defined as
writeable: false
, which is clearly not the case.Simple example:
Additionally,
@click="expression"
is wrapped in an anonymous function by Vue ifexpression
is not callable (akin to@click="() => { expression; }"
).Side note: In your example, if you want
state
to be reactive you have to declare it asconst state = ref(1)
. Otherwise, even thoughstate++
will changestate
's value, it won't trigger a re-render, so it will look like it didn't update, until something else triggers a component re-render and the template value gets updated (overridden).