關(guān)于找到任意組件實例的方法
找到任意組件實例的方法
由一個組件,向上找到最近的指定組件
/**
?* 由一個組件,向上找到最近的指定組件
?* @param {*} context 當(dāng)前上下文,比如你要基于哪個組件來向上尋找,一般都是基于當(dāng)前的組件,也就是傳入 this
?* @param {*} componentName 要找的組件的 name
?*/
function findComponentUpward(context, componentName) {
? let parent = context.$parent
? let name = parent.$options.name
? while (parent && (!name || [componentName].indexOf(name) < 0)) {
? ? parent = parent.$parent
? ? if (parent) name = parent.$options.name
? }
? return parent
}由一個組件,向上找到所有的指定組件
/**
?* 由一個組件,向上找到所有的指定組件
?* @param {*} context 當(dāng)前上下文,比如你要基于哪個組件來向上尋找,一般都是基于當(dāng)前的組件,也就是傳入 this
?* @param {*} componentName 要找的組件的 name
?*/
function findComponentsUpward(context, componentName) {
? let parents = []
? const parent = context.$parent
? if (parent) {
? ? if (parent.$options.name === componentName) parents.push(parent)
? ? return parents.concat(findComponentsUpward(parent, componentName))
? } else {
? ? return []
? }
}由一個組件,向下找到最近的指定組件
/**
?* 由一個組件,向下找到最近的指定組件
?* @param {*} context 當(dāng)前上下文,比如你要基于哪個組件來向上尋找,一般都是基于當(dāng)前的組件,也就是傳入 this
?* @param {*} componentName 要找的組件的 name
?*/
function findComponentDownward(context, componentName) {
? const childrens = context.$children
? let children = null
? if (childrens.length) {
? ? for (const child of childrens) {
? ? ? const name = child.$options.name
? ? ? if (name === componentName) {
? ? ? ? children = child
? ? ? ? break
? ? ? } else {
? ? ? ? children = findComponentDownward(child, componentName)
? ? ? ? if (children) break
? ? ? }
? ? }
? }
? return children
}由一個組件,向下找到所有指定的組件
/**
?* 由一個組件,向下找到所有指定的組件
?* @param {*} context 當(dāng)前上下文,比如你要基于哪個組件來向上尋找,一般都是基于當(dāng)前的組件,也就是傳入 this
?* @param {*} componentName 要找的組件的 name
?*/
function findComponentsDownward(context, componentName) {
? return context.$children.reduce((components, child) => {
? ? if (child.$options.name === componentName) components.push(child)
? ? const foundChilds = findComponentsDownward(child, componentName)
? ? return components.concat(foundChilds)
? }, [])
}由一個組件,找到指定組件的兄弟組件
/**
?* 由一個組件,找到指定組件的兄弟組件
?* @param {*} context 當(dāng)前上下文,比如你要基于哪個組件來向上尋找,一般都是基于當(dāng)前的組件,也就是傳入 this
?* @param {*} componentName 要找的組件的 name
?* @param {*} exceptMe 是否把本身除外
?*/
function findBrothersComponents(context, componentName, exceptMe = true) {
? let res = context.$parent.$children.filter((item) => {
? ? return item.$options.name === componentName
? })
? let index = res.findIndex((item) => item._uid === context._uid)
? if (exceptMe) res.splice(index, 1)
? return res
}vue常用組件庫
移動端常用組件庫
1.Vant https://youzan.github.io/vant
2.CubeUI https://didi.github.io/cube-ui
3.MintUI https://mint-ui.github.io
3.NutUI https://nutui.jd.com/ // 京東自己的
pc端常用組件庫
1.ElementUI https://element.eleme.cn
2.IViewUI https://www.iviewui.com
2.AntDesignUI https://ant.design/
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue在chrome44偶現(xiàn)點擊子元素事件無法冒泡的解決方法
這篇文章主要給大家介紹了關(guān)于Vue在chrome44偶現(xiàn)點擊子元素事件無法冒泡的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
解決vue前端文件上傳報錯:上傳失敗,原因:413 Request Entity Too&
這篇文章主要介紹了解決vue前端文件上傳報錯:上傳失敗,原因:413 Request Entity Too Large,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
windows下vue.js開發(fā)環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了windows下vue.js開發(fā)環(huán)境搭建教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Vue中"This dependency was not found"問題的解決方法
這篇文章主要介紹了Vue中"This dependency was not found"的問題的解決方法,需要的朋友可以參考下2018-06-06
詳解Vue.js使用Swiper.js在iOS<11時出現(xiàn)錯誤
這篇文章主要介紹了詳解Vue.js使用Swiper.js在iOS<11時出現(xiàn)錯誤,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
vue3.0 CLI - 2.4 - 新組件 Forms.vue 中學(xué)習(xí)表單
這篇文章主要介紹了vue3.0 CLI - 2.4 - 新組件 Forms.vue 中學(xué)習(xí)表單的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2018-09-09
淺談vue中document.getElementById()拿到的是原值的問題
這篇文章主要介紹了淺談vue中document.getElementById()拿到的是原值的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue如何使用swiper插件修改左右箭頭的默認(rèn)樣式
這篇文章主要介紹了vue如何使用swiper插件修改左右箭頭的默認(rèn)樣式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01

