Creating a calendar component using Vuetify and TypeScript in a class-based Vue component

I'm currently in the process of setting up a calendar similar to the one showcased on vuetify's website

The only difference is that I'm utilizing class-components in TypeScript instead of JavaScript.

I'm encountering errors when making calls to

this.$refs.calendar.some_function
which are all clearly documented here

The error states: Property 'getFormatter' does not exist on type 'Vue | Element | Vue[] | Element[]'. Property 'getFormatter' does not exist on type 'Vue'.

Error message: Property 'prev' does not exist on type 'Vue | Element | Vue[] | Element[]'. Property 'prev' does not exist on type 'Vue'.

Error pops up: Property 'next' does not exist on type 'Vue | Element | Vue[] | Element[]'. Property 'next' does not exist on type 'Vue'.

Additionally, there are many other console errors appearing in the browser such as:

Issue with property or method "setToday" not being defined in the instance but referenced during render

This error seems to occur for every function. Could it be related to compilation issues in TypeScript?

The template structure mirrors the one displayed on their site, and my class setup looks like this (some non-affected functions removed):

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';

@Component
export default class AboutComponent extends Vue {
  private today: string = '2019-01-08';
  private focus: string = '2019-01-08';
  private type: string = 'month';
  private typeToLabel: any = {
    month: 'Month',
    week: 'Week',
    day: 'Day'
  };
  private start: any = null;
  private end: any = null;
  private selectedEvent: any = {};
  private selectedElement: any = null;
  private selectedOpen: boolean = false;
  private events: any[] = []; // Contains same events as the ones on their page

  private get title () {
    const { start, end } = this;
    if (!start || !end) {
      return '';
    }

    const startMonth: any = this.monthFormatter(start);
    const endMonth: any = this.monthFormatter(end);
    const suffixMonth: any = startMonth === endMonth ? '' : endMonth;

    const startYear: any = start.year;
    const endYear: any = end.year;
    const suffixYear: any = startYear === endYear ? '' : endYear;

    const startDay: string = start.day + this.nth(start.day);
    const endDay: string = end.day + this.nth(end.day);

    switch (this.type) {
      case 'month':
        return `${startMonth} ${startYear}`;
      case 'week':
        return `${startMonth} ${startDay} ${startYear} - ${suffixMonth} ${endDay} ${suffixYear}`;
      case 'day':
        return `${startMonth} ${startDay} ${startYear}`;
    }
    return '';
  }

  private get monthFormatter () {
    return this.$refs.calendar.getFormatter({
      timeZone: 'UTC', month: 'long'
    });
  }

  private prev () {
    this.$refs.calendar.prev();
  }

  private next () {
    this.$refs.calendar.next();
  }
}
</script>

How can I properly inform TypeScript about these existing functions? Appreciate the help!

Answer №1

After some searching, I stumbled upon the solution in this particular thread.

To fix my issue, I made the following modifications to my code:

private accessCalendar (): Vue & { prev: () => void, next: () => void,
    getFormatter: (format: any) => any } {
      return this.$refs.calendar as Vue & { prev: () => void, next: () => void, getFormatter: (format: any) => any };
    }

I also updated the functions that were using this.$refs.calendar to now utilize:

private formatMonth (): any {
  return this.accessCalendar.getFormatter({
    timeZone: 'UTC', month: 'long'
  });
}

private moveBackward (): void {
  this.accessCalendar.prev();
}

