Is it possible to begin the vue root instance by using a .vue class component?

Utilizing vue-class-component allows me to incorporate class syntax and TypeScript type checking within my .vue files. I can easily create .vue files and register them as components using this method, with the exception of the root Vue() instance.

This approach is successful

The hello.vue file demonstrates the usage of vue-class-component (abridged for conciseness)

<template></template>

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

    @Component({
    })
    export default class Hello extends Vue {
        created() {
            console.log("I am created!")
        }
    }
</script>

Subsequently, importing hello in the root Vue instance can be done in the following manner:

import Hello from "./components/hello.vue"

let v = new Vue({
    el: "#app",
    template: `<div><hello></hello></div>`,
    components: {Hello},
    created : function(){
        console.log("root instance created")
    }
});

However, a roadblock emerges here

The desire to use the same class syntax when creating the root Vue instance sets forth a challenge:

app.vue

<template><hello></hello></template>

<script lang="ts">
    import Vue from 'vue'
    import Component from 'vue-class-component'
    import Hello from "./components/hello.vue"

    @Component({
        el: "#app",
        components: {Hello}
    })

    export default class App extends Vue {
        created() {
            console.log("created the root instance")
        }
    }
</script>

Consequently, importing app.vue in index.ts raises complications.

import Vue from "vue"
import App from "./components/app.vue"

let vm = new Vue(App)

An attempt to utilize App for initializing the Vue root instance triggers the following error message:

Argument of type 'typeof Vue' is not assignable to parameter 
of type 'ComponentOptions<Vue> | undefined'

In order to resolve this issue, how do I define a ComponentOptions? Furthermore, is it conceivable for a .vue file to serve as the entry point of the application instead of index.ts?

Answer №1

In an options object, the components section can define components either by spec or directly including the component itself (although this is not commonly documented).

The Vue constructor doesn't accept a component, but requires a spec instead. Since a component already extends Vue, there's no need to pass it through the Vue constructor again. It should simply be:

let vm = new App();

It has been noted in the comments that specifying el should be done during the constructor call and not within the @Component. Therefore, it should be written as follows:

let vm = new App({el: '#app'});

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

What is the reason why the beforeDestroy method is not invoked during a refresh, but the created method is triggered

I'm attempting to trigger a page refresh in vue.js. When doing so, I notice that the created hook is being called. Why isn't the destroyed hook triggered instead? If the created hook is being called, it suggests that the component should have be ...

How come the Angular8 form status registers as VALID even if the input fields are empty?

The following is the structure of the component: export class SchedulerComponent implements OnInit { schedulerForm : FormGroup; constructor(private fb: FormBuilder, private schedulerReportService: SchedulerReportService) { this. ...

Tips for sending information back to the previous screen in React Native Navigation version 5

Recently, I upgraded to react native navigation version 5 and now I am facing an issue with sending data back to the previous screen when making a goBack() call. To navigate to the next view, I use: const onSelectCountry = item => { console.log(it ...

Error: Observable<any> cannot be converted to type Observable<number> due to a piping issue

What causes the type error to be thrown when using interval(500) in the code snippet below? const source = timer(0, 5000); const example = source.pipe(switchMap(() => interval(500))); const subscribe = example.subscribe(val => console.log(val)); V ...

Vue component input form not providing expected result

Here is the code snippet for my "ecedata" component with an input field: <template> <div class="col-l-4"> <p style="text-align: center">Data/day (GB)</p> <div class="form-input-set" style="background: white"& ...

When attempting to log out of Keycloak, a TypeError occurs in the front-end application stating that it cannot read properties of undefined related to the action of logging out

I've been trying to implement a logout feature in my front-end application using Keycloak, but I'm encountering difficulties. Most of the examples I found online are for older versions of Keycloak and use 'auth' and 'redirectURI&ap ...

Using custom properties from the Material-UI theme object with custom props in Emotion styled components: a step-by-step guide

I have implemented a custom object called fTokens into the MUI theme using Module augmentation in TypeScript This is how my theme.d.ts file is structured declare module "@mui/material/styles" { interface FPalette { ... } interface FTokens ...

Utilizing JSDoc's "includePattern" for various file formats

Recently, I delved into JSDocs and decided to implement it in my Vue.js project. However, since my project involves multiple file types like .js and .vue, I encountered a syntax error while trying to add them to the "includePattern". Here's the snippe ...

Using FIND to search an array object results in an error: Type 'undefined' is not compatible with type ''

I'm currently attempting to search for an element within one array and then assign it to another array object. However, I keep receiving the following error message: Type 'ClosureSummary | undefined' is not assignable to type 'Closure ...

How to retrieve an element using a dynamically generated class name in Vue.js

<v-data-table :headers="menuheaders" //this menus from api response :items="menus" item-key="usersmenu_menuid" items-per-page="1000" hide-default-footer="" class="elevation-1" > <template v-s ...

How can you deduce the type from a different property in Typescript?

I have encountered obstacles in my development process and need assistance overcoming them. Currently, I am trying to configure TObject.props to only accept 'href' or 'download' if the condition TObject.name = 'a' is met, and ...

Vue-Lottie meets Nuxt: the perfect combination

Has anyone successfully integrated Vue-Lottie with Nuxt? I attempted to import it as instructed by Vue-Lotti: import Lottie from './lottie.vue'; However, it threw an error saying it couldn't locate the package. So then I tried importing i ...

Leverage vuejs' this.$refs to work with multiple elements at once

I am working with 4 select elements: <div class="form-row"> <div class="form-group col-3"> <label for="stateSelect">Status</label> <select v-model ...

Sending properties to MUI Box component enhancer (Typescript)

I'm having trouble figuring out how to pass props to override the Box component. I specifically need to pass position="end" as InputAdornment requires it, but I can't seem to find the proper way in the documentation. Here's the complete co ...

JavaScript: specify parameters for function inputs

I'm curious about optimizing Javascript/Typescript functions arguments for clean code. To demonstrate, let's consider a basic example. Imagine I have a React component with a view property as props: <Grid view="Horizontal" /> ty ...

Vuetify: Enable editing for specific column in v-data-table cells

I recently created a simple record display using vuetify with v-data-table. https://i.stack.imgur.com/mEc4c.png I have implemented an editable cell feature for adding data. When you click on any column cell, a popup edit box appears to modify the data. ...

“Can you confirm if this syntax is correct for defining methods in Vue.js?”

When defining methods in my Vue app, I have chosen to use the following format: methods: { myMethod(arg) { // do something here } } Although it is more common to see it written like this: methods: { myMethod: function (arg) { // do somethi ...

Enumerate all interdependencies among 2 directories or libraries in an Angular application

I am currently working on a large Angular project and need to refactor some code by identifying dependencies between two specific folders/libs (using nx). Here is an example of the file structure: /apps /lib-1 a.service.ts b.component.t ...

Is there a way for me to specifically show just the initial image contained in the images[] array within the json data?

{"status": true, "data": {"basic": {"dob": "2018-02-02", "gender": "male", "contact": {"address": null}, "is_new": false, "is_email_verified": false, "is_phone_verified": false}, "listings": [{"pid": 109, "category": {"name": "Education"}, "location": {"l ...

Call a function within a stateless component in a React application

I have a question regarding my React component. I am attempting to call the function ButtonAppBar within my stateless component, but the TypeScript compiler is throwing an error stating '{' expected. I'm unsure whether I need to pass it to m ...