-
Notifications
You must be signed in to change notification settings - Fork 379
Method does't exist in $ref component #257
Comments
@cawa-93 This issue is caused by the type definition of I recommend you to ask this question at Vue's official forum or its discord server. Here is my solution. I usually separate component definition into <script>
// CommentForm.vue
import CommentForm from './CommentForm'
export default CommentForm
</script> // CommentForm.ts
@Component
export default class CommentForm extends Vue {
public focus() {
return 1;
}
} <script>
// Comments.vue
import Comments from './Comments'
import CommentForm from '@/components/comment-form.vue'
export default Comments.extend({
components: { CommentForm }
})
</script> // Comments.ts
import CommentForm from '@/components/comment-form';
@Component
export default class Comments extends Vue {
@Ref() readonly commentField!: CommentForm;
public tryFocus() {
this.commentField.focus(); // no error
}
} |
I also encountered this problem, do you have any solutions except separating component definition into .vue and .ts? |
I'm also experiencing this issue and would like to find a solution that doesn't require separating the component definition into multiple files. Given the example from @cawa-93 : export default class Comments extends Vue {
@Ref() readonly commentField!: CommentForm;
public tryFocus() {
this.commentField.focus(); // <- Error: TS2339: Property 'focus' does not exist on type 'Vue'.
}
} I would expect EDIT declare module '*.vue' {
import Vue from 'vue';
export default Vue;
} Anyway, I've found a couple ways around it.
@Component
export default class Comments extends Vue {
@Ref() readonly commentField!: CommentForm;
public tryFocus() {
// @ts-ignore
this.commentField.focus(); // no error
}
}
@Component
export default class Comments extends Vue {
@Ref() readonly commentField!: any;
public tryFocus() {
this.commentField.focus(); // no error
}
}
// components/types/index.ts
export interface CommentFormInterface {
focus: () => number;
} // Child component
@Component
export default class CommentForm extends Vue implements CommentFormInterface {
public focus() {
return 1;
}
} import CommentForm from '@/components/comment-form.vue';
import { CommentFormInterface } from '@/components/types';
@Component({
components: { CommentForm },
})
export default class Comments extends Vue {
// @Ref() readonly commentField!: CommentFormInterface; // Go To Definition navigates to interface definition
// or
@Ref() readonly commentField!: CommentForm & CommentFormInterface; // Go To Definition navigates to CommentForm component definition.
public tryFocus() {
this.commentField.focus(); // no error
}
}
@Component
export default class Comments extends Vue {
@Ref() readonly commentField!: CommentForm & { focus: () => number };
public tryFocus() {
this.commentField.focus(); // no error
}
} As for me, I'll probably just go with option 1 and silence the issue with |
I did handle this issue as following: export type VueComponent = { $el: HTMLElement } And use solution 4 from @jyork03: @Component
export default class Comments extends Vue {
@Ref() readonly commentField!: CommentForm & VueComponent
public tryFocus() {
this.commentField.focus(); // no error
}
} Thus you don't need to write all the HTMLElement methods and properties |
Example:
The text was updated successfully, but these errors were encountered: