Vue uses Ref to get component instances across levels Example Introduction During the development process, we will inevitably use cross-level ref instance acquisition. In most cases, we can find the required instance through the component's own As shown in the figure below, we use Document directory structureThere are six components, namely A, B, C, D, E and index, and they are inserted into their respective pages in the order of the components in the above figure. The page style is as follows: Install vue-refDownload vue-ref npm install vue-ref --save Global Registration import ref from 'vue-ref' Vue.use(ref) How to use <!-- vm.dom will be the DOM node --> <p v-ref="c => this.dom = c">hello</p> <!-- vm.child will be the child component instance --> <child-component v-ref="c => this.child = c"></child-component> <span v-for="n in 10" :key="n" v-ref="(c, key) => {...}">{{ n }} </span> Root component custom method [using provide and inject]In our index page, we provide three methods:
provide() { return { setChildrenRef: (name, ref) => { this[name] = ref }, getChildrenRef: name => { return this[name] }, getRef: () => { return this } } }, Describe each page separatelyComponent A page: Get the setChildrenRef method through the injection method, and cache component D through the above instructions Component B page: Component C page: Component D page: Component E page: In this page, we not only inject two methods, but also set a method to switch the color of component D to test whether we have really obtained the instance of component D across levels. resultAs you can see, the three parent instances are the same, and the text style of component D is successfully modified in component E. good! The above is the details of the steps for Vue to use Ref to obtain components across levels. For more information about Vue using Ref to obtain components, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Installation tutorial of the latest stable version of MySQL 5.7.17 under Linux
>>: mysql5.7 create user authorization delete user revoke authorization
As shown below: update table1 as z left join tabl...
Preface Today, I was reviewing the creational pat...
In the previous article, I introduced the detaile...
Table of contents MyISAM and InnoDB Reasons for p...
Common application scenarios The interfaces of cu...
Preface When creating a primary and foreign key f...
vue+element UI encapsulates a public function to ...
1. Overview of file permissions and ownership 1. ...
1.html <div class="loginbody"> &l...
MySQL Lock Overview Compared with other databases...
Table of contents Preface HTTP HTTP Server File S...
What is the input type="file"? I don'...
This article example shares the specific code of ...
Analyze the execution process. Move the mouse int...
CSS is the realm of style, layout, and presentati...