我正在使用 Typescript Class 组件,但我遇到了无法使用的问题this.$refs.<refname>.focus()
模板代码:
<header class="py-2 px-1 m-0 row">
<input
type="text"
class="form-control m-1"
ref="searchBoard"
placeholder="Find boards by name..."
/>
</header>
此输入字段位于弹出窗口中。
打字稿代码:
import { Component, Vue } from "vue-property-decorator";
@Component
export default class BoardList extends Vue {
// I added this to solve the compile error
$refs!: {
searchBoard: HTMLInputElement;
};
isShown = false;
toggleDropdown() {
this.isShown = !this.isShown;
this.$refs.searchBoard.focus();
}
}
然后我得到这个错误:
此问题已在此问题中修复Vuejs typescript this.$refs..value does not exist 添加:
$refs!: {
searchBoard: HTMLInputElement;
};
我的控制台出现新错误
[Vue warn]: Error in v-on handler: "TypeError: this.$refs.searchBoard is undefined"
found in
---> <BoardList> at src/components/boards/buttons/BoardList.vue
<NavbarTop> at src/components/NavbarTop.vue
<ComponentName> at src/App.vue
<Root> vue.runtime.esm.js:619
VueJS
7
有没有办法做到这一点?