构建 Vue.js 项目时使用 Gridsome 时出现 TypeError

TypeError when using Gridsome while building a Vue.js project

我正在使用 Vue.jsGridsome 为自己创建一个投资组合。但是,当我在站点中添加 JSON 文件以包含我的个人资料信息时,我遇到了问题。这就是我在 Index.vue 组件中导入文件的方式:

<script>
import Intro from "~/components/Intro.vue";
import profile from "~/data/profile.json";

export default {
  components: {
    Intro,
  },
  metaInfo: {
    title: "Farzin Nasiri",
  },
  data: () => ({
    profile
  }),
};
</script>

下面是我的使用方法:

<Intro :personal="profile.personal" />

当我运行开发中的项目(命令:gridsome develop)时,一切正常,数据读取正常。但是,当我想构建在 dist 文件夹中创建构建文件的项目(命令:gridsome build)时,会发生此错误:

Initializing plugins...
Load sources - 0.04s
Create GraphQL schema - 0.05s
Create pages and templates - 0.03s
Generate temporary code - 0.03s
Bootstrap finish - 0.67s
Compile assets - 13.28s
Execute GraphQL (6 queries) - 0.03s
Write out page data (6 files) - 0s
Could not generate HTML for "/":
TypeError: Cannot read property '__esModule' of undefined
    at i (/home/farzin/MyProjects/portfolio.farzinnasiri.com/node_modules/vue-server-renderer/build.prod.js:1:68670)

这是我的项目结构(src 文件夹):

├── components
│   ├── Intro.vue
│   └── README.md
├── data
│   └── profile.json
├── favicon.png
├── layouts
│   ├── Default.vue
│   └── README.md
├── main.js
├── pages
│   ├── About.vue
│   ├── Index.vue
│   └── README.md
├── templates
│   ├── README.md
│   └── Work.vue
└── vendor
    └── bootstrap.min.css

这是我的 package.json:

{
  "name": "portfolio.farzinnasiri.com",
  "version": "0.0.2",
  "private": true,
  "scripts": {
    "build": "gridsome build",
    "develop": "gridsome develop",
    "explore": "gridsome explore"
  },
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.28",
    "@fortawesome/free-brands-svg-icons": "^5.13.0",
    "@fortawesome/free-solid-svg-icons": "^5.13.0",
    "@fortawesome/vue-fontawesome": "^0.1.9",
    "@gridsome/source-filesystem": "^0.6.2",
    "@gridsome/transformer-remark": "^0.5.0",
    "gridsome": "^0.7.0"
  }
}

我真的不明白问题出在哪里,需要帮助来解决它。感谢您的宝贵时间

Gridsome & SSR

来自Gridsome docs

gridsome build uses server-side rendering (SSR) to create a fully rendered page. If your Vue component does not support SSR ... it won't be rendered properly.

we suggest you to encapsulate the component inside <ClientOnly></ClientOnly> tags and import the library inside Vue's mounted() handler.

您收到的错误来自 vue-server-renderer,它来自 Vue 的服务器端 API。您需要执行上述说明。

~导入

此外,在不知道您如何在开发中启用加载 JSON 作为模块(即 ~)的情况下,也许这是一个问题,因为这在默认的 Vue CLI 中不起作用环境。

尝试从所有导入中删除 ~

import profile from "@/data/profile.json";

您必须将导入的对象传递给 vue,如下所示:

data: () => ({
    myProfile:profile
}),

所以你可以这样使用它:

<Intro :personal="myProfile.personal" />