【发布时间】:2022-01-04 07:16:35
【问题描述】:
我正在使用 vuetify 创建一个应用,但我在使用 v-menu 时遇到了一些问题。
我有一个文本输入,当我点击它时,它会打开一个 v 菜单。在菜单中,我制作了一个组件,它是一个从 0 到 9 的简单键盘。用那个键盘我想改变文本输入。 我想要一个保存按钮来保存所做的更改并关闭菜单。但我无法让它工作。
我怎样才能让它与多个文本输入一起工作。我知道这对于编辑文本输入可能看起来过于复杂,但该应用程序将在更大的触摸屏上运行,我不想为了更改数字而打开大屏幕键盘。
你会怎么做?
<v-menu bottom :close-on-content-click="false">
<template v-slot:activator="{ on, attrs }">
<v-text-field
v-bind="attrs"
v-on="on"
v-model="result"
readonly
></v-text-field>
</template>
<Calculator :data.sync="result" /> <-------- My keypad component
</v-menu>
键盘组件
<template>
<v-card class="widget" @contextmenu.prevent="" color="grey lighten-2" max-width="260" >
<v-card-text>
<v-card class="mb-2" elevation="1" min-height="64px" max-height="64px" tile flat color="lighten-grey" >
<v-card-text>
<v-text-field dense flat reverse v-model="value" :suffix="suffix" ></v-text-field>
</v-card-text>
</v-card>
<v-row no-gutters class="pb-1">
<v-col align="center" justify="center">
<v-btn color="grey lighten-5" @click="click1" tile elevation="1" height="55px" >1</v-btn>
</v-col>
<v-col align="center" justify="center">
<v-btn color="grey lighten-5" @click="click2" tile elevation="1" height="55px" >2</v-btn>
</v-col>
<v-col align="center" justify="center">
<v-btn color="grey lighten-5" @click="click3" tile elevation="1" height="55px" >3</v-btn>
</v-col>
</v-row>
<v-row no-gutters class="pb-1">
<v-col align="center" justify="center">
<v-btn color="grey lighten-5" @click="click0" tile elevation="1" height="55px" >0</v-btn >
</v-col>
<v-col align="center" justify="center">
<v-btn color="grey lighten-5" @click="clickDecimal" tile elevation="1" height="55px" >.</v-btn >
</v-col>
<v-col align="center" justify="center">
<v-btn color="grey lighten-5" @click="clickClear" tile elevation="1" height="55px" >C</v-btn >
</v-col>
</v-row>
<v-row no-gutters class="pt-2">
<v-col align="right">
<v-btn @click="cancel" tile small>Cancel</v-btn>
<v-btn class="ml-2 mr-0" @click="onSave" tile small>Save</v-btn>
</v-col>
</v-row>
</v-card-text>
</v-card>
</template>
<script>
export default {
props: ['data', 'suffix'],
data() {
return { value: '0', };
},
methods: {
click0() {
if (this.value.toString().length > 9) return;
if (this.value === '0') { this.value = ''; }
this.value = this.value + '0';
},
click1() {
if (this.value.toString().length > 9) return;
if (this.value === '0') { this.value = ''; }
this.value = this.value + '1';
},
click2() {
if (this.value.toString().length > 9) return;
if (this.value === '0') { this.value = ''; }
this.value = this.value + '2';
},
click3() {
if (this.value.toString().length > 9) return;
if (this.value === '0') { this.value = ''; }
this.value = this.value + '3';
},
clickClear() {
this.value = '0';
},
clickDecimal() {
if (this.value.includes('.')) return;
this.value = this.value + '.';
},
onSave() {},
cancel() {},
},
};
</script>
【问题讨论】:
-
考虑从子
Calculator组件发射result并从parent设置相同的值。 -
你能分享你的键盘组件吗?像 Anees 所说的发出事件来更新父 v-text-field 是正确的方法。我可以为你制作一个密码箱。
-
我厌倦了使用 emit,但不知道如何处理多个文本字段。我可以分享我的组件,但这个评论太重要了。
-
我在我的问题@cmfc31 中添加到组件
标签: javascript vue.js vuetify.js