[TOC]
>[success] # 編寫自己的工具類進行父子傳遞
<a href='https://juejin.im/book/5bc844166fb9a05cd676ebca/section/5bc844166fb9a05cf52af65f'>文章來自AresnTalkingData 前端架構師,iView 作者 發布在掘金網小冊中內容啟發整理</a>
如果你有能力有錢請你購買原作者文找那個,尊敬每一個原作者是我們應該做的,不要做代碼行業的伸手黨
感謝這些大佬的文章,本內容是根據大佬的文章二次整理,用更通俗的理解讓初學者也能看懂
~~~
1.準備工作,'iview' 作者喜歡在vue結構目錄中創建一個lib文件夾,并且在文件夾中創建一個'utils'
文件專門用來寫自己的工具方法,結構目錄如下:
│ ├── 'lib' //工具包
│ ├── 'tools.js' // 存放和業務無關工具性質的js代碼
│ └── 'util.js' //存放和業務相關工具性質的js代碼
2.下面將會做這個五個場景父子傳遞的工具類:
2.1.由一個組件,向上找到最近的指定組件;
2.2.由一個組件,向上找到所有的指定組件;
2.3.由一個組件,向下找到最近的指定組件;
2.4.由一個組件,向下找到所有指定的組件;
2.5.由一個組件,找到指定組件的兄弟組件。
3.注意這次的工具組件傳遞的查找方法和'dispatch'不同的,這次是找整個組件,而'dispatch' 是吧某個
方法傳遞
~~~
>[info] ## 由一個組件,向上找到最近的指定組件 -- findComponentUpward
~~~
1.思路:編寫這個函數時候,需要的形參分析,根據需要我們是要找到某個組件的最近的指定的父組件,
因此某個組件肯定是參數之一,指定的父組件就是參數之二
2.利用'dispatch' 思想我們需要去遞歸,一直找到當前組件的父組件,知道找到和我們需要匹配的組件
,因此需要'$parent',和'$options.name'
~~~
>[danger] ##### 在utils 中正式編寫
~~~
1.context 參數代表當前起點,也就當前組件的'this',componentName 就是目標組件的,可以理解成當前'this'
向上的某個父組件或者有可能是他的爺爺組件
2.邏輯 先獲取當前組件的'$parent' 和 'componentName' 父組件的名稱,然后去循環如果他有父組件,并且
組件沒有名字或者名字不等于目標的名字,我們就繼續遞歸循環查找,直到找到返回整個對象
3.'iview' 作者的解釋:
3.1.第一個參數一般都是傳入 this,即當前組件的上下文(實例)。
4.提醒自己一點:在寫代碼的時候一定要對某些條件做判斷,例如下面的代碼中的 'if(parent)' 就可以減收不必要
的操作
~~~
~~~
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;
}
export { findComponentUpward };
~~~
>[danger] ##### 使用篇章
* 創建一個test-a 父組件
~~~
<!--test-a 組件作為父組件-->
<template>
<test-b></test-b>
</template>
<script>
import testB from './test-b'
export default {
name: "test-a",
components: {
testB
},
methods:{
sayHiB(){
console.log('我是A組件的方法,但是現在被B調用了');
}
}
}
</script>
<style scoped>
</style>
~~~
* 組件B 子組件去使用組件a的方法
~~~
<template>
<div>
組件 B
</div>
</template>
<script>
import {findComponentUpward} from '../../lib/utils'
export default {
name: "test-b",
// 發現一個規律類似這種組件調用 最好是在生命周期時候就注冊好
// 不要在點擊的時候在觸發
// 也可以吧這個放回的對象放進B組件的 data中方便調用
mounted () {
const comA = findComponentUpward(this, 'test-a');
if (comA) {
comA.sayHiB(); // 我是A組件的方法,但是現在被B調用了
}
}
}
</script>
<style scoped>
</style>
~~~
>[info] ## 由一個組件,向上找到所有的指定組件 -- findComponentsUpward
~~~
1.findComponentsUpward 場景遞歸后續研究 做標記
~~~
>[danger] ##### findComponentsUpward
~~~
// 由一個組件,向上找到所有的指定組件
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 [];
}
}
export { findComponentsUpward };
~~~
>[info] ## 由一個組件,向下找到最近的指定組件 -- findComponentDownward
~~~
1.原理就是找到當前組件的所有子組件,然后遞歸查找看那個子組件符合我們傳入的名字
如果相等就是我們需要的組件
2.這里要說明一個數組的循環,for ...in 和 for ...of,in簡單粗暴理解循環對象用的k值
,因此循環數組的時候是腳標,of 是用來循環數組中的內容
~~~
>[danger] ##### findComponentDownward
~~~
1.找到當前組件的所有子組件利用'$children',如果子組件中也沒有就去子組件的子組件找
,也就是遞歸查找,知道找到了 返回對應的子組件
2.這里注意循環數組的循環使用 for ...of
~~~
~~~
function findComponentDownward (context,componentName){
let childrens = context.$children
// 定義一個接受 變量
let children = null;
if(childrens.length>0){
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
}
export { findComponentDownward };
~~~
>[danger] ##### 案例
* 父組件A
~~~
<!--test-a 組件作為父組件-->
<template>
<test-b></test-b>
</template>
<script>
import testB from './test-b'
import {findComponentDownward } from '../../lib/utils'
export default {
name: "test-a",
components: {
testB
},
mounted(){
// 調用子組件方法
const comB = findComponentDownward(this, 'test-b');
if (comB) {
comB.sayHiB(); // 我是B組件的方法,但是現在被A調用了
}
}
}
</script>
<style scoped>
</style>
~~~
* 子組件B
~~~
<template>
<div>
組件 B
</div>
</template>
<script>
export default {
name: "test-b",
methods:{
sayHiB(){
console.log('我是B組件的方法,但是現在被A調用了');
}
}
}
</script>
<style scoped>
</style>
~~~
>[info] ## 由一個組件,向下找到所有的指定組件 -- findComponentsDownward
~~~
1.findComponentsDownward 場景遞歸后續研究 做標記
~~~
>[danger] ##### findComponentsUpward
~~~
1.后續理解'reduce' 方法
~~~
~~~
// 由一個組件,向下找到所有指定的組件
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);
}, []);
}
export { findComponentsDownward };
~~~
>[info] ## 找到指定組件的兄弟組件——findBrothersComponents
~~~
~~~
>[danger] ##### findBrothersComponents
~~~
1.這里使用了三個參數,和之前一樣錢兩個分別是起始組件對象,要找的組件名字,
這里還用了數組方法'findIndex' 用來找到腳標
2.對第三個參數做詳細講解,第三個參數是,是否包含自己,咋一看覺得無法理解,
舉個例子,想彈窗這類組件 在一個頁面可能會使用多次,但是她們的名字相同,但是
我在對應的兄弟組件肯定是不想包含本身,因此利用了'_uid' 唯一標識做了標記去重
~~~
~~~
function findBrothersComponents (context,componentName,exceptMe = true) {
// 找到符合的子組件名稱數組
let res = context.$parent.$children.filter(item =>{
return item.$options.name === componentName;
})
// 找到當前本身組件在數組中的位置
let index = res.findIndex(item =>{
return item._uid === context._uid
})
if (exceptMe) res.splice(index, 1);
return res;
}
export { findBrothersComponents };
~~~
>[danger] ##### 案例說明
* 父組件中同一個組件調用兩次
~~~
<!--test-a 組件作為父組件-->
<template>
<div>
<test-b></test-b>
<test-b></test-b>
</div>
</template>
<script>
import testB from './test-b'
export default {
name: "test-a",
components: {
testB
},
}
</script>
<style scoped>
</style>
~~~
* 子組件中兄弟組件默認不包括自己
~~~
<template>
<div>
組件 B
</div>
</template>
<script>
import {findBrothersComponents } from '../../lib/utils'
export default {
name: "test-b",
methods:{
sayHiB(){
console.log('我是B組件的方法,但是現在被A調用了');
}
},
mounted(){
const comB = findBrothersComponents(this, 'test-b');
if (comB) {
console.log(comB);
}
}
}
</script>
<style scoped>
</style>
~~~
- Vue--基礎篇章
- Vue -- 介紹
- Vue -- MVVM
- Vue -- 創建Vue實例
- Vue -- 模板語法
- Vue -- 指令用法
- v-cloak -- 遮蓋
- v-bind -- 標簽屬性動態綁定
- v-on -- 綁定事件
- v-model -- 雙向數據綁定
- v-for -- 只是循環沒那么簡單
- 小知識點 -- 計劃內屬性
- key -- 屬性為什么要加
- 案例說明
- v-if/v-show -- 顯示隱藏
- v-for 和 v-if 同時使用
- v-pre -- 不渲染大大胡語法
- v-once -- 只渲染一次
- Vue -- class和style綁定
- Vue -- filter 過濾器
- Vue--watch/computed/fun
- watch -- 巧妙利用watch思想
- Vue -- 自定義指令
- Vue -- $方法
- Vue--生命周期
- Vue -- 專屬ajax
- Vue -- transition過渡動畫
- 前面章節的案例
- 案例 -- 跑馬燈效果
- 案例 -- 選項卡內容切換
- 案例-- 篩選商品
- 案例 -- 搜索/刪除/更改
- 案例 -- 用computed做多選
- 案例 -- checked 多選
- Vue--組件篇章
- component -- 介紹
- component -- 使用全局組件
- component -- 使用局部組件
- component -- 組件深入
- component -- 組件傳值父傳子
- component -- 組件傳值子傳父
- component -- 子傳父語法糖拆解
- component -- 父組件操作子組件
- component -- is 動態切換組件
- component -- 用v-if/v-show控制子組件
- component -- 組件切換的動畫效果
- component -- slot 插槽
- component -- 插槽2.6
- component -- 組件的生命周期
- component -- 基礎組件全局注冊
- VueRouter--獲取路由參數
- VueRouter -- 介紹路由
- VueRouter -- 安裝
- VueRouter -- 使用
- VueRouter--router-link簡單參數
- VueRouter--router-link樣式問題
- VueRouter--router-view動畫效果
- VueRouter -- 匹配優先級
- vueRouter -- 動態路由
- VueRouter -- 命名路由
- VueRouter -- 命名視圖
- VueRouter--$router 獲取函數
- VueRouter--$route獲取參數
- VueRouter--路由嵌套
- VueRouter -- 導航守衛
- VueRouter -- 寫在最后
- Vue--模塊化方式結構
- webpack--自定義配置
- webpack -- 自定義Vue操作
- VueCli -- 3.0可視化配置
- VueCli -- 3.0 項目目錄
- Vue -- 組件升級篇
- Vue -- 組件種類與組件組成
- Vue -- 組件prop、event、slot 技巧
- Vue -- 組件通信(一)
- Vue -- 組件通信(二)
- Vue -- 組件通信(三)
- Vue -- 組件通信(四)
- Vue -- 組件通信(五)
- Vue -- 組件通信(六)
- Vue -- bus非父子組件通信
- Vue -- 封裝js插件成vue組件
- vue組件分裝 -- 進階篇
- Vue -- 組件封裝splitpane(分割面板)
- UI -- 正式封裝
- Vue -- iview 可編輯表格案例
- Ui -- iview 可以同時編輯多行
- Vue -- 了解遞歸組件
- UI -- 正式使用遞歸菜單
- Vue -- iview Tree組件
- Vue -- 利用通信仿寫一個form驗證
- Vue -- 使用自己的Form
- Vue -- Checkbox 組件
- Vue -- CheckboxGroup.vue
- Vue -- Alert 組件
- Vue -- 手動掛載組件
- Vue -- Alert開始封裝
- Vue -- 動態表單組件
- Vue -- Vuex組件的狀態管理
- Vuex -- 參數使用理解
- Vuex -- state擴展
- Vuex -- getters擴展
- Vuex--mutations擴展
- Vuex -- Action 異步
- Vuex -- plugins插件
- Vuex -- v-model寫法
- Vuex -- 更多
- VueCli -- 技巧總結篇
- CLI -- 路由基礎
- CLI -- 路由升級篇
- CLI --異步axios
- axios -- 封裝axios
- CLI -- 登錄寫法
- CLI -- 權限
- CLI -- 簡單權限
- CLI -- 動態路由加載
- CLI -- 數據性能優化
- ES6 -- 類的概念
- ES6類 -- 基礎
- ES6 -- 繼承
- ES6 -- 工作實戰用類數據管理
- JS -- 適配器模式
- ES7 -- 裝飾器(Decorator)
- 裝飾器 -- 裝飾器修飾類
- 裝飾器--修飾類方法(知識擴展)
- 裝飾器 -- 裝飾器修飾類中的方法
- 裝飾器 -- 執行順序
- Reflect -- es6 自帶版本
- Reflect -- reflect-metadata 版本
- 實戰 -- 驗證篇章(基礎)
- 驗證篇章 -- 搭建和目錄
- 驗證篇章 -- 創建基本模板
- 驗證篇章 -- 使用
- 實戰 -- 更新模型(為了迎合ui升級)
- 實戰 -- 模型與接口對接
- TypeSprict -- 基礎篇章
- TS-- 搭建(一)webpack版本
- TS -- 搭建(二)直接使用
- TS -- 基礎類型
- TS -- 枚舉類型
- TS -- Symbol
- TS -- interface 接口
- TS -- 函數
- TS -- 泛型
- TS -- 類
- TS -- 類型推論和兼容
- TS -- 高級類型(一)
- TS -- 高級類型(二)
- TS -- 關于模塊解析
- TS -- 聲明合并
- TS -- 混入
- Vue -- TS項目模擬
- TS -- vue和以前代碼對比
- TS -- vue簡單案例上手
- Vue -- 簡單弄懂VueRouter過程
- VueRouter -- 實現簡單Router
- Vue-- 原理2.x源碼簡單理解
- 了解 -- 簡單的響應式工作原理
- 準備工作 -- 了解發布訂閱和觀察者模式
- 了解 -- 響應式工作原理(一)
- 了解 -- 響應式工作原理(二)
- 手寫 -- 簡單的vue數據響應(一)
- 手寫 -- 簡單的vue數據響應(二)
- 模板引擎可以做的
- 了解 -- 虛擬DOM
- 虛擬dom -- 使用Snabbdom
- 閱讀 -- Snabbdom
- 分析snabbdom源碼 -- h函數
- 分析snabbdom -- init 方法
- init 方法 -- patch方法分析(一)
- init 方法 -- patch方法分析(二)
- init方法 -- patch方法分析(三)
- 手寫 -- 簡單的虛擬dom渲染
- 函數表達解析 - h 和 create-element
- dom操作 -- patch.js
- Vue -- 完成一個minVue
- minVue -- 打包入口
- Vue -- new實例做了什么
- Vue -- $mount 模板編譯階段
- 模板編譯 -- 分析入口
- 模板編譯 -- 分析模板轉譯
- Vue -- mountComponent 掛載階段
- 掛載階段 -- vm._render()
- 掛載階段 -- vnode
- 備份章節
- Vue -- Nuxt.js
- Vue3 -- 學習
- Vue3.x --基本功能快速預覽
- Vue3.x -- createApp
- Vue3.x -- 生命周期
- Vue3.x -- 組件
- vue3.x -- 異步組件???
- vue3.x -- Teleport???
- vue3.x -- 動畫章節 ??
- vue3.x -- 自定義指令 ???
- 深入響應性原理 ???
- vue3.x -- Option API VS Composition API
- Vue3.x -- 使用set up
- Vue3.x -- 響應性API
- 其他 Api 使用
- 計算屬性和監聽屬性
- 生命周期
- 小的案例(一)
- 小的案例(二)-- 泛型
- Vue2.x => Vue3.x 導讀
- v-for 中的 Ref 數組 -- 非兼容
- 異步組件
- attribute 強制行為 -- 非兼容
- $attrs 包括 class & style -- 非兼容
- $children -- 移除
- 自定義指令 -- 非兼容
- 自定義元素交互 -- 非兼容
- Data選項 -- 非兼容
- emits Option -- 新增
- 事件 API -- 非兼容
- 過濾器 -- 移除
- 片段 -- 新增
- 函數式組件 -- 非兼容
- 全局 API -- 非兼容
- 全局 API Treeshaking -- 非兼容
- 內聯模板 Attribute -- 非兼容
- key attribute -- 非兼容
- 按鍵修飾符 -- 非兼容
- 移除 $listeners 和 v-on.native -- 非兼容
- 在 prop 的默認函數中訪問 this -- ??
- 組件使用 v-model -- 非兼容
- 渲染函數 API -- ??
- Slot 統一 ??
- 過渡的 class 名更改 ???
- Transition Group 根元素 -- ??
- v-if 與 v-for 的優先級對比 -- 非兼容
- v-bind 合并行為 非兼容
- 監聽數組 -- 非兼容