Car*_*osa 3 vue.js bootstrap-4 vuejs2
我有一个 vuejs 组件,它显示一个带有小表单的模式对话框。提交表单后,我想隐藏模态,但不知道该怎么做。提交时,表单调用父级中的方法。
这是组件代码
<template>
<div>
<div id="todoModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">{{ title }}</h4>
<button type="button" class="close" data-dismiss="modal">
×
</button>
</div>
<div class="modal-body">
<form id="todoForm" @submit.prevent="$emit('save')">
<div class="form-group">
<label for="name">Todo name</label>
<input
id="name"
v-model="todo.name"
type="text"
class="form-control"
aria-describedby="nameHelp"
placeholder="Enter Todo Name"
/>
<small id="nameHelp" class="form-text text-muted"
>Enter your todo's name</small
>
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-primary mr-4" type="submit" form="todoForm">
<span v-if="mode == 'create'">Create</span>
<span v-if="mode == 'update'">Update</span>
</button>
<button
type="button"
class="btn btn-danger mr-auto"
data-dismiss="modal"
@click="
$parent.showModal = false;
$parent.getTodos();
"
>
Cancel
</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "CreateTodo",
props: ["mode", "title", "todo", "showModal"],
ref: "createTodoModal",
mounted() {
if (this.mode == "create") {
this.title = "Create Todo";
}
},
methods: {}
};
</script>
<style scoped></style>
Run Code Online (Sandbox Code Playgroud)
这是我的 APP.js 文件
<template>
<div id="app" class="container mt-5">
<router-view
ref="createtodo"
class="CreateTodo"
name="a"
:todo="todo"
:title="title"
:mode="mode"
:show-modal="showModal"
@save="saveTodo"
></router-view>
</div>
</template>
<script>
import { APIService } from "./APIServices";
const apiService = new APIService();
export default {
name: "App",
data: function() {
return {
mode: "create",
title: "Create Todo",
todo: {},
todos: [],
numberOfTodos: 0,
showModal: false
};
},
mounted: function() {
this.getTodos();
},
methods: {
saveTodo: function() {
if (this.mode == "create") {
apiService.createTodo(this.todo).then(
result => {
if (result.status === 200) {
this.todo = result.data;
this.getTodos();
}
},
error => {}
);
this.showModal = false;
// this.$refs.createtodo.$refs.todoModal
} else if (this.mode == "update") {
apiService.updateTodo(this.todo).then(
result => {
this.getTodos();
},
error => {}
);
this.showModal = false;
} else if (this.mode == "update") {
apiService.updateTodo(this.todo).then(
result => {
this.showModal = false;
this.getTodos();
},
error => {}
);
}
},
}
};
</script>
<style lang="scss">
</style>
Run Code Online (Sandbox Code Playgroud)
我尝试使用 ref 从 APP.js 引用 Modal,但它不起作用。
小智 13
添加一个 id 到关闭 X 按钮”
<button type="button" class="close" data-dismiss="modal" aria-label="Close" id="close">
<span aria-hidden="true">×</span>
</button>
Run Code Online (Sandbox Code Playgroud)
然后创建一个方法来关闭模态框:
closeModal() {
document.getElementById('close').click();
},
Run Code Online (Sandbox Code Playgroud)
Abd*_*RIB 11
就像 @Dan Stoian 回复一样,您可以在 vue.js 中使用 ref :
<button ref="Close" type="button" data-dismiss="modal" ...>
...
</button>
Run Code Online (Sandbox Code Playgroud)
在你的处理程序中
this.$refs.Close.click();
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 boostrap,则需要从中调用 hide 和 show 方法,因为 modal api 会动态创建 html 元素(作为深色背景)
我建议创建一个保存方法而不是调用 $emit,您可以在发出保存信号之前调用模态隐藏方法。
<script>
import $ from 'jquery'
export default {
name: "CreateTodo",
props: ["mode", "title", "todo"],
ref: "createTodoModal",
mounted() {
if (this.mode == "create") {
this.title = "Create Todo";
}
},
methods: {
save() {
$('#ModalId').modal('hide')
this.$emit('save')
}
}
};
</script>
Run Code Online (Sandbox Code Playgroud)
在这种情况下不需要 showModal。
归档时间: |
|
查看次数: |
7703 次 |
最近记录: |