Vue.js 通过单击按钮打开模式

2023-11-23

如何使用按钮在其他组件中显示模态框?例如,我有以下组件:

info.vue

<template>
  <div class="container">
    <button class="btn btn-info" @click="showModal">show modal</button>
    <example-modal></example-modal>
  </div>
</template>

<script>
import exampleModal from './exampleModal.vue'
export default {
  methods: {
    showModal () {
      // how to show the modal
    }
  },
  components:{
    "example-modal":exampleModal
  }
}
</script>

exampleModal.vue

<template>
    <!-- Modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body">
            hihi
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
        </div>
        </div>
    </div>
    </div>
</template>

如何显示 exampleModal.vue 中的模式?我知道我可以 使用 data-toggle 和 data-target 显示模式,如下所示:

<button class="btn btn-info" data-toggle="modal" data-target="#exampleModal">show modal</button>

但是有没有办法使用“showModal”方法来显示模式呢?


According to your snippet, you're using a classic Bootstrap modal. You just need to use data-toggle and data-target attributes in that case:

<div id="app">
  <div class="container">
    <button class="btn btn-info" data-toggle="modal" data-target="#exampleModal">show modal</button>
    <example-modal></example-modal>
  </div>
</div>

Edit

我误读了这个问题,所以我编辑了我的答案。

可以使用自定义方法打开模式。您只需要使用以下命令找到元素“#exampleModal”refs然后使用引导方法打开模式(Bootstrap 编程 API)

<div id="app">
  <div class="container">
    <button class="btn btn-info" @click="showModal">show modal</button>
    <example-modal ref="modal"></example-modal>
  </div>
</div>
methods: {
  showModal() {
    let element = this.$refs.modal.$el
    $(element).modal('show')
  }
}

小提琴示例

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Vue.js 通过单击按钮打开模式 的相关文章

随机推荐

  • System.Diaganostics.Process.Id 与任务管理器中显示的进程 ID 不同。为什么?

    我正在使用 C System Diagnostic Process object 它的属性之一是Id The Id这产生的与PID 如图所示Windows Task Manager 为什么是这样 你看 一旦这个过程开始 它启动了另外两个非托
  • 有没有一种简单的方法可以将相机的 onPreviewFrame 中的字节数组转换为 android 中的图片?

    我问是否有一个简单的方法 因为有一个谷歌问题报告说使用decodeByteArray是不可能的 但该报告起源于 2008 年 我希望那里没有发布解决方案 问题报告中列出的方法是自己解码格式 但我不想将其放入并减慢程序速度 任何帮助将不胜感激
  • 如何扩展 Node.js WebSocket Redis 服务器?

    我正在编写一个聊天服务器Acani 我有一些问题扩展 Node js and 具有负载均衡器可扩展性的 websocket Node js 负载均衡究竟意味着什么 这是否意味着会有n我的服务器应用程序运行的独立版本 每个版本都在单独的服务器
  • 登录成功后如何重定向到页面?

    我对 Web 表单开发相当陌生 正在尝试使用 VS 2010 中的 ASP NET Web 应用程序模板创建的项目 用户成功登录后 我希望将用户重定向到我创建的页面 如何修改我的项目以在登录后重定向用户 任何示例 教程 等都非常感谢 Tha
  • Base91,是怎么计算的?

    我一直在网上查找 basE91 是如何计算的 我找到了诸如this one它指定了用于特定值的字符 但我没有找到如何获取该值的方法 我尝试将输入值更改为二进制并获取 6 位和 7 位的块 但这些不起作用 并且我得到了不正确的输出 我不想要为
  • 有没有类似 GhostDoc 的 C++ 工具

    当我使用 C 进行开发时 我大量使用 GhostDoc 来加快代码注释的过程 我目前正在开发一个 C 项目 但尚未找到等效的工具 我了解 Doxygen 但据我所知 它用于在代码外创建文档 而不是在代码中创建注释 有没有好的等效工具 我更喜
  • WPF:如何循环浏览窗口中的所有控件?

    如何循环遍历 WPF 窗口中的所有控件 我在MSDN 文档所以这有帮助 Enumerate all the descendants of the visual object static public void EnumVisual Vis
  • ImageIO.read() 返回 403 错误

    我有以下代码 public BufferedImage urlToImage String imageUrl throws MalformedURLException IOException URL url new URL imageUrl
  • 拦截JAX-RS请求:向tomcat注册ContainerRequestFilter

    我正在尝试通过 ContainerRequestFilter 拦截对我的 JAX RS Web 服务的请求 我想将它与自定义注释一起使用 这样我就可以装饰 Web 服务的某些方法 这应该使我能够根据是否在安全通道上发出的信息来处理对此方法的
  • uWSGI - Django 管理员的不同 Harakiri 超时

    是否可以将 Django 管理页面的 uWSGI 的 harakiri 超时设置为不同的 我有一个网站需要上传大文件 这可能需要 20 或 30 秒以上 但我希望全局 harakiri 超时更像 15 秒 因为我将数据发送回用户的方式 uW
  • 使用意图 URI 启动我的应用程序

    我知道这个问题已经在 StackOverflow 中被问过很多次了 但我还没有找到解决方案 我的应用程序发送一封电子邮件 其中包含一个链接 单击该链接后应启动该应用程序 根据 hackbod 的说法 最好的方法是使用 Intent URI
  • 启动 jupyter-lab 并从命令行打开特定笔记本?

    我正在编写一个 bash 脚本 最后启动 jupyter lab 并 理想情况下 打开一个特定的笔记本 在 jupyter 中 这很容易做到 jupyter notebook my folder my notebook ipynb 我找不到
  • 清除wpf中的数据网格值

    我需要冲洗我的datagrid每当一个treeviewitem被点击 我的代码如下 private void treeView1 SelectedItemChanged object sender RoutedPropertyChanged
  • 全局构造函数调用不在 .init_array 部分

    我正在尝试在嵌入式目标 ARM Cortex M3 上添加全局构造函数支持 假设我有以下代码 class foobar int i public foobar i 100 void inc i foobar foo int main foo
  • 在.NET中生成PDF文件[关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 目前不接受答案 我正在使用 C NET 2 0 我想生成一个 PDF 文件 我应该在文件中写一段文字 生成PDF需要满足哪些先决条件 任何简单的编码示例都会对我有帮
  • jQuery 文件上传在 IE 8 和 9 中不起作用

    此代码适用于 FF 和 chrome 在 IE 8 或 9 中 我收到 500 错误 指出非空属性为空 这是html div class fileupload buttonbar div
  • `f()` 和 `new f()` 有什么区别? [复制]

    这个问题在这里已经有答案了 可能的重复 JavaScript 中的 new 关键字是什么 从 JS 闭包创建对象 我应该使用 new 关键字吗 看这段代码 function friend name return name name var
  • 使用jQuery获取url并提取url段

    在具有类别列表的网页上 每个类别标题都按以下格式链接 http localhost admin category unpublish 2 我编写了以下js代码 试图捕获url和段 unpublish action 和 2 id 并且需要将请
  • Windows 和 Linux 上 fopen 中“r+”的使用

    我正在摆弄一些打开 读取和修改文本文件的代码 一个快速 简化 的例子是 include
  • Vue.js 通过单击按钮打开模式

    如何使用按钮在其他组件中显示模态框 例如 我有以下组件 info vue