【问题标题】:Vuex ORM model constructor issue on insert插入时出现Vuex ORM模型构造函数问题
【发布时间】:2020-01-27 19:39:31
【问题描述】:

我有以下型号:

import { Model } from '@vuex-orm/core'

export class User extends Model {
    static entity = 'users'

    static fields () {
        return {
            id: this.attr(null),
            name: this.attr('')
        }
    }
}

当我尝试这样做时:

User.insert({
    data: { id: 1, name: 'John' }
})

我收到以下错误:

Uncaught (in promise) TypeError: Class constructor User cannot be invoked without 'new'

知道是什么问题吗?这段代码来自文档,所以我有点困惑。

【问题讨论】:

  • 我遇到了同样的问题。

标签: vuex-orm


【解决方案1】:

我可以看到您在应用程序开始时缺少模型在数据库中的注册。

你必须使用相应的插件来实现你的 vuex 存储 示例:

import Vue from 'vue'
import Vuex from 'vuex'
import VuexORM from '@vuex-orm/core'
import User from '@/models/User'
import Post from '@/models/Post'

Vue.use(Vuex)

// Create a new instance of Database.
const database = new VuexORM.Database()

// Register Models to Database.
database.register(User)
database.register(Post)

// Create Vuex Store and register database through Vuex ORM.
const store = new Vuex.Store({
  plugins: [VuexORM.install(database)]
})

export default store

You now have good documentation in this regard

希望对你有所帮助,问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 2011-10-16
    • 2021-10-20
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多