How to get any element in a subcomponent by Vue.js

as shown in the figure, dynamically adding a node to a line of text and the corresponding input,input is originally hidden, and the input is displayed after clicking on the text. After that, I want input to get the focus (focus), but I don"t know how to get the input properly (without using the DOM operation).

the code is as follows:: (html file)

<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<div id="app">
        <test v-for="(item,index) in items" :item="item" @show="show"></test>
</div>
<script>
Vue.component("test",{
    props:["item"],
    template:
    `
    <div>
        <p @click="show($event,item)">{{item.text1}}

<input v-model="item.text1" v-show="item.show_input" :ref="item.text1"></input> </div> `, methods:{ show(event,item){ this.$emit("show",event,item); } } }); new Vue({ el:"-sharpapp", data:{ items:[ { "text1":"aaaaaaa", "show_input":false }, { "text1":"bbbbbbb", "show_input":false } ] }, methods:{ show(event,item){ item.show_input=true; let ref_name=item.text1; console.log(this.$refs.ref_name); // undefined } } }); </script>
Mar.04,2021

how can you get the ref of input in your test component directly outside the component?
two methods:
1, in the test component: directly take the ref of input.

        show(event,item){
            this.$emit('show',event,item);
            console.log(this.$refs[item.text1]);
            this.$nextTick(() => {
              this.$refs[item.text1].focus()
            })
        }

2. External components: first get the ref, of the test component and then get the ref of the input.

        show(event,item){
          console.log(this.$refs)
            item.show_input = true;
            this.$nextTick(() => {
              // 
              this.$refs[`test-${item.text1}`][0].$refs['input'].focus()
            })
        }

is the virtual dom rendered successfully when you output or get the refs? get it after rendering. Learn about $nexttick

.
Menu