Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

javascript - 在Vuejs中按名称获取特定的父组件(Getting Specific Parent Component By Name in Vuejs)

I need a reliable way to call the a parent component of n parents in vuejs.(我需要一种可靠的方法来在vuejs中调用n个父级的父级组件。)

Looking for something more like closest('element') in jQuery where you don't have to specify how many parents or children there are, you only give it the name of the component you need and it will look it up for you recursivly untill it finds the right one.(在jQuery中寻找更像closet('element')的东西时,您不必指定有多少个父母或孩子,只需给它指定所需组件的名称,它将为您递归查找直到它找到合适的。) I just wonder if there is something like: this.$find(component-name);(我只是想知道是否有这样的东西:this。$ find(component-name);) or this.$closest(component-name);(或this。$ closest(component-name);) I have read somewhere people are using $ref, but I don't think that's the right one for this use-case or is it ?(我在某人正在使用$ ref的地方阅读过,但我认为这不是该用例的正确选择,还是这样?) Instead of this.$parent.$parent which is obviously not very reliable, when you don't know how many parents there are.(当您不知道有多少父母时,显然不是那么可靠的this。$ parent。$ parent。) Thanks,(谢谢,)   ask by Eden Reich translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use a while loop, iterating over all the parent components.(您可以使用while循环,遍历所有父组件。)

getComponent(componentName) { let component = null let parent = this.$parent while (parent && !component) { if (parent.$options.name === componentName) { component = parent } parent = parent.$parent } return component }, Example:(例:) mounted() { console.log(this.getComponent('App')) }, Utility function (TS):(实用功能(TS):) import Vue from 'vue' /** * @name findParentComponentByName * @summary Find the Vue instance of the first parent component that matches the provided component name. * * @description The `findParentComponentByName()` method returns the Vue instance of the first parent component * that has a `name` component option that matches the provided component name. * * @param {Vue} vm - The children component Vue instance that is looking for the parent component Vue instance * @param {string} componentName - The parent component name * @returns {Vue|undefined} The parent component instance that matches the provided component name, * otherwise, undefined is returned * * @example * // Find `<App/>` component from `<Child/>`: * <App> * <GrandParent> * <Parent> * <Child/> * </Parent> * </GrandParent> * </App> * * // Descendant component Vue instance * new Vue({ * name: 'Child', * * created() { * const app = findParentComponentByName(this, 'App') * // => VueComponent {_uid: 1, _isVue: true, $options: {…}, _renderProxy: Proxy, _self: VueComponent, …} * }, * }) */ export function findParentComponentByName( vm: Vue, componentName: string ): Vue | undefined { // // Components tree: // +---------------------+ return undefined, , <Child/> is not a descendant of <App/> // | <App> |---> Return if component name option matches, otherwise // |---------------------| continue traversing the tree upwards // | <GrandParent> |-----> Return if component name option matches, otherwise // |---------------------| continue traversing the tree upwards // | <Parent> |-------> Return if component name option matches, otherwise // |---------------------| traverse the tree upwards // | <Child/> |---------> STARTING POINT, start looking for App component from here // |---------------------| // | </Parent> | // |---------------------| // | </GrandParent> | // |---------------------| // | </App> | // +---------------------+ // let component: Vue | undefined let parent = vm.$parent while (parent && !component) { if (parent.$options.name === componentName) { component = parent } parent = parent.$parent } return component } Example:(例:) // In consuming component import { findParentComponentByName } from '@/utils' // ... mounted() { console.log(findParentComponentByName(this, 'App')) },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...