我正在使用Nuxt创建网页,我想创建一个类并在我的.vue文件之一中使用它。我尝试使用导入:import Card from "~/assets/mylib/Card.js"
,但是不起作用。不确定如何在.vue文件中访问我的Card.js文件。
index.vue
import Card from "~/assets/mylib/Card.js"
created() {
let card = new Card("blue")
}
Card.js
class Card {
constructor(color) {
this.color = color
}
}
错误:
_assets_mylib_Card_js__WEBPACK_IMPORTED_MODULE_4___default.a is not a constructor
修改Card.js如下:
export default class Card {
constructor(color) {
this.color = color
}
}
然后从index.vue内部导入它,如下所示:
import { Card } from "~/assets/mylib/Card"
您必须像以前那样更新您的Card.js
export class Card { constructor(color) { this.color = color } }
并按如下所示导入Vue文件
import { Card } from "~/assets/mylib/Card"