Как получить id input vue.js

<input id="surname" v-model="name">

<script>
data(){
   name: 'some name'
}
methods:{
  getName(){
      console.log(this.name)
  }
}
</script>

Как получить id элемента input ?


Ответы (1 шт):

Автор решения: Eugene Kuznetsov

Вы можете к вашему input добавить атрибут ref, и в скрипте производить нужные манипуляции, пример ниже:

var app = new Vue({
  el: '#app',
  data: {
    message: ''
  },
  methods:{
    getNameId(){
        console.log(this.$refs.textName.id);
    }
  }
})
<htmL>
  <body>
    <div id="app">
       <input id="surname" ref="textName" v-model="message">
       <button @click="getNameId">test</button>
     </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"> 
    </script>
  </body>
</html>

→ Ссылка