英文:
Update v-model variable in another function
问题
这可能是一个非常简单的问题,但在搜索了几个小时后,我没有找到任何有用的信息。考虑以下代码:
<script setup>
var code = 123;
function run() {
code = 5456;
}
</script>
<template>
<input v-model="code" /> <button @click="run">Run</button>
</template>
当我点击按钮时,输入框中显示的值没有改变。
我尝试了使用 ref
,但它不起作用。
英文:
This maybe a very simple problem, but I didn't find any useful information after searching for some hours. Consider the following code:
<script setup>
var code = 123;
function run() {
code = 5456;
}
</script>
<template>
<input v-model="code" /> <button @click="run">Run</button>
</template>
When I click button, the value shown in input is not changed.
I tried ref
which doesn't work.
答案1
得分: 1
Ref works - try this code:
<script setup>
import { ref } from 'vue'
var code = ref(123);
function run() {
code.value = 5456;
}
</script>
<template>
<input v-model="code" /> <button @click="run">Run</button>
</template>
You can confirm it here on Vue Playground
英文:
Ref works - try this code:
<script setup>
import { ref } from 'vue'
var code = ref(123);
function run() {
code.value = 5456;
}
</script>
<template>
<input v-model="code" /> <button @click="run">Run</button>
</template>
You can confirm it here on Vue Playground
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论