Vue.js組件高級(jí)特性實(shí)例詳解
本文實(shí)例講述了Vue.js組件高級(jí)特性。分享給大家供大家參考,具體如下:
1 遞歸
為組件設(shè)置 name 屬性,這個(gè)組件就可以在自身的模板內(nèi)遞歸調(diào)用自己。
html:
<div id="app"> <deniro-component :count="1"></deniro-component> </div>
js:
Vue.component('deniro-component',{
name:'deniro-component',
props:{
count:{
type:Number,
default:1
}
},
template:'\
<div class="child">\
<p>{{count}} 微信大變樣!看了這些新功能后,網(wǎng)友淡定不住了</p>\
<deniro-component\
:count="count + 1"\
v-if="count < 3"></deniro-component>\
</div>\
'
});
var app = new Vue({
el: '#app',
data: {}
});
效果:

渲染結(jié)果:

可以利用組件的可遞歸特性,來實(shí)現(xiàn)一些具有不確定層級(jí)的組件,比如級(jí)聯(lián)選擇器和樹型組件。
2 內(nèi)聯(lián)模板
一般情況下,組件的模板都是在 template 中定義的。我們也可以在組件標(biāo)簽中加上 inline-template 屬性,這樣組件就會(huì)把它的內(nèi)容作為實(shí)際的模板內(nèi)容。
內(nèi)聯(lián)模板可以接收父、子組件中聲明的數(shù)據(jù),所以更加靈活。
html:
<div id="app2">
<deniro-component2 inline-template>
<div>
<h2>父組件中定義子組件模板</h2>
<p>{{content1}}</p>
<p>{{content2}}</p>
</div>
</deniro-component2>
</div>
js:
Vue.component('deniro-component2',{
data:function () {
return {
content1:'雙屏手機(jī)一碰就碎?實(shí)測(cè)結(jié)果意外(來源于子組件)'
}
}
});
var app2 = new Vue({
el: '#app2',
data: {
content2:'AI正在改變所有行業(yè) 咖啡也將被消滅(來源于父組件)'
}
});
渲染結(jié)果:
<div id="app2"> <div> <h2>父組件中定義子組件模板</h2> <p>雙屏手機(jī)一碰就碎?實(shí)測(cè)結(jié)果意外(來源于子組件)</p> <p>AI正在改變所有行業(yè) 咖啡也將被消滅(來源于父組件)</p> </div> </div>
如果父子組件定義的數(shù)據(jù)同名,那么優(yōu)先使用子組件中的數(shù)據(jù)。
因?yàn)樽饔糜蜉^難理解,所以除非必要,否則不建議使用。
3 動(dòng)態(tài)加載
我們可以使用 is 來實(shí)現(xiàn)動(dòng)態(tài)掛載組件。
html:
<div id="app3">
<deniro-component3 :is="currentView"></deniro-component3>
<button @click="change('A')">切換到 A 組件</button>
<button @click="change('B')">切換到 B 組件</button>
<button @click="change('C')">切換到 C 組件</button>
</div>
js:
var app3 = new Vue({
el: '#app3',
components: {
componentA: {
template: '<div>組件 A</div>'
},
componentB: {
template: '<div>組件 B</div>'
},
componentC: {
template: '<div>組件 C</div>'
}
},
data: {
currentView: 'componentA'
},
methods: {
change: function (component) {
this.currentView = 'component' + component;
}
}
});
效果:

data 中的 is 變量也可以直接綁定組件對(duì)象。
html:
<div id="app4"> <deniro-component4 :is="currentView"></deniro-component4> </div>
js:
var news={
template:'<p>無人機(jī)送快遞 漸行漸近</p>'
}
var app4 = new Vue({
el: '#app4',
data: {
currentView: news
}
});
渲染結(jié)果:
<div id="app4"> <p>無人機(jī)送快遞 漸行漸近</p> </div>
4 異步加載
當(dāng)工程變得越來越大時(shí),就需要考慮性能嘍。我們可以把組件定義成一個(gè)工廠函數(shù),實(shí)現(xiàn)組件動(dòng)態(tài)解析。Vue.js 會(huì)把本次渲染后的結(jié)果緩存起來,從而提高性能。
html:
<div id="app5"> <deniro-component5></deniro-component5> </div>
js:
Vue.component('deniro-component5', function (resolve,reject) {
window.setTimeout(function () {
resolve({
template:'<div>全球首個(gè)5G電話撥通</div>'
});
},1000);
});
var app5 = new Vue({
el: '#app5'
});
效果:

