本篇文章為大家展示了利用Vue.extend 怎么制作一個(gè)登錄注冊(cè)框,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

模態(tài)框是我們UI 控件中一個(gè)很重要的組件,使用場(chǎng)景有很多種,我們?cè)?Vue 組件中創(chuàng)建模態(tài)框組件而用到的一個(gè)知識(shí)點(diǎn)是利用Vue.extend 來(lái)創(chuàng)建。
文檔中的解釋是

在最近在做一個(gè)常用的類(lèi)似下面的登錄/注冊(cè) 業(yè)務(wù)場(chǎng)景時(shí),利用Vue.extend 來(lái)改善我們的代碼,使我們代碼邏輯更清晰化。

需求:點(diǎn)擊登錄或注冊(cè)出現(xiàn)各自的模態(tài)框。
我們對(duì)于這種常見(jiàn)的登錄注冊(cè)業(yè)務(wù),一般都是分為Sigin.vue 和Register.vue 兩個(gè)組件,然后把兩個(gè)組件寫(xiě)入 App.vue 組件中,或者是layout.vue 組件中。
原來(lái)的這種使用,對(duì)于我們的整塊的登錄注冊(cè)邏輯是分散的,一些需要登錄或者是權(quán)限的邏輯,可能都需要特意去提取一個(gè)Visible 來(lái)控制我們的登錄框。
使用Vue.extend 可以達(dá)到統(tǒng)一接口,不用邏輯分散,下面的示例,僅作參考,不了解該 api 使用的可以了解下,而了解的,歡迎指導(dǎo):smiley:
新建LoginModel 目錄,新建Sigin.vue 和Register.vue 兩個(gè)組件
<template> <div>登錄</div> </template> <template> <div>注冊(cè)</div> </template>
再新建index.vue 組件
<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 ,導(dǎo)入我們的index.vue
import Vue from "vue";
import ModalCops from "./index.vue";
const LoginModal = Vue.extend(ModalCops); // 創(chuàng)建 Vue 子類(lèi)
let instance;
const ModalBox = (options = {}) => {
if (instance) {
instance.doClose();
}
// 實(shí)例化
instance = new LoginModal({
data: {
show: true, // 實(shí)例化后顯示
...options
}
});
instance.$mount();
document.body.appendChild(instance.$el); // 將模態(tài)框添加至 body
return instance;
};
// 對(duì)應(yīng)的登錄
ModalBox.sigin = () => {
return ModalBox({
type: "sigin"
});
};
ModalBox.register = () => {
return ModalBox({
type: "register"
});
};
export default {
install(Vue) {
Vue.prototype.$loginer = ModalBox;
}
};創(chuàng)建完成后,我們可以在入口掛載到 Vue 實(shí)例上
// main.js import LoginModal from "./components/LoginModal"; Vue.use(LoginModal);
在需要登錄/注冊(cè)的地方只用調(diào)用
<div>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" @click="onLogin('sigin')">登錄</a>
/
<a href="javascript:;" rel="external nofollow" rel="external nofollow" @click="onLogin('register')">注冊(cè)</a>
</div>
onLogin(type) {
this.$loginer({
type
})
}效果如下

我們都知道模態(tài)框需要關(guān)閉事件,而像這種業(yè)務(wù)的關(guān)閉事件必然是需要驗(yàn)證提交信息,所以我們需要加上關(guān)閉回調(diào)函數(shù)。
修改Sigin.vue 和Register.vue 兩個(gè)組件,添加事件
// Sigin.vue
<template>
<div>
<button @click="onClick">登錄確認(rèn)</button>
</div>
</template>
<script>
export default {
name: "Sigin",
methods: {
onClick() {
this.$emit("sigin");
}
}
};
</script>
// Register.vue
<template>
<button @click="onClick">注冊(cè)確認(rèn)</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;
}
};使用回調(diào)
onLogin(type) {
const funcs = {
sigin: () => {
console.log("登錄請(qǐng)求");
},
register: () => {
console.log("注冊(cè)請(qǐng)求");
}
};
this.$loginer({
type,
ok: () => {
return new Promise((resolve, reject) => {
// isOk 驗(yàn)證數(shù)據(jù)是否正確
if (this.isOk) {
funcs[type]();
resolve();
} else {
reject();
}
});
}
});
}上述內(nèi)容就是利用Vue.extend 怎么制作一個(gè)登錄注冊(cè)框,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標(biāo)題:利用Vue.extend怎么制作一個(gè)登錄注冊(cè)框-創(chuàng)新互聯(lián)
URL地址:http://chinadenli.net/article16/dpiodg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、標(biāo)簽優(yōu)化、小程序開(kāi)發(fā)、搜索引擎優(yōu)化、網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容