{"id":2105,"date":"2022-09-11T21:31:42","date_gmt":"2022-09-11T13:31:42","guid":{"rendered":"https:\/\/yukihiew.com\/?p=2105"},"modified":"2023-06-27T15:29:26","modified_gmt":"2023-06-27T07:29:26","slug":"vue-bootstrap-modal-open-by-javascript","status":"publish","type":"post","link":"https:\/\/yukihiew.com\/vue-bootstrap-modal-open-by-javascript\/","title":{"rendered":"Vue | \u7528\u7d14js\u63a7\u5236Bootstrap Modal\u900f\u904e\u4e8b\u4ef6\u89f8\u767c\u986f\u793a (\u975ebootstrap vue)"},"content":{"rendered":"\n
\u7531\u65bcBootstrap Vue\u53ea\u652f\u63f4\u5230vue 2.6\u591a\u7684\u7248\u672c\uff0c\u5c0e\u81f4\u5c07vue\u5347\u7d1a\u81f33.0\u7684\u6642\u5019\u7121\u6cd5\u4f7f\u7528\uff0c\u9700\u8981\u81ea\u5df1\u64b0\u5beb\u985e\u4f3c\u7684modal\u5448\u73fe\u65b9\u5f0f\u3002<\/p>\n\n\n
\u4f7f\u7528 Vue3+Bootstrtap5 \u88fd\u4f5c\u5831\u932fModal \u542b\u53ef\u8b8a\u52d5\u8a0a\u606f\u5167\u5bb9\u3002<\/p>\n\n\n
\u8d77\u624b\u5f0f\uff1a\u8a18\u5f97\u5c0e\u5165modal<\/p>\n\n\n\n
import {Modal} from 'bootstrap';<\/code><\/pre>\n\n\n\n\u7528js\u958b\u555fmodal<\/h3>\n\n\n
\u81f3\u65bc\u7528js\u6253\u958bBootstrap Modal\u7684\u5beb\u6cd5\u5f88\u7c21\u55ae\uff0c\u4f46\u662f\u8cc7\u6599\u771f\u7684\u5f88\u5c11\uff0c\u5927\u90e8\u5206\u90fd\u662f\u7528JQ\u6216\u6309\u9215\u53bb\u6253\u958bmodal\uff0c\u4f46\u4e0d\u60f3\u70ba\u4e86\u6253\u958bModal\u53bb\u52a0\u8f09JQ\uff0c\u5f88\u6c92\u610f\u7fa9\uff0c\u6240\u4ee5\u82b1\u8d85\u591a\u6642\u9593\u6316\u76f8\u95dc\u8cc7\u6599\u5f8c\u81ea\u5df1\u63a8\u65b7\u548c\u5617\u8a66\u51fa\u7684\u7d50\u679c\u3002
\u5c31\u662f\u7528bootstrap\u6587\u4ef6\u63d0\u4f9b\u7684\u8d85\u7d1a\u4e0d\u8d77\u773c\u7684\u65b9\u6cd5\u53bb\u5b58\u53d6Modal\u7684\u5be6\u4f8b\u5c31\u53ef\u4ee5\u5c0dModal\u9032\u884c\u64cd\u4f5c\u3002<\/p>\n\n\n\n
var errorModal = document.getElementById('errorModal'); \/\/Modal\u7684id\nvar modal = Modal.getOrCreateInstance(errorModal); \/\/\u5275\u5efa\u6216\u53d6\u5f97modal\u5be6\u4f8b\nmodal.show();<\/code><\/pre>\n<\/div><\/div>\n\n\n\n\nErrorModal.vue \u7684\u914d\u7f6e<\/p>\n\n\n\n
\/\/template\n <!-- Modal -->\n <div class=\"modal fade ErrorModal\" id=\"errorModal\" tabindex=\"-1\" aria-labelledby=\"ModalLabel\" aria-hidden=\"true\">\n <div class=\"modal-dialog modal-dialog-centered\">\n <div class=\"modal-content\">\n <div class=\"modal-body\">\n <div v-html=\"error_msg[errorCode]\"><\/div>\n <\/div>\n <div class=\"modal-footer\">\n <button type=\"button\" class=\"btn btn-secondary\" data-bs-dismiss=\"modal\">\u78ba\u8a8d<\/button>\n <\/div>\n <\/div>\n <\/div>\n <\/div><\/code><\/pre>\n\n\n\n\/\/script\n props:[\"errorCode\"],\n data() {\n return {\n error_msg:{ \/\/error code \u5b9a\u7fa9\u5728\u9019\n axios_error:\"\u7121\u6cd5\u64f7\u53d6\u8cc7\u6599\u4f86\u6e90!!\",\n get_null:\"\u7121\u8cc7\u6599\u53ef\u986f\u793a!!\",\n \/\/ text_error:\"\u90e8\u5206\u6b04\u4f4d\u5167\u5bb9\u7570\u5e38\uff0c<br>\u8acb\u78ba\u8a8d\u8cc7\u6599\u586b\u5beb\u662f\u5426\u6b63\u78ba\",\n },\n };\n },<\/code><\/pre>\n<\/div><\/div>\n\n\n\n\napp.vue \u7684\u914d\u7f6e<\/p>\n\n\n\n
\/\/template\n<div id=\"app\" class=\"app\">\n <!-- \u5831\u932fmodal -->\n <ErrorModal \n :errorCode=\"errorCode\"\n \/>\n <ChildComponent \n @openErrorModal=\"openErrorModal\"\n \/>\n<\/div><\/code><\/pre>\n\n\n\nimport ErrorModal from '@\/components\/modal\/ErrorModal';\nimport ChildComponent from \"@\/views\/ChildComponent\";\ncomponents: {\n ErrorModal,\n ChildComponent\n},\ndata(){\n return{\n errorCode:\"axios_error\",\n }\n}\n...\nmethods:{\n openErrorModal(errorCd){\n this.errorCode = errorCd;\n var errorModal = document.getElementById('errorModal'); \n var modal = Modal.getOrCreateInstance(errorModal);\n modal.show();\n }\n}<\/code><\/pre>\n<\/div><\/div>\n\n\n\n\nChildComponent.vue \u7684\u914d\u7f6e<\/p>\n\n\n\n
\/\/template\n<div>\n <ul>\n <li v-for=\"(item, index) in dataList\" :key=\"index\">\n {{ item.name }}\n <\/li>\n <\/ul>\n <\/div><\/code><\/pre>\n\n\n\n\/\/script\n mounted() {\n this.axios.get(\"\/api\/ChooseDate\/list\").then(res => {\n if(res.data.array.length !== 0){\n this.dataList = res.data.array;\n }else{\n \/\/\u56de\u50b3\u9663\u5217\u70ba\u7a7a\u503c\u7684\u5831\u932f\u8207\u8a0a\u606fcode\n this.$emit('openErrorModal',\"get_null\");\n }\n }).catch(err=>{\n \/\/call api \u56de\u61c9\u932f\u8aa4\u7684\u5831\u932f\u8207\u8a0a\u606fcode\n this.$emit('openErrorModal',\"axios_error\");\n });\n },<\/code><\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"\u7531\u65bcBootstrap Vue\u53ea\u652f\u63f4\u5230vue 2.6\u591a\u7684\u7248\u672c\uff0c\u5c0e\u81f4\u5c07vue\u5347\u7d1a\u81f33.0\u7684\u6642\u5019\u7121\u6cd5\u4f7f\u7528\uff0c\u9700\u8981\u81ea\u5df1 […]<\/p>\n","protected":false},"author":1,"featured_media":2117,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[124],"tags":[123],"class_list":["post-2105","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-124","tag-vue","entry","has-media"],"yoast_head":"\n
Vue | \u7528\u7d14js\u63a7\u5236Bootstrap Modal\u900f\u904e\u4e8b\u4ef6\u89f8\u767c\u986f\u793a (\u975ebootstrap vue) · \u9019\u88e1\u662fYUKI<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n\n\n\n\t\n\t\n\t\n