Vue extend學(xué)習(xí)示例講解
vue中通過extend動(dòng)態(tài)創(chuàng)建全局組件;
1、什么是動(dòng)態(tài)創(chuàng)建組件
只有在觸發(fā)事件的時(shí)候,才產(chǎn)生某組件,平時(shí)它并不存在;
2、Vue.extend()
使用基礎(chǔ) Vue 構(gòu)造器,創(chuàng)建一個(gè)“子類”。參數(shù)是一個(gè)包含組件選項(xiàng)的對(duì)象;其實(shí)就是一個(gè)子類構(gòu)造器是Vue組件的核心api,實(shí)現(xiàn)思路就是使用原型繼承的方法返回了Vue的子類,并且利用mergeOptions把傳入組件的options和父類的options進(jìn)行了合并。
extend創(chuàng)建的是一個(gè)組件構(gòu)造器,而不是一個(gè)具體的實(shí)例;
接收一個(gè)對(duì)象(包含組件選項(xiàng)的對(duì)象)作為參數(shù),需要使用new來創(chuàng)建實(shí)例,并且需要$mount手動(dòng)掛載到一個(gè)元素上,才可以獲取到這個(gè)元素的相應(yīng)的信息。
- 脫離填鴨式的寫法;代碼自由
- 代碼復(fù)用,解耦
- 原生JS語法結(jié)合vue(jsx)
- 通過傳入?yún)?shù),可以顯示不同狀態(tài)的模板
基礎(chǔ)用法:
<div id="mount-point"></div>
// 創(chuàng)建構(gòu)造器
/* Vue.extend( options )
參數(shù):{Object} options
用法:使用基礎(chǔ) Vue 構(gòu)造器,創(chuàng)建一個(gè)“子類”。參數(shù)是一個(gè)包含組件選項(xiàng)的對(duì)象;
data 選項(xiàng)是特例,需要注意: 在 Vue.extend() 中它必須是函數(shù);*/
var Profile = Vue.extend({
template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>',
data: function () {
return {
firstName: 'Walter',
lastName: 'White',
alias: 'Heisenberg'
}
}
})
// 創(chuàng)建 Profile 實(shí)例,并掛載到一個(gè)元素上。
new Profile().$mount('#mount-point')
// 結(jié)果如下:
<p>Walter White aka Heisenberg</p>
/*
可以看到,extend 創(chuàng)建的是 Vue 構(gòu)造器,而不是我們平時(shí)常寫的組件實(shí)例,所以不可以通過 new Vue({ components: testExtend }) 來直接使用,需要通過 new Profile().$mount('#mount-point') 來掛載到指定的元素上。
*/
3、通過extend實(shí)現(xiàn)彈窗的動(dòng)態(tài)創(chuàng)建

