gpt4 book ai didi

javascript - 从 VueJS 中的组件模板打开 Vuetify 对话框

转载 作者:行者123 更新时间:2023-12-03 00:32:29 25 4
gpt4 key购买 nike

我正在使用 VueJS Vuetify framework我需要打开一个对话框 - 从另一个模板导入为组件模板。单击 App.vue 中的菜单按钮后,模态框应该打开。这是我的设置:

  • App.vue = 带菜单按钮的导航模板
  • Modal.vue = Modal 模板,在 main.js 中作为全局导入

ma​​in.js

import Modal from './components/Modal.vue'
Vue.component('modal', Modal)

Modal.vue 模板:

<template>
<v-layout row justify-center>
<v-btn color="primary" dark @click.native.stop="dialog = true">Open Dialog</v-btn>
<v-dialog v-model="dialog" max-width="290">
<v-card>
<v-card-title class="headline">Use Google's location service?</v-card-title>
<v-card-text>Let Google help apps determine location. This means sending anonymous location data to Google, even when no apps are running.</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="green darken-1" flat="flat" @click.native="dialog = false">Disagree</v-btn>
<v-btn color="green darken-1" flat="flat" @click.native="dialog = false">Agree</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-layout>
</template>
<script>
export default {
data () {
return {
dialog: false
}
}
}
</script>

如何打开对话框?

最佳答案

不需要事件总线和 v-model

更新:

当我第一次回答这个问题时,我将我的答案作为“解决方法”发布,因为当时感觉并不完全“正确”,而且我对 Vue.js 还很陌生。我想使用 v-model 指令打开或关闭对话框,但我无法到达那里。一段时间后我发现how to do this in the docs ,使用输入事件值属性,以下是我认为在没有事件总线的情况下应该如何完成的方法。

父组件:

<template>
<v-btn color="accent" large @click.stop="showScheduleForm=true">
<ScheduleForm v-model="showScheduleForm" />
</template>

<script>
import ScheduleForm from '~/components/ScheduleForm'

export default {
data () {
return {
showScheduleForm: false
}
},
components: {
ScheduleForm
}
}
</script>

子组件(ScheduleForm):

<template>
<v-dialog v-model="show" max-width="500px">
<v-card>
<v-card-actions>
<v-btn color="primary" flat @click.stop="show=false">Close</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</template>

<script>
export default {
props: {
value: Boolean
},
computed: {
show: {
get () {
return this.value
},
set (value) {
this.$emit('input', value)
}
}
}
}
</script>

原始答案:

我能够解决这个问题,而不需要全局事件总线。

我使用了带有 getter 和 setter 的计算属性。由于 Vue 会警告您直接改变父属性,因此在 setter 中我只是向父属性发出了一个事件。

代码如下:

父组件:

<template>
<v-btn color="accent" large @click.stop="showScheduleForm=true"></v-btn>
<ScheduleForm :visible="showScheduleForm" @close="showScheduleForm=false" />
</template>

<script>
import ScheduleForm from '~/components/ScheduleForm'

export default {
data () {
return {
showScheduleForm: false
}
},
components: {
ScheduleForm
}
}
</script>

子组件(ScheduleForm):

<template>
<v-dialog v-model="show" max-width="500px">
<v-card>
<v-card-actions>
<v-btn color="primary" flat @click.stop="show=false">Close</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</template>

<script>
export default {
props: ['visible'],
computed: {
show: {
get () {
return this.visible
},
set (value) {
if (!value) {
this.$emit('close')
}
}
}
}
}
</script>

关于javascript - 从 VueJS 中的组件模板打开 Vuetify 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48035310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com