我试图在Vue.js中设置输入元素的焦点。我在网上找到了一些帮助,但没有任何解释对我有用。在vue.js中设置输入元素的焦点
这里是我的代码:
<template>
<form method="post" action="" v-on:submit.prevent="search">
<input type="text" placeholder="Person name" required v-model="name" v-el="nameInput" />
<input type="text" placeholder="Company" required v-model="company" v-el="domainInput" />
<input type="submit" value="Search" class="btn show-m" />
</form>
</template>
<script>
export default {
data() {
return {
contacts: [],
name: null,
company: null
}
},
ready: {
// I tried the following :
this.$$.nameInput.focus();
this.$els.nameInput.focus();
// None of them worked !
}
methods: {
search: function (event) {
// ...
// I also would like to give the focus here, once the form has been submitted.
// And here also, this.$$ and this.$els doesn't work
},
}
}
</script>
我想什么,我能在网上找到目标的重点this.$$.nameInput.focus();
和this.$els.nameInput.focus();
,但this.$$
是不确定的,而this.$els
是空的。
如果能帮忙,我使用vue.js v1.0.15
谢谢您的帮助。
这就是奇怪,为什么functionning的是这样的:
然后你就可以在输入和其他元素使用
v-focus
属性? (如果你有任何想法)。 'v-el =“XXX”'更符合逻辑否? –但是这个工作,所以你应该有我接受的答案:)谢谢 –
[这](https://github.com/vuejs/vue/issues/1292)是当前功能的基本思维过程。 – Andrius