模态框是咱们 UI
控件中一个很重要的组件,使用场景有不少种,咱们在 Vue 组件中建立模态框组件而用到的一个知识点是利用 Vue.extend
来建立。
文档中的解释是javascript
在最近在作一个经常使用的相似下面的登陆/注册
业务场景时,利用 Vue.extend
来改善咱们的代码,使咱们代码逻辑更清晰化。vue
需求:点击登陆或注册出现各自的模态框。java
咱们对于这种常见的登陆注册业务,通常都是分为 Sigin.vue
和 Register.vue
两个组件,而后把两个组件写入 App.vue 组件中,或者是 layout.vue
组件中。git
原来的这种使用,对于咱们的整块的登陆注册逻辑是分散的,一些须要登陆或者是权限的逻辑,可能都须要特地去提取一个 Visible
来控制咱们的登陆框。github
使用 Vue.extend
能够达到统一接口,不用逻辑分散,下面的示例,仅做参考,不了解该 api 使用的能够了解下,而了解的,欢迎指导😃vue-cli
新建 LoginModel
目录,新建 Sigin.vue
和 Register.vue
两个组件api
<template> <div>登陆</div> </template> <template> <div>注册</div> </template>
再新建 index.vue
组件app
<template> <div v-if="show"> <Sigin v-if="type === 'sigin'" @sigin="loginCallback" /> <Register v-if="type === 'register'" @register="loginCallback" /> </div> </template> <script> import Sigin from "./sigin"; import Register from "./register"; export default { components: { Register, Sigin }, data() { return { show: false, type: "sigin" }; } }; </script>
新建 index.js
,导入咱们的 index.vue
函数
import Vue from "vue"; import ModalCops from "./index.vue"; const LoginModal = Vue.extend(ModalCops); // 建立 Vue 子类 let instance; const ModalBox = (options = {}) => { if (instance) { instance.doClose(); } // 实例化 instance = new LoginModal({ data: { show: true, // 实例化后显示 ...options } }); instance.$mount(); document.body.appendChild(instance.$el); // 将模态框添加至 body return instance; }; // 对应的登陆 ModalBox.sigin = () => { return ModalBox({ type: "sigin" }); }; ModalBox.register = () => { return ModalBox({ type: "register" }); }; export default { install(Vue) { Vue.prototype.$loginer = ModalBox; } };
建立完成后,咱们能够在入口挂载到 Vue 实例上this
// main.js import LoginModal from "./components/LoginModal"; Vue.use(LoginModal);
在须要登陆/注册的地方只用调用
<div> <a href="javascript:;" @click="onLogin('sigin')">登陆</a> / <a href="javascript:;" @click="onLogin('register')">注册</a> </div> onLogin(type) { this.$loginer({ type }) }
效果以下
咱们都知道模态框须要关闭事件,而像这种业务的关闭事件必然是须要验证提交信息,因此咱们须要加上关闭回调函数。
修改 Sigin.vue
和 Register.vue
两个组件,添加事件
// Sigin.vue <template> <div> <button @click="onClick">登陆确认</button> </div> </template> <script> export default { name: "Sigin", methods: { onClick() { this.$emit("sigin"); } } }; </script> // Register.vue <template> <button @click="onClick">注册确认</button> </template> <script> export default { name: "Register", methods: { onClick() { this.$emit("register"); } } }; </script>
修改 index.vue
添加 $emit
事件
<template> <div v-if="show"> <Sigin v-if="type === 'sigin'" @sigin="loginCallback" /> <Register v-if="type === 'register'" @register="loginCallback" /> </div> </template> <script> import Sigin from "./sigin"; import Register from "./register"; export default { components: { Register, Sigin }, data() { return { show: false, type: "sigin" }; }, methods: { loginCallback() { if (!this.ok) return; this.ok().then(valid => { if (valid) { this.doClose(); } }); }, doClose() { this.show = false; } } }; </script>
修改 index.js
文件
import Vue from "vue"; import ModalCops from "./index.vue"; const LoginModal = Vue.extend(ModalCops); let instance; const ModalBox = (options = {}) => { if (instance) { instance.doClose(); } instance = new LoginModal({ data: { show: true, ...options } }); instance.ok = () => { return new Promise(resolve => { const before = options.ok ? options.ok() : false; if (before && before.then) { before.then( () => resolve(true), () => { console.log("reject"); } ); } else if (typeof before === "boolean" && before !== false) { resolve(true); } }); }; instance.$mount(); document.body.appendChild(instance.$el); return instance; }; ModalBox.sigin = ok => { return ModalBox({ type: "sigin", ok }); }; ModalBox.register = ok => { return ModalBox({ type: "register", ok }); }; ModalBox.close = () => { instance.doClose(); instance.show = false; }; export default { install(Vue) { Vue.prototype.$loginer = ModalBox; } };
使用回调
onLogin(type) { const funcs = { sigin: () => { console.log("登陆请求"); }, register: () => { console.log("注册请求"); } }; this.$loginer({ type, ok: () => { return new Promise((resolve, reject) => { // isOk 验证数据是否正确 if (this.isOk) { funcs[type](); resolve(); } else { reject(); } }); } }); }
效果以下
本文代码地址