简体   繁体   English

将 Vue 2 迁移到 Vue 3,typeError: Vue is not a constructor

[英]Migrating Vue 2 to Vue 3, typeError: Vue is not a constructor

How can I migrate my Vue 2 syntax to Vue 3, because I'm receiving the following error:如何将我的 Vue 2 语法迁移到 Vue 3,因为我收到以下错误:

TypeError: Vue is not a constructor. TypeError:Vue 不是构造函数。

Right now I'm using Vue 3:现在我正在使用 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

import { createApp } from "vue";

const app = createApp({
});

app.mount("#app");

The equivalent of your code in Vue 3, Vuex 4, Vue Router 4 would be something like:您在 Vue 3、Vuex 4、Vue Router 4 中的代码相当于:

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");
});

The store syntax is slightly different in store.js : store.js中的 store 语法略有不同:

import { createStore } from 'vuex'

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

And the router in router.js :还有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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM