[英]How to show modal on component/page created/load in Vue.js
I'm new to Vue.js and I struggle with modals.我是 Vue.js 的新手,我在模态上很挣扎。 I want to first load the modal window as a user navigates to a certain component.
当用户导航到某个组件时,我想首先加载模态 window 。 I tried to create some example modal and it opens if I click a button and trigger
$('#modalId').modal('show')
.我尝试创建一些示例模式,如果我单击按钮并触发
$('#modalId').modal('show')
,它会打开。 But when I try to execute the same command from Vue's created () {...}
It does not open a modal window.但是当我尝试从 Vue 的
created () {...}
执行相同的命令时,它不会打开模态 window。 I don't want to open a modal window with a button click, I want to open as page/component loads.我不想通过单击按钮打开模态 window,我想在页面/组件加载时打开。
<!-- This works fine -->
<button class="btn btn-dark" @click="openModal" data-target="#loginModal">Open modal</button>
<!-- MODAL -->
<div class="modal" ref="modal" id="loginModal" data-keyboard="false" data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Insert required data</h5>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control">
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-primary" data-dismiss="modal">Submit</button>
<a href="#" @click="logout" data-dismiss="modal">
<span>{{ $t('pages.userIconInHeader.signOut') }}</span>
</a>
</div>
</div>
</div>
</div>
...
export default {
name: 'test',
data () {
return {}
},
created () {
// HERE I WOULD LIKE TO OPEN MODAL. If it's even possible?
$(document).ready(function () {
this.openModal()
})
},
methods: {
openModal () {
$('#loginModal').modal('show')
},
...
Use in mounted
without $(document).ready(function(){})
:在没有 $(document)
mounted
$(document).ready(function(){})
的情况下使用:
mounted() {
this.openModal();
},
Don't mix jQuery and Vue.不要将 jQuery 和 Vue 混用。 Vue works with Virtual DOM and you should update, show, hide elements according to Vue documentation, no work around with jQuery.
Vue 与 Virtual DOM 一起使用,您应该根据 Vue 文档更新、显示、隐藏元素,jQuery 无法解决。 This is article about creating modal in Vue, I hope it helps https://alligator.io/vuejs/vue-modal-component/
这是关于在Vue中创建模态的文章,希望对https://alligator.io/vuejs/vue-modal-component/有帮助
mounted() {
this.showModal()
},
methods: {
showModal() {
this.$modal.show("model-name")
},
}
this can be used for opening model in vuejs这可用于在 vuejs 中打开 model
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.