這里使用 setTimeout 來模擬異步下載,下載成功后會(huì)調(diào)用 resolve 方法。
一般情況下,會(huì)把組件的配置定義為對(duì)象配置,然后通過 Ajax 請(qǐng)求組件配置信息,最后通過 resolve 傳入這些配置。
完整實(shí)例代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>vue.js組件高級(jí)特性</title>
<script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
</head>
<body>
<div id="app">
<deniro-component :count="1"></deniro-component>
</div>
<div id="app2">
<deniro-component2 inline-template>
<div>
<h2>父組件中定義子組件模板</h2>
<p>{{content1}}</p>
<p>{{content2}}</p>
</div>
</deniro-component2>
</div>
<div id="app3">
<deniro-component3 :is="currentView"></deniro-component3>
<button @click="change('A')">切換到 A 組件</button>
<button @click="change('B')">切換到 B 組件</button>
<button @click="change('C')">切換到 C 組件</button>
</div>
<div id="app4">
<deniro-component4 :is="currentView"></deniro-component4>
</div>
<div id="app5">
<deniro-component5></deniro-component5>
</div>
<script>
Vue.component('deniro-component5', function (resolve,reject) {
window.setTimeout(function () {
resolve({
template:'<div>全球首個(gè)5G電話撥通</div>'
});
},1000);
});
var app5 = new Vue({
el: '#app5'
});
var news={
template:'<p>無人機(jī)送快遞 漸行漸近</p>'
}
var app4 = new Vue({
el: '#app4',
data: {
currentView: news
}
});
var app3 = new Vue({
el: '#app3',
components: {
componentA: {
template: '<div>組件 A</div>'
},
componentB: {
template: '<div>組件 B</div>'
},
componentC: {
template: '<div>組件 C</div>'
}
},
data: {
currentView: 'componentA'
},
methods: {
change: function (component) {
this.currentView = 'component' + component;
}
}
});
Vue.component('deniro-component2', {
data: function () {
return {
content1: '雙屏手機(jī)一碰就碎?實(shí)測(cè)結(jié)果意外(來源于子組件)'
}
}
});
var app2 = new Vue({
el: '#app2',
data: {
content2: 'AI正在改變所有行業(yè) 咖啡也將被消滅(來源于父組件)'
}
});
Vue.component('deniro-component', {
name: 'deniro-component',
props: {
count: {
type: Number,
default: 1
}
},
template: '\
<div class="child">\
<p>{{count}} 微信大變樣!看了這些新功能后,網(wǎng)友淡定不住了</p>\
<deniro-component\
:count="count + 1"\
v-if="count < 3"></deniro-component>\
</div>\
'
});
var app = new Vue({
el: '#app',
data: {}
});
</script>
</body>
</html>
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun 測(cè)試上述代碼運(yùn)行效果。
希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。
相關(guān)文章
詳解vue-cli本地環(huán)境API代理設(shè)置和解決跨域
這篇文章主要介紹了詳解vue-cli本地環(huán)境API代理設(shè)置和解決跨域,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
vue組件實(shí)現(xiàn)可搜索下拉框擴(kuò)展
這篇文章主要為大家詳細(xì)介紹了vue組件實(shí)現(xiàn)可搜索下拉框的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
vue?el-date-picker?日期回顯后無法改變問題解決
這篇文章主要介紹了vue?el-date-picker?日期回顯后無法改變問題解決,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
Vue3實(shí)現(xiàn)LuckSheet在線預(yù)覽Excel表格
在前端開發(fā)中預(yù)覽Excel文件是常見的需求之一,本文將介紹如何使用Vue.js框架以及兩個(gè)優(yōu)秀的Excel庫——LuckyExcel和Luckysheet,來實(shí)現(xiàn)Excel文件在線預(yù)覽功能,希望對(duì)大家有所幫助2023-11-11
詳解vue-cli快速構(gòu)建vue應(yīng)用并實(shí)現(xiàn)webpack打包
這篇文章主要介紹了詳解vue-cli快速構(gòu)建vue應(yīng)用并實(shí)現(xiàn)webpack打包,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
el-table樹形數(shù)據(jù)序號(hào)排序處理方案
這篇文章主要介紹了el-table樹形數(shù)據(jù)序號(hào)排序處理方案,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-03-03
Vue中的scoped實(shí)現(xiàn)原理及穿透方法
這篇文章主要介紹了Vue中的scoped實(shí)現(xiàn)原理及穿透方法,本文通過實(shí)例文字相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-05-05

