說(shuō)說(shuō)Vue.js中的functional函數(shù)化組件的使用
Vue.js 組件提供了一個(gè) functional 開(kāi)關(guān),設(shè)置為 true 后,就可以讓組件變?yōu)闊o(wú)狀態(tài)、無(wú)實(shí)例的函數(shù)化組件。因?yàn)橹皇呛瘮?shù),所以渲染的開(kāi)銷(xiāo)相對(duì)來(lái)說(shuō),較小。
函數(shù)化的組件中的 Render 函數(shù),提供了第二個(gè)參數(shù) context 作為上下文,data、props、slots、children 以及 parent 都可以通過(guò) context 來(lái)訪(fǎng)問(wèn)。
1 示例
這里,我們用 functional 函數(shù)化組件來(lái)實(shí)現(xiàn)一個(gè)智能組件。
html:
<div id="app">
<smart-component :data="data"></smart-component>
<button @click="change('img')">圖片組件</button>
<button @click="change('video')">視頻組件</button>
<button @click="change('text')">文本組件</button>
</div>
js:
//圖片組件設(shè)置
var imgOptions = {
props: ['data'],
render: function (createElement) {
return createElement('div', [
createElement('p', '圖片組件'),
createElement('img', {
attrs: {
src: this.data.url,
height: 300,
weight: 400
}
})
]);
}
};
//視頻組件設(shè)置
var videoOptions = {
props: ['data'],
render: function (createElement) {
return createElement('div', [
createElement('p', '視頻組件'),
createElement('video', {
attrs: {
src: this.data.url,
controls: 'controls',
autoplay: 'autoplay'
}
})
]);
}
};
//文本組件設(shè)置
var textOptions = {
props: ['data'],
render: function (createElement) {
return createElement('div', [
createElement('p', '文本組件'),
createElement('p', this.data.content)
]);
}
};
Vue.component('smart-component', {
//設(shè)置為函數(shù)化組件
functional: true,
render: function (createElement, context) {
function get() {
var data = context.props.data;
console.log("smart-component/type:" + data.type);
//判斷是哪一種類(lèi)型的組件
switch (data.type) {
case 'img':
return imgOptions;
case 'video':
return videoOptions;
case 'text':
return textOptions;
default:
console.log("data 類(lèi)型不合法:" + data.type);
}
}
return createElement(
get(),
{
props: {
data: context.props.data
}
},
context.children
)
},
props: {
data: {
type: Object,
required: true
}
}
})
var app = new Vue({
el: '#app',
data: {
data: {}
},
methods: {
change: function (type) {
console.log("輸入類(lèi)型:" + type);
switch (type) {
case 'img':
this.data = {
type: 'img',
url: 'http://pic-bucket.ws.126.net/photo/0001/2019-02-07/E7D8PON900AO0001NOS.jpg'
}
break;
case 'video':
this.data = {
type: 'video',
url: 'http://wxapp.cp31.ott.cibntv.net/6773887A7F94A71DF718E212C/03002002005B836E73A0C5708529E09C1952A1-1FCF-4289-875D-AEE23D77530D.mp4?ccode=0517&duration=393&expire=18000&psid=bbd36054f9dd2b21efc4121e320f05a0&ups_client_netip=65600b48&ups_ts=1549519607&ups_userid=21780671&utid=eWrCEmi2cFsCAWoLI41wnWhW&vid=XMzc5OTM0OTAyMA&vkey=A1b479ba34ca90bcc61e3d6c3b2da5a8e&iv=1&sp='
}
break;
case 'text':
this.data = {
type: 'text',
content: '《流浪地球》中的科學(xué):太陽(yáng)何時(shí)吞并地球?科學(xué)家已經(jīng)給出時(shí)間表'
}
break;
default:
console.log("data 類(lèi)型不合法:" + type);
}
}
},
created: function () {
//默認(rèn)為圖片組件
this.change('img');
}
});
效果:

- 首先定義了圖片組件設(shè)置對(duì)象、視頻組件設(shè)置對(duì)象以及文本組件設(shè)置對(duì)象,它們都以 data 作為入?yún)ⅰ?/li>
- 函數(shù)化組件 smart-component,也以 data 作為入?yún)?。?nèi)部根據(jù) get() 函數(shù)來(lái)判斷需要渲染的組件類(lèi)型。
- 函數(shù)化組件 smart-component 的 render 函數(shù),以 get() 作為第一個(gè)參數(shù);以 smart-component 所傳入的 data,作為第二個(gè)參數(shù):
return createElement(
get(),
{
props: {
data: context.props.data
}
},
context.children
)
根實(shí)例 app 的 change 方法,根據(jù)輸入的類(lèi)型,來(lái)切換不同的組件所需要的數(shù)據(jù)。
2 應(yīng)用場(chǎng)景
函數(shù)化組件不常用,它應(yīng)該應(yīng)用于以下場(chǎng)景:
- 需要通過(guò)編程實(shí)現(xiàn)在多種組件中選擇一種。
- children、props 或者 data 在傳遞給子組件之前,處理它們。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- javascript function(函數(shù)類(lèi)型)使用與注意事項(xiàng)小結(jié)
- JavaScript函數(shù)式編程(Functional Programming)組合函數(shù)(Composition)用法分析
- JavaScript函數(shù)式編程(Functional Programming)箭頭函數(shù)(Arrow functions)用法分析
- JavaScript函數(shù)式編程(Functional Programming)高階函數(shù)(Higher order functions)用法分析
- JavaScript函數(shù)式編程(Functional Programming)純函數(shù)用法分析
- JavaScript函數(shù)式編程(Functional Programming)聲明式與命令式實(shí)例分析
- JS中注入eval, Function等系統(tǒng)函數(shù)截獲動(dòng)態(tài)代碼
- JavaScript的function函數(shù)詳細(xì)介紹
相關(guān)文章
vite+ts vite.config.ts使用path報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了vite+ts vite.config.ts使用path報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue?3?表格時(shí)間監(jiān)控與動(dòng)態(tài)后端請(qǐng)求觸發(fā)詳解?附Demo展示
在Vue3中,使用el-table組件渲染表格數(shù)據(jù),通過(guò)el-table-column指定內(nèi)容,時(shí)間點(diǎn)需前端校準(zhǔn),用getTime()比較,到達(dá)時(shí)觸發(fā)操作,異步API請(qǐng)求可用async/await處理,setInterval實(shí)現(xiàn)定時(shí)監(jiān)控,配合條件判斷防止重復(fù)請(qǐng)求2024-09-09
vue自適應(yīng)布局postcss-px2rem詳解
這篇文章主要介紹了vue自適應(yīng)布局(postcss-px2rem)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-05-05
關(guān)于Vue-extend和VueComponent問(wèn)題小結(jié)
這篇文章主要介紹了Vue-extend和VueComponent問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
詳解VUE 對(duì)element-ui中的ElTableColumn擴(kuò)展
本篇文章主要介紹了詳解VUE 對(duì)element-ui中的ElTableColumn擴(kuò)展,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
nginx如何配置vue項(xiàng)目history的路由模式(非根目錄)
這篇文章主要介紹了nginx如何配置vue項(xiàng)目history的路由模式(非根目錄),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue實(shí)現(xiàn)表格數(shù)據(jù)的增刪改查的示例代碼
Vue是一個(gè)用于構(gòu)建用戶(hù)界面的JavaScript框架,在Vue中可以通過(guò)使用Vue組件來(lái)實(shí)現(xiàn)對(duì)表格的增刪改查操作,下面將介紹一些基礎(chǔ)的Vue組件和技術(shù)來(lái)實(shí)現(xiàn)對(duì)表格的增刪改查操作,需要的朋友可以參考下2024-08-08

