vuejs2 - VUE:通过 v-if 显示后选择输入
问题描述
我有以下标记:
<div id="app">
<button class="button is-primary" @click="showMyTF" ref="mybtn">
Show my Textfield:
</button>
<input class="input" type="text" value="My textfield value"
class="formfield" v-if="showTextField" ref="mytf"/>
</div>
和 Vue 代码:
new Vue({
el: "#app",
data: {
showTextField: false
},
methods: {
showMyTF() {
this.showTextField = true;
this.$refs.mybtn.innerText = "There you have it";
this.$refs.mytf.select();
}
}
})
我在这里做了一个 JSFiddle 示例: https ://jsfiddle.net/Pintiboy/Lkec1n5g/
我的问题是:是否可以在显示后立即选择文本字段的文本(通过单击按钮)。它在那里后确实有效(第二次单击按钮),但我希望它在出现后立即被选中。有任何想法吗?
解决方案
您可以使用this.$nextTick
showMyTF() {
this.showTextField = true;
this.$refs.mybtn.innerText = "There you have it";
this.$nextTick(() => {
this.$refs.mytf.select();
})
}
jsfiddle中的演示
推荐阅读
- r - 当有多个“键”时如何合并数据集
- function - 使用参数开关更改函数的行为方式
- java - 如何将一组包含对象列表的对象插入 Firebase
- python - 我怎样才能用魔杖作曲?
- ms-access - 您是否能够对整个访问记录使用条件格式?根据一个参数更改整个记录颜色?
- java - React Native:错误:找不到符号 > 新 AppCenterReactNativeAnalyticsPackage(MainApplication.this
- progressive-web-apps - Ionic 4 PWA 启动画面更改位置路径
- php - 无法从表单将数据发送到 mysql
- browser - JXBrowser,如何更改使用的默认图像查看器?
- shell - 将数组传递给函数不起作用