Vue組件跨層級獲取組件操作
this.$parent 訪問父實例
this.$children 當前實例的直接子組件。(不保證順序,不是響應(yīng)式)
this.$parent.$parent.$refs.xxx 跨級訪問父組件
this.$children.$children.$refs.xxx 跨級訪問子組件
這種遞歸的方式 代碼繁瑣 性能低效
ref
只能獲取當前組件上下文組件 無法跨層級
ref 是字符串 被用來給元素或子組件注冊引用信息。
引用信息將會注冊在父組件的 $refs 對象上。
如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;
如果用在子組件上,引用就指向組件實例
<!-- vm.$refs.p/this.$refs.p 獲取DOM node --> <p ref="p">hello</p> <!-- vm.$refs.child/this.$refs.child 獲取組件實例 --> <child-component ref="child"></child-component>
注:
因為 ref 本身是作為渲染結(jié)果被創(chuàng)建的,在初始渲染的時候你不能訪問它們,它們還不存在
$refs 不是響應(yīng)式的,因此你不應(yīng)該試圖用它在模板中做數(shù)據(jù)綁定。
這僅作為一個用于直接操作子組件的“逃生艙”——你應(yīng)該避免在模板或計算屬性中訪問 $refs。
當 ref 和 v-for 一起使用的時候,你得到的引用將會是一個包含了對應(yīng)數(shù)據(jù)源的這些子組件的數(shù)組。
如何優(yōu)雅的獲取跨層級實例 ?
1、npm install vue-ref || yarn add vue-ref 安裝vue-ref插件
2、導(dǎo)入import ref from "vue-ref"
3、使用插件Vue.use(ref, { name: "ant-ref" });name是給插件起名
插件使用方法
//使用`provide` 在根組件提供數(shù)據(jù) provide() { return { //主動通知 將組件實例綁定在根組件上 setChildrenRef: (name, ref) => { this[name] = ref; }, //主動獲取 獲取綁定的組件 getChildrenRef: name => { return this[name]; }, // 獲取根組件 getRef: () => { return this; } } } // 使用`inject` 在子組件中注入數(shù)據(jù) inject: { setChildrenRef: { default: () => {} }, getParentRef: { from: "getRef", default: () => {} }, getParentChildrenRef: { from: "getChildrenRef", default: () => {} } } //使用指令注冊子組件 <ChildrenH v-ant-ref="c => setChildrenRef('childrenH', c)" /> //使用指令注冊DOM元素 <h3 v-ant-ref="c => setChildrenRef('childrenE', c)">E 結(jié)點</h3>
//獲取根組件實例 this.getParentRef() //獲取指定名稱組件實例 this.getParentChildrenRef("childrenH") //這里輸出的事DOM this.getParentChildrenRef("childrenE")
vue-ref插件源碼
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = { install: function install(Vue) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var directiveName = options.name || 'ref'; console.log(arguments) Vue.directive(directiveName, { bind: function bind(el, binding, vnode) { //自定義指令傳入值 是函數(shù), 在這里執(zhí)行 傳入組件實例 binding.value(vnode.componentInstance || el, vnode.key); //vnode.key 是使用插件時起的名稱 }, update: function update(el, binding, vnode, oldVnode) { if (oldVnode.data && oldVnode.data.directives) { var oldBinding = oldVnode.data.directives.find(function (directive) { var name = directive.name; return name === directiveName; }); if (oldBinding && oldBinding.value !== binding.value) { oldBinding && oldBinding.value(null, oldVnode.key); // 如果指令綁定的值有變化,則更新 組件實例 binding.value(vnode.componentInstance || el, vnode.key); return; } } // Should not have this situation if (vnode.componentInstance !== oldVnode.componentInstance || vnode.elm !== oldVnode.elm) { binding.value(vnode.componentInstance || el, vnode.key); } }, unbind: function unbind(el, binding, vnode) { binding.value(null, vnode.key); } }); } };
補充知識:vue項目中z-index不起作用(將vue實例掛在到window上面)
問題描述:由于原有項目(傳統(tǒng)項目)中嵌入新的vue組件,dialog彈出框的z-index:999999;任然不起作用;
解決辦法:將vue實例掛載到window
解決代碼如下:
入口文件index.js中
import Index from './components/index.vue' import './index.pcss' function install (Vue) { Vue.component('gys-index-list', Index) } if (typeof window !== 'undefined' && window.Vue) { install(window.Vue) }
在父組件中正確引入壓縮后的css文件+js文件(這里截圖的父組件是html文件)
將元素添加到body上面(解決z-index不起作用,前面內(nèi)容只是鋪墊)
總結(jié)描述:由于項目版本的迭代,需要將新的項目(使用的vue框架)嵌入到原有的傳統(tǒng)的html文件項目中,控制臺提示找不到vue組件。除了正確引入vue實例外,需要查看NGINX配置是否正確
以上這篇Vue組件跨層級獲取組件操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3中項目優(yōu)化方法詳解(Web?Worker的使用)
最近在做vue3的項目中,遇到了計算量龐大導(dǎo)致頁面響應(yīng)緩慢的問題,所以下面這篇文章主要給大家介紹了關(guān)于vue3中項目優(yōu)化方法的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-07-07vue中el-table兩個表尾合計行聯(lián)動同步滾動條實例代碼
項目開發(fā)中遇到一個比較兩個form差異的需求,但當item過多就需要滾動條,下面這篇文章主要給大家介紹了關(guān)于vue中el-table兩個表尾合計行聯(lián)動同步滾動條的相關(guān)資料,需要的朋友可以參考下2022-05-05vue使用路由的query配置項時清除地址欄的參數(shù)案例詳解
這篇文章主要介紹了vue使用路由的query配置項時如何清除地址欄的參數(shù),本文通過案例給大家分享完美解決方案,需要的朋友可以參考下2023-09-09element ui循環(huán)調(diào)用this.$alert 消息提示只顯示最后一個
這篇文章主要介紹了element ui循環(huán)調(diào)用this.$alert 消息提示只顯示最后一個,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10vue3中使用ref和emit來減少props的使用示例詳解
現(xiàn)在在開發(fā)vue3項目的過程中,我們開發(fā)小組漸漸的減少props的使用,轉(zhuǎn)而用ref 和 emit 來代替,這篇文章主要介紹了vue3中使用ref和emit來減少props的使用,需要的朋友可以參考下2022-06-06