Upgrading from Vue 2 to Vue 3 led to a typeError: Vue is not defined as a constructor

I'm in the process of migrating my Vue 2 syntax to Vue 3 and running into an issue with the following error message:

TypeError: Vue is not a constructor.

Here's my current code using Vue 3:

let app;

firebase.auth().onAuthStateChanged(user => {
  console.log("user", user);
  if (!app) {
    app = new Vue({
      router,
      store,
      render: h => h(App)
    }).$mount("#app");
  }
});

To successfully migrate to Vue 3, I need to make the following changes:

import { createApp } from "vue";

const app = createApp({
});

app.mount("#app");

Answer №1

To make the transition to Vue 3, Vuex 4, and Vue Router 4, you would need to update your code as follows:

import { createApp } from 'vue'
import store from './store'
import router from './router'
import App from './App.vue'

let app;

firebase.auth().onAuthStateChanged(user => {
  console.log("user", user);
  app = createApp(App);
  app.use(store);
  app.use(router);
  app.mount("#app");
});

In store.js, the syntax for creating the store has changed slightly:

import { createStore } from 'vuex'

// now uses `createStore`
export default createStore({ 
  state: {},
  getters: {},
  mutations: {},
  actions: {}
})

For the router configuration in router.js:

import { createWebHistory, createRouter } from "vue-router";
import Home from "@/views/Home.vue";
import About from "@/views/About.vue";

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/about",
    name: "About",
    component: About,
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

export default router;

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

Can you explain the function of "app.router" in the context of Express.js?

When looking at the default app.js file generated by express.js, I came across the following line: ... app.use(app.router); ... This particular line of code has left me perplexed for a couple of reasons. First, upon consulting the express api documentati ...

Eliminating Sunday and holiday hours between two specified dates

Looking for suggestions or methods to calculate idle time between two given date times? Challenges I'm facing include: If the start Date falls on a Sunday or Holiday: any remaining Sunday hours and the morning of the next day (until 6:30 AM) shoul ...

How can I utilize Handlebars and a JSON API call to effectively transfer data from an array response and display it within a Handlebars modal?

$("#searchMovieBtn").click(() => { const movieSource = $("#movie-template").html(); const movieList = Handlebars.compile(movieSource); const movieSearch = $("#addMovie").val(); console.log(movieSearch); queryURL = `https://ap ...

Creating a jQuery countdown script

Is it possible to easily convert the function below into jQuery and still maintain the ability to call multiple instances of the countdown? This particular function receives a server time echoed by the server and then initiates a countdown to the specifie ...

Interacting with a 3D model using the mouse cursor in a three

After stumbling upon the three.js library recently, I've been spending several days experimenting with it. I am eager to incorporate a mouse event into my project similar to this example where the head of the skull follows the cursor. However, I want ...

Steps for adding a div to a Vue template

When working with my template, I utilize a for loop to iterate through data entries. <tr v-for="entry in filteredData"> <td> @{{ entry.position}} </td> <td :class="addNewElement(entry.value)"> </td> <td> ...

I'm having trouble with my Discord app command. Every time I try to use it, I keep getting the error messages "The application did not respond" and "Dispatching MESSAGE_SEND_FAILED." How

Having a good amount of experience in JavaScript, I was optimistic about developing a Discord application as it seemed quite manageable. Eventually, I successfully integrated the bot into a server and implemented a command that is now listed in Discord&apo ...

Sliding the container with a width adjustment and left margin fails to display all images

In the provided HTML code below, there is a functionality to move images horizontally by clicking on buttons: $(document).ready(function() { calculate_width(); $('#moveleft').click(function() { var loga = $('#marki #loga'); ...

Is there a way to stop all currently running JavaScript functions when a Vue component is being destroyed?

Is there a way to terminate all active asynchronous functions associated with a component when the component is being destroyed? I am dealing with a View-component that fetches a large amount of data upon entering the page. The issue arises when the user ...

Form Validation Using a Separate Function

Currently, I am utilizing the jQuery Validation plugin from http://jqueryvalidation.org/ to validate my forms. By default, the validation process is triggered when the form is submitted. However, I would like to initiate the validation when a specific func ...

Send data in JSON format from an AngularJS client to an Express server

Having an issue when trying to send a JSON Object from an AngularJS $http service to an Express Server. The server receives an empty object: "{}" I've looked at this topic, but it hasn't resolved my problem: angular post json to express This is ...

Variable declared in $scope suddenly losing its definition

Within my directive controller, I've implemented a $watch function as follows: $scope.$watch(function () { return service.abc; }, function(newVal, oldVal) { $scope.abc = {abc: newVal}; }); However, I've encountered issues with the variabl ...

How come my HTML interpolation isn't showing up correctly in Vue.js?

I am experiencing trouble with getting the data in my app.js file to interpolate and show up on the screen. I attempted using the older v-repeat method, but still no luck. I'm wondering if there are some missing elements in the installation process. A ...

Efficiently organizing dates in the Vuetify date range picker

I am working with a vuetify date range picker component and have the following code: <v-menu ref="effectiveDateMenu" v-model="effectiveDateMenu" :close-on-content-cl ...

Display a view with Vue Router based on specific conditions

Currently, I am working on a layout that includes optional sidebars based on the vue routes. Here is what I have so far: In my App.vue, I have a MainLayout component with slots where the router views are rendered. <main-layout> <template #m ...

Leveraging a shared directory within an npm project

In my Vue project, I have multiple clients with similar components stored in a "common" folder structure: clients -- client1 ---- ... -- client2 ---- ... -- client3 ---- ... -- common ---- imports.js ---- ... Currently, each project has its own package.j ...

What is the best way to create a "comment posting and replying" section on a website using either PHP or Javascript?

I'm currently in the process of developing my own personal website. I believe that incorporating a section for "user comments and replies" would be a great way to enhance my site. I prefer to avoid user email verification. I'd like users to be a ...

The functionality of the `next-auth` signOut button click does not seem to accept a variable as input, although

The Nav.jsx component code provided is working as intended. In this implementation, clicking the 'Sign Out' button will redirect the application to http://localhost:3000. 'use client' import { signOut } from 'next-auth/react&apos ...

What is the best way to address a NULL value within a VueJS filter?

This piece of code demonstrates a working filter filteredItems() { return this.items.filter(item => item.name.toLowerCase().indexOf(this.search.toLowerCase()) > -1) } However, my attempt to filter on a second column ...

Utilizing jQuery to create a blocking modal with a spinning gif, without the need for jQuery Block

When working on a Bootstrap page and faced with a time-consuming task, it's common to want a spinning GIF to show that the process is happening and to prevent users from interacting with certain parts of the UI. While jQuery offers the plugin jQuery ...