3.1、創(chuàng)建動(dòng)態(tài)組件
<!--動(dòng)態(tài)組件的模板-->
<template>
<!-- 可以用MessageBox做蒙塵-->
<div :class="['MessageBox',type]">
<div class="inner">
<header class="header">
<h1 class="title">{{ title }}</h1>
<span @click="$messageBox.hide()">x</span>
</header>
<div class="content">{{ content }}</div>
</div>
</div>
</template>
<script>
export default {
name: "MessageBox",
props: {
title: {
type: String,
default: "this is title",
},
content: {
type: String,
default: "this is content",
},
type: {
type: String,
default: "primary",
//檢測(cè)傳進(jìn)來的類型是否是這四種,通過ES6提供的includes方法模糊查詢
validator(value) {
return [
"primary",
"success",
"warn",
"danger"
].includes(value);
}
}
}
}
</script>
<style scoped lang="less">
.MessageBox {
position: fixed;
left: 50%;
top: 0;
//蒙塵的大小設(shè)置
width: 50%;
height: 400px;
background-color: rgba(0, 0, 0, .5);
//不同彈窗的樣式
&.primary {
.header {
background-color: blue;
color: #fff;
}
}
&.success {
.header {
background-color: green;
color: #fff;
}
}
&.warn {
.header {
background-color: rgba(255, 138, 71, 0.96);
color: #fff;
}
}
&.danger {
.header {
background-color: red;
color: #fff;
}
}
.inner {
position: absolute;
top: 100px;
left: 50%;
width: 500px;
margin-left: -250px;
background-color: #fff;
box-shadow: 1px 3px 5px #ddd;
border-radius: 5px;
overflow: hidden;
.header {
height: 44px;
padding: 0 10px;
line-height: 44px;
box-sizing: border-box;
h1 {
margin: 0;
font-weight: normal;
}
.title {
font-size: 16px;
float: left;
}
span {
//將鼠標(biāo)改為小手樣式
cursor: pointer;
float: right;
}
}
.content {
padding: 20px;
box-sizing: border-box;
}
}
}
</style>3.2、編輯動(dòng)態(tài)組件的邏輯
//引入需要?jiǎng)討B(tài)創(chuàng)建的模板
import _MessageBox from "@/components/messageBox/MessageBox";
export default {
//install開發(fā)插件的方法,install帶有Vue的構(gòu)造器,可以使用Vue.extend,和Vue.component(注冊(cè)組件)
//在Vue.use的時(shí)候就會(huì)調(diào)用這個(gè)install
install(Vue) {
let messageBox = null;
//使用Vue.component全局注冊(cè)組件
Vue.component(_MessageBox.name, _MessageBox);
//將方法添加到Vue的prototype屬性中,這樣實(shí)例就可以繼承里面的方法
Vue.prototype.$messageBox = {
show, hide,
info({title, content, type}, callback) {
this.show({title, content, type: "primary"}, callback)
},
success({title, content, type}, callback) {
this.show({title, content, type: "success"}, callback)
},
warn({title, content, type}, callback) {
this.show({title, content, type: "warn"}, callback)
},
danger({title, content, type}, callback) {
this.show({title, content, type: "danger"}, callback)
}
}
//顯示彈窗
function show(props, callback) {
//判斷這個(gè)組件是否存在,如果不存在
if (!messageBox) {
//生成構(gòu)造函數(shù)、構(gòu)造器
const MessageBox = Vue.extend({
/*
render該渲染函數(shù)接收一個(gè) createElement 方法作為第一個(gè)參數(shù)用來創(chuàng)建 VNode(節(jié)點(diǎn))。
如果組件是一個(gè)函數(shù)組件,渲染函數(shù)還會(huì)接收一個(gè)額外的 context 參數(shù),為沒有實(shí)例的函數(shù)組件提供上下文信息。
*/
//此處傳入的是一個(gè)函數(shù)組件,所以渲染的函數(shù)還可以額外接收一個(gè)參數(shù)
render(h) {
//h函數(shù)就是vue中的createElement函數(shù),這個(gè)函數(shù)作用就是創(chuàng)建虛擬dom,追蹤dom變化的
return h("MessageBox", {
//用于接收傳遞的參數(shù)
props: {...props}
})
}
});
//將動(dòng)態(tài)模板組件實(shí)例化
messageBox = new MessageBox();
//將這個(gè)實(shí)例手動(dòng)掛載,掛載后可以通過$el獲取這個(gè)元素
this.vm = messageBox.$mount();
//將組件添加到body上,脫離了根節(jié)點(diǎn),不在"id=app中"
document.body.appendChild(this.vm.$el)
callback && callback();
}
}
//關(guān)閉彈窗
function hide(callback) {
//移出這個(gè)組件
document.body.removeChild(this.vm.$el);
//將這個(gè)實(shí)例銷毀
messageBox.$destroy();
messageBox = null;
this.vm = null;
//如果存在才會(huì)執(zhí)行
callback && callback();
}
}
}3.3、在main.js中引入使用
import Vue from 'vue'
import App from './App.vue'
//1、引入
import MessageBox from "@/components/messageBox";
//2、全局注冊(cè)使用
Vue.use(MessageBox);
new Vue({
render: h => h(App)
}).$mount('#app')3.4、在需要的地方通過觸發(fā)事件顯示彈窗
<template>
<div>
<button @click="showMessageBox">show</button>
<button @click="showInfoMessageBox">info</button>
<button @click="showSuccessMessageBox">success</button>
<button @click="showWarnMessageBox">warn</button>
<button @click="showDangerMessageBox">danger</button>
</div>
</template>
<script>
export default {
name: "Extend",
methods: {
//通過this.$messageBox可以訪問到Vue實(shí)例的屬性和方法
showMessageBox() {
this.$messageBox.success({
title: 'App',
content: 'this is content of extend study',
type: 'success'
}, () => {
console.log('show over')
})
},
showInfoMessageBox() {
this.$messageBox.info({
title: 'App',
content: 'this is content of extend study',
}, () => {
console.log('info over')
})
},
showSuccessMessageBox() {
this.$messageBox.success({
title: 'App',
content: 'this is content of extend study',
type: 'success'
}, () => {
console.log('success over')
})
},
showWarnMessageBox() {
this.$messageBox.warn({
title: 'App',
content: 'this is content of extend study',
type: 'warn'
}, () => {
console.log('warn over')
})
},
showDangerMessageBox() {
this.$messageBox.danger({
title: 'App',
content: 'this is content of extend study',
type: 'danger'
})
}
}
}
</script>3.5、效果圖




