我想使用bootstrap vue modal component在vuejs app中实现此功能:
当用户单击页面UI上的“删除”按钮时:
-
它显示了其正文中包含动态内容的模式:“您确定要删除customer:customer_name_here”
-
如果用户点击“取消”按钮:模态消失 .
-
如果用户点击“确定”按钮:
-
它将模态正文内容更改为:'删除客户'customer_name_here'...,它会禁用“取消”和“确定”按钮,并调用API来删除客户 .
从API收到成功响应时:
-
它将模态正文内容更改为:'Successfully deleted customer ' customer_name_here'
-
仅显示模态页脚中的“确定”按钮,如果单击模式将消失 .
这个代码到目前为止:
<b-button v-b-modal.modal1 variant="danger">Delete</b-button>
<b-modal id="modal1" title="Delete Customer"
@ok="deleteCustomer" centered no-close-on-backdrop -close-on-esc ref="modal">
<p class="my-4">Are you sure, you want to delete customer:</p>
<p>{{customer.name}}</p>
</b-modal>
Vue JS代码:
deleteCustomer(evt) {
evt.preventDefault()
this.$refs.modal.hide()
CustomerApi.deleteCustomer(this.customer.id).then(response => {
// successful response
})
4 回答
如果我理解正确,您希望根据不同的状态组合显示模态内容 .
作为您的描述,应该有2个状态:
deletionState:表示是否开始删除
loadingState:表示是否正在等待服务器的响应
检查Bootstrap Vue Modal Guide,然后搜索keyword = Disabling built-in buttons ,您将看到我们可以使用
cancel-disabled
和ok-disabled
props来控制默认 Cancel 和 OK 按钮的禁用状态(或者您可以使用slot = modal-footer ,或 modal-ok , modal-cancel . ) .您可以使用的其他道具:
ok-only
,cancel-only
,busy
.最后用状态组合绑定
v-if
和props以显示内容 .如下演示:
这是Bootstrap-vue模式的通用包装器组件,它根据
nextState
属性获取状态数组和导航 . 它利用 computed properties 来响应状态变化 .在父级中,状态数组也在计算属性中定义,以便我们可以向消息添加客户(或照片)属性 .
Edit
添加了内容槽,允许父组件在模态内容中定义精确标记 .
您可能更喜欢使用单独的模态,逻辑变得更清晰,您可以轻松添加更多路径,例如重试API错误 .
正如我们在评论中讨论的那样,另一种解决方案就像Quasar Stepper .
设计一个组件作为步骤(下面的演示名称为
b-step-modal
),然后使用一个模态步进器(下面的演示名称为
b-stepper-modal
)作为父级 .然后你只需要列出你所有的步骤作为
modal-stepper
的孩子 . 如果您想要禁用按钮或跳过一步等,您可以使用步骤挂钩(下面演示提供step-begin
和step-end
)来实现目标 .如下粗略演示: