vue中this.$createElement方法的使用
vue this.$createElement方法
element ui中的slider的marks屬性中使用到this.$createElement方法設(shè)置標記樣式:

上面雖然只用到兩個參數(shù),實際上,此方法有三個參數(shù):
①第一個參數(shù)為標簽,即創(chuàng)建的節(jié)點元素的標簽是什么
②第二個參數(shù)是屬性配置,如class、style等
③第三個參數(shù)是節(jié)點元素的內(nèi)容
this.$createElement方法的寫法
const h = this.$createElement;
this.$info({
title: 'This is a notification message',
content: h('div', {}, [
h('p', 'some messages...some messages...'),
h('p', 'some messages...some messages...'),
]),
onOk() {},
});生成的對話框如下:

使用第二個參數(shù)進行屬性配置,例如以下代碼:
h('div', { class: 'test' }, [
h('p', 'some messages...some messages...'),
h('p', 'some messages...some messages...')
])生成的是:

可以看出,dom元素上多了class屬性
再試試以下代碼:
h('div',
{
style: {
color: 'green'
}
}, [
h('p', 'some messages...some messages...'),
h('p', 'some messages...some messages...')
])生成的對話框的字體就會變成綠色:

關(guān)于createElement使用實例
Vue 提供了createElement 來創(chuàng)建虛擬dom,方便我們來函數(shù)化的方式來定義復(fù)雜的組件結(jié)構(gòu)。在組件定義的時候,通常render函數(shù)的參數(shù)里都會帶上該函數(shù)的引用。方便用戶調(diào)用。
參數(shù)說明
createElement 默認暴露給用戶傳遞3個參數(shù),參考文檔代碼介紹:
// @returns {VNode}
createElement(
// {String | Object | Function}
// 一個 HTML 標簽名、組件選項對象,或者
// resolve 了上述任何一種的一個 async 函數(shù)。必填項。
'div',
// {Object}
// 一個與模板中屬性對應(yīng)的數(shù)據(jù)對象??蛇x。
{
// (詳情見下一節(jié))
},
// {String | Array}
// 子級虛擬節(jié)點 (VNodes),由 `createElement()` 構(gòu)建而成,
// 也可以使用字符串來生成“文本虛擬節(jié)點”??蛇x。
[
'先寫一些文字',
createElement('h1', '一則頭條'),
createElement(MyComponent, {
props: {
someProp: 'foobar'
}
})
]
)使用示例
//創(chuàng)建一個div節(jié)點
createElement("div", {
return createElement("div", {
domProps: {
innerHTML: "hello !"
}
})
})
// 按照組件名來返回一個虛擬dom
createElement("component-name", {
props: {
name: "hello"
}
})
// 設(shè)置子對象
return createElement("div", {
class: "box"
}, [
createElement("span", {
domProps: {
innerHTML: 'bob !'
}
})
])源碼解讀
createElement 最終是通過調(diào)用new VNode 來創(chuàng)建虛擬dom,函數(shù)在調(diào)用new VNode之前處理了很多限制的情況,比如:data不能是響應(yīng)式數(shù)據(jù),tag是否為空等等,詳見下面代碼中的中文注釋
function _createElement (
context,
tag,
data,
children,
normalizationType
) {
if (isDef(data) && isDef((data).__ob__)) { //檢測是否是響應(yīng)式數(shù)據(jù)
warn(
"Avoid using observed data object as vnode data: " + (JSON.stringify(data)) + "\n" +
'Always create fresh vnode data objects in each render!',
context
);
return createEmptyVNode()
}
// object syntax in v-bind
if (isDef(data) && isDef(data.is)) { //檢測data中是否有is屬性,是的話tag替換為is指向的內(nèi)容,處理動態(tài)組件
tag = data.is;
}
if (!tag) { // tag如果為空,創(chuàng)建空虛擬節(jié)點
// in case of component :is set to falsy value
return createEmptyVNode()
}
// warn against non-primitive key
if (isDef(data) && isDef(data.key) && !isPrimitive(data.key) // data 中的key如果定義了必須是數(shù)字或者字符串
) {
{
warn(
'Avoid using non-primitive value as key, ' +
'use string/number value instead.',
context
);
}
}
// support single function children as default scoped slot
if (Array.isArray(children) &&
typeof children[0] === 'function'
) {
data = data || {};
data.scopedSlots = { default: children[0] };
children.length = 0;
}
// 標準化處理children的兩種模式
if (normalizationType === ALWAYS_NORMALIZE) {
children = normalizeChildren(children);
} else if (normalizationType === SIMPLE_NORMALIZE) {
children = simpleNormalizeChildren(children);
}
var vnode, ns;
if (typeof tag === 'string') {
var Ctor;
ns = (context.$vnode && context.$vnode.ns) || config.getTagNamespace(tag);
if (config.isReservedTag(tag)) { // 判讀是否是標準的html 標簽
// platform built-in elements
vnode = new VNode(
config.parsePlatformTagName(tag), data, children,
undefined, undefined, context
);
} else if ((!data || !data.pre) && isDef(Ctor = resolveAsset(context.$options, 'components', tag))) {// 如果tag對應(yīng)的是組件名,創(chuàng)建組件
// component
vnode = createComponent(Ctor, data, context, children, tag);
} else {
// unknown or unlisted namespaced elements
// check at runtime because it may get assigned a namespace when its
// parent normalizes children
vnode = new VNode(
tag, data, children,
undefined, undefined, context
);
}
} else {
// direct component options / constructor
vnode = createComponent(tag, data, context, children);
}
if (Array.isArray(vnode)) {
return vnode
} else if (isDef(vnode)) {
if (isDef(ns)) { applyNS(vnode, ns); }
if (isDef(data)) { registerDeepBindings(data); }
return vnode
} else {
return createEmptyVNode()
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
深入淺析Vue.js中 computed和methods不同機制
這篇文章給大家介紹了Vue.js中 computed和methods不同機制,在vue.js中,methods和computed兩種方式來動態(tài)當作方法使用,文中還給大家提到了computed和methods的區(qū)別,感興趣的朋友一起看看吧2018-03-03
從0搭建Vue3組件庫如何使用?glup?打包組件庫并實現(xiàn)按需加載
這篇文章主要介紹了從0搭建Vue3組件庫如何使用?glup?打包組件庫并實現(xiàn)按需加載,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
vuex頁面刷新導(dǎo)致數(shù)據(jù)丟失的解決方案
這篇文章主要介紹了vuex頁面刷新導(dǎo)致數(shù)據(jù)丟失的解決方案,幫助大家更好的使用vue框架,感興趣的朋友可以了解下2020-12-12
Vue項目中實現(xiàn)描點跳轉(zhuǎn)scrollIntoView的案例
這篇文章主要介紹了Vue項目中實現(xiàn)描點跳轉(zhuǎn)scrollIntoView的案例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
vue2導(dǎo)航根據(jù)路由傳值,而改變導(dǎo)航內(nèi)容的實例
下面小編就為大家?guī)硪黄獀ue2導(dǎo)航根據(jù)路由傳值,而改變導(dǎo)航內(nèi)容的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
vue elementUI table 自定義表頭和行合并的實例代碼
這篇文章主要介紹了vue elementUI table 自定義表頭和行合并的實例代碼,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05

