加载焦点文本框上的Vue.js

Kok*_*zzu 6 vue.js vuejs2

如何使用VueJS专注于负载?

如果使用jquery,我应该做的就是:

$(document).ready(function() {
   $('#username').focus();
});
Run Code Online (Sandbox Code Playgroud)

有没有vue-way?

sob*_*evn 13

您可以为此创建自定义指令:

// Register a global custom directive called v-focus
Vue.directive('focus', {
  // When the bound element is inserted into the DOM...
  inserted: function (el) {
    // Focus the element
    el.focus()
  }
})
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

<input v-focus>
Run Code Online (Sandbox Code Playgroud)

文档中的完整示例:https://vuejs.org/v2/guide/custom-directive.html

指令文档:https://vuejs.org/v2/api/#Vue-directive