到此這篇關(guān)于Vue extend學(xué)習(xí)示例講解的文章就介紹到這了,更多相關(guān)Vue extend內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue extends 屬性的用法示例詳解
- Vue extend使用示例深入分析
- 關(guān)于Vue-extend和VueComponent問題小結(jié)
- Vue.extend實(shí)現(xiàn)組件庫(kù)message組件示例詳解
- vue使用Vue.extend方法仿寫個(gè)loading加載中效果實(shí)例
- 如何巧用Vue.extend繼承組件實(shí)現(xiàn)el-table雙擊可編輯(不使用v-if、v-else)
- vue中使用mixins/extends傳入?yún)?shù)的方式
- vue?extend+promise封裝全局彈窗組件
- Vue.extend 登錄注冊(cè)模態(tài)框的實(shí)現(xiàn)
相關(guān)文章
Vue3+Vite中不支持require的方式引入本地圖片的解決方案
這篇文章主要介紹了Vue3+Vite中不支持require的方式引入本地圖片的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Vuex中Store的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了Vuex中Store的簡(jiǎn)單實(shí)現(xiàn),為了在 Vue 組件中訪問 this.$store property,你需要為 Vue 實(shí)例提供創(chuàng)建好的 store,Vuex 提供了一個(gè)從根組件向所有子組件,以 store 選項(xiàng)的方式 注入 該 store 的機(jī)制,需要的朋友可以參考下2023-11-11
Nuxt3+ElementPlus構(gòu)建打包部署全過程
網(wǎng)上大部分關(guān)于Nuxt打包部署教程可謂是可以用五花八門來形容,這對(duì)于第一次接觸的朋友簡(jiǎn)直是無從下手,這篇文章主要給大家介紹了關(guān)于Nuxt3+ElementPlus構(gòu)建打包部署的相關(guān)資料,需要的朋友可以參考下2023-01-01
vue+Element?ui實(shí)現(xiàn)照片墻效果
這篇文章主要為大家詳細(xì)介紹了vue+Element?ui實(shí)現(xiàn)照片墻效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Vue3封裝ElImageViewer預(yù)覽圖片的示例代碼
本文主要介紹了Vue3封裝ElImageViewer預(yù)覽圖片的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Vue+WebSocket頁(yè)面實(shí)時(shí)刷新長(zhǎng)連接的實(shí)現(xiàn)
最近vue項(xiàng)目要做數(shù)據(jù)實(shí)時(shí)刷新,數(shù)據(jù)較大,會(huì)出現(xiàn)卡死情況,所以本文主要介紹了頁(yè)面實(shí)時(shí)刷新長(zhǎng)連接,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06