private moveForward (): void {
  this.accessCalendar.next();
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Supply type Parameters<T> to a function with a variable number of arguments

I am interested in utilizing the following function: declare function foo(...args: any): Promise<string>; The function foo is a pre-defined javascript 3rd party API call that can accept a wide range of parameters. The objective is to present a coll ...

How can you share a variable with all Vue components without the need to pass it through props every time?

One thing I'm curious about is the use of react consumer and provider for passing variables in React. Does Vue offer a similar feature? I have a Firebase class that needs to be passed to almost every component, and passing it through props doesn&apos ...

A step-by-step guide to creating a delete feature for nested data in Laravel using Vue.js

My standard function format for deleting is not working on nested data. methods: { delete() { axios.delete('./../api/parent/' + this.parent.id) .then( response => { if( re ...

Is there a way to determine if a User is still active using VUE, Django, and JWT?

Currently, I am utilizing rest_framework_jwt for managing the authentication process. Upon reviewing the documentation on Django REST framework JWT refresh-token, I came across the following statement: Each time a user interacts with the page, you can ve ...

What are the consequences of relying too heavily on deep type inference in React and Typescript?

In the process of migrating my React + Javascript project to Typescript, I am faced with preserving a nice unidirectional flow in my existing code. The current flow is structured as follows: 1. Component: FoobarListComponent -> useQueryFetchFoobars() 2 ...

Grouping various event listeners within a v-for loop

My Desired Outcome In my Vue component, I am displaying a list of actions using v-for. Each action should have a corresponding @click event handler that triggers the action method within the component. I need help declaring these actions in my data() fun ...

Is it possible to harness the power of a Typescript array within my HTML while incorporating primeng chips?

My subject line brings up a question: How can I transform the following piece of HTML code? HTML <p-chip label="1 day(s)" styleClass="p-mb-2 p-mr-2 custom-chip"></p-chip> <p-chip label="2 day(s)" styleClass=&qu ...

Stop the print dialog box from appearing when using the Ctrl + P shortcut

I'm working on an Angular app and I want to prevent the print dialog from opening when pressing "Ctrl + P". To address this issue, I have implemented the following code: window.onbeforeprint = (event) => { event.stopPropagation(); cons ...

What is the simplest way to incorporate Vue with Typescript, without the need for a complex build setup?

I've been spending the last couple of days experimenting with my basic ASP.NET Core website set up for Typescript 2.9, but unfortunately, I haven't made much progress. My main goal is to keep the front-end simple, with just single Vue apps on eac ...

What is the best way to retrieve the pressed key in an ng2 application?

I am encountering an issue with capturing the pressed key in my basic ng2 application. My goal is to determine which key was pressed each time. To achieve this, I set up a simple markup with an input field that detects keyup events to track the pressed k ...

binding data for an observable number in a loop

Is there a way to show stars for each item in a list using the 5-star scoring system on a video interface without creating an array for the score? interface Video{ Score: number; } <td> <span data-bind="foreach: score"> < ...

The Facebook Debugger fails to collect meta-tags from Nuxt pages

I'm currently utilizing Nuxt (Universal) in conjunction with Wordpress through Apollo and Graphql. Here's how I am fetching my content: async asyncData({ app, params, store, route, redirect }) { await store.dispatch("actionSetPageContent ...

Utilizing dark mode feature using vuex and storing it in local storage

I encountered a bug in Chrome that says "Write operation failed: computed property 'isDark' is readonly." My mutation creates a DarkMode key in the localStorage, and I also set up a store to check if DarkMode exists in the localStorage. It work ...

Issue with importing and exporting external types causing failures in Jest unit tests for Vue 2

I am in the process of creating a package that will contain various types, enums, consts, and interfaces that I frequently use across different projects. To achieve this, I have set up a main.ts file where I have consolidated all the exports and specified ...

Changing the value of a Redux object causes the TextInput within a FlatList item to lose focus

I have a nested object in my redux store that I am using to render my FlatList. Each item in the set contains a textInput to modify the reps in the set, but every time I change the value of the input, it becomes unfocused and the keyboard dismisses. Is the ...

Tips for concealing information within the column labeled company Name with respect to the field designated as Company Name

I am currently working on an Angular 7 app and I am facing an issue: I cannot hide the data in the column for Company Name. The field "Name" in the report control JSON is labeled as Company Name. The report control is a table that contains various fields ...

Tips for creating a dynamic component with v-for and utilizing dynamic slots

I am facing an issue with a child component that utilizes v-for. My goal is to allow the parent component to dictate how each item within the v-for loop is displayed by passing down a slot or similar mechanism. The challenge lies in the fact that the paren ...

I keep encountering the following error message: " ERROR Error Code: 200 Message: Http failure during parsing for http://localhost:3000/login"

My Angular Login component is responsible for passing form data to the OnSubmit method. The goal is to send form data from the front-end application and authenticate users based on matching usernames and passwords in a MySQL database. ***This login form i ...

Guide on setting up global typing for AngularJS in your project

I have been working on a project that initially used the deprecated typings method for incorporating Typescript definitions. I now want to transition to using the @types method instead. Currently, we have a typings.json file located in the root of the pro ...

The Vuetify treeview displayed all objects as open, even though I had not enabled the "open-all" option

Currently, I am configuring Vuetify's treeview component for my project. When I clicked on the folder objects within the treeview, every object opened up without me setting the 'open-all' option. My project is based on vue cli 3 and my ESLi ...