【发布时间】:2019-09-25 07:55:30
【问题描述】:
我对 Vue.js 和 Vuetify 还很陌生(使用 AngularJS 好几年了,但我们公司正在改用 Vue.js)。我想要完成的是,当用户单击“登录”按钮时,它会进行一些检查(即用户名不能为空)并启动 Vuetify 对话框以提醒用户。我知道 Vuetify 有一些内置验证,但正在寻找更强大的东西,我可以等待响应(即当我需要等待类似的东西时,我可以使用你的历史记录/位置)。
基本上想做的事:
if (!userName){
userName = await mbox('You must Enter your Username');
return
}
或
var mProceed = await mbox('Can we use your location for awesome stuff?');
其中 mbox(一个简单的消息框弹出框)是一个函数,它返回一个 promise,以编程方式加载一个 vue 组件,将其添加到 dom,然后等待响应。
即
async function mbox (mText) {
// load dialog component here and set message passed in
// How Do I load the template / wait for it?
return dialogResult
}
Vue 组件看起来像(按钮标题和文本是我传递给 mbox 函数的变量):
<template>
<v-layout row justify-center>
<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="dialog = false"
>
Disagree
</v-btn>
<v-btn
color="green darken-1"
flat="flat"
@click="dialog = false"
>
Agree
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-layout>
</template>
我可以编辑模板/添加 vue 组件的脚本我只是不确定如何通过返回承诺并等待响应的方法调用它?
【问题讨论】:
-
我只注册了一次观察者并用 Promise
await new Promise(resolve => { const stop = this.$watch('dialog', () => resolve(stop())) })
标签: javascript vue.js vuejs2 vuetify.js