我正在迈出使用 Quasar 的第一步。
我想构建一个模态窗口以在表单中重复使用。
我在自定义组件中使用对话框插件和 q-layout。
但是,当我在另一个组件中使用此自定义组件时,对话框插件方法不起作用。
有什么办法可以解决吗?
util/modal.js
import { Dialog } from "quasar";
export function ModalWindow(CustomComponent) {
Dialog.create({
component:CustomComponent,
ok: {
push: true
},
cancel: {
color: 'negative'
},
persistent: true
})
}
modal/ModalWindow.vue(自定义组件):
<template>
<q-dialog persistent ref="dialog" @hide="onDialogHide">
<q-layout view="lhh LpR lff" container style="height: 400px" class="bg-grey-3">
<q-toolbar class="bg-primary text-white q-mb-sm">
<q-toolbar-title>
<slot name="titelWindow"></slot>
</q-toolbar-title>
<q-btn v-close-popup flat round dense icon="close" />
</q-toolbar>
<q-form @submit.prevent="submitForm">
<q-card-section>
<slot></slot>
</q-card-section>
<q-card-actions align="right">
<slot name="toolbarBottom"></slot>
</q-card-actions>
</q-form>
</q-layout>
</q-dialog>
</template>
<script>
export default {
methods: {
show () {
this.$refs.dialog.show()
},
hide () {
this.$refs.dialog.hide()
},
onDialogHide () {
this.$emit('hide')
}
}
}
</script>
在页面上调用方法 ModalWindow:
<script>
import { ModalWindow } from 'src/util/modal'
import CustomComponent from "components/modal/MyModalWindow.vue"
export default {
methods: {
showUWin: function(id) {
ModalWindow(CustomComponent)
}
}
}
</script>
到目前为止它运行良好。
但是,正如我所说,当我在另一个组件中使用此自定义组件时,对话框插件方法不起作用。
在另一个组件中渲染自定义组件:MyModalForm.vue
<template>
<MyModalWindow>
<!--Dialog's show method doesn't work-->
</MyModalWindow>
</template>
<script>
export default {
name: 'MyModalForm',
components: {
'MyModalWindow': require('components/modal/MyModalWindow.vue').default,
}
}
</script>
在页面上调用方法 ModalWindow:
<script>
import { ModalWindow } from 'src/util/modal'
import CustomComponent from "components/modal/MyModalForm.vue"
export default {
methods: {
showWin: function(id) {
ModalWindow(CustomComponent)
}
}
}
</script>
我进入控制台:
[Vue warn]: Error in mounted hook: "TypeError: this.$refs.dialog.show
is not a function"
最佳答案
我最近遇到了同样的错误。
我的理解是,当你使用类似的东西时:
Dialog.create({
component: CustomComponent,
...
})
// or
this.$q.dialog({
component: CustomComponent
})
CustomComponent
必须根据文档直接实现所需的显示/隐藏/...方法。
因此您必须在每个自定义组件中重复此代码(将其调整为正确的“ref”,特定于您的组件):
methods: {
show () {
this.$refs.dialog.show()
},
hide () {
this.$refs.dialog.hide()
},
onDialogHide () {
this.$emit('hide')
}
}
并适本地传播 onOk 和 onCancel 事件。
例如,总结一切:
<template>
<MyModalWindow ref="myModalForm" @ok="onOk" />
</template>
<script>
export default {
name: 'MyModalForm',
components: {
'MyModalWindow'
},
methods: {
show() {
this.$refs.myModalForm.show();
},
hide() {
this.$refs.myModalForm.hide();
},
onHide() {
this.$emit('hide');
},
onOk() {
this.$emit('ok');
this.hide();
}
}
}
</script>
关于vue.js - 在另一个组件上重用带有自定义组件的 Quasar Dialog 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58557479/