我正在使用 VueJS Vuetify 框架,我需要从另一个模板打开一个对话框 - 作为组件模板导入。单击 App.vue 中的 菜单 按钮 后,模式应打开。这是我的设置:
- App.vue = 带菜单按钮的导航模板
- Modal.vue = 模态模板,在 main.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>
如何打开对话框?
原文由 Tom 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用自定义事件打开对话框,并使用 事件总线进行非父子通信。
如果您的应用程序变得有点复杂,我建议您使用 Vuex 进行状态管理。
事件总线解决方案:
在您的 main.js 或新文件中创建并导出一个新的 Vue 实例:
export const bus = new Vue()
在 app.vue 中 导入
bus
并发出事件:在 modal.vue 中 还导入总线并在创建的钩子中监听事件: