如何使用VueJS关注负载?

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

$(document).ready(function() {
   $('#username').focus();
});

有没有办法?

推荐答案

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

// 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()
  }
})

然后像这样使用:

<input v-focus>

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

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

Vue.js相关问答推荐

Nuxt3 / Vue3 - 当属性更改时如何动态更改渲染组件?

在 Vue 文件中创建根 Vue 实例

如何将 vuejs 项目部署到 heroku

Vue 单元测试:您正在开发模式下运行 Vue?

Express.js + lint 出错

在 vue 组件中检索配置和环境变量

Vuex - 如何跨不同选项卡持久存储更新?

如何在某些异步数据(在 Vuex 存储中)加载之前防止任何路由?

在 v-for 循环中使用 v-model

Jest错误找不到模块....

Vuex - 何时从 http 服务器加载/初始化存储数据

如何突出显示 Vuetify 菜单中的选定项目?

如何使用 JavaScript 按索引删除数组元素?

Vue CLI 3 sass-resources-loader - Options.loaders 未定义

vuejs 配置:使用全局变量?

Vuetify RTL 风格

vuex 是 state.array.push react式的吗?

Vue 3 – 渲染无法动画的非元素根 node

在~/components/AddPlaceModal.vue中找不到导出AddPlaceModal

居中 v-toolbar-title