<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
</div>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
var App = {
data() {
return {
isShow: false
}
},
template: `
<div>
<input type="text" v-show="isShow" ref="input">
</div>
`,
mounted() {
this.isShow = true;
// console.log(this.$refs.input); // <input type="text" style>
// this.$refs.input.focus(); // 页面加载后,input不会自动获取焦点,原因:使用了v-show,$refs获取不到更新后的DOM
this.$nextTick(() => {
this.$refs.input.focus();
});
}
}
new Vue({
el: '#app',
data() {
return {
}
},
template: `<App />`,
components: {
App
}
});
</script>
</body>
</html>