5

我很难让 AWS Amplify 与Vite.js 一起工作

// First I was getting this error:
Uncaught ReferenceError: global is not defined

所以,我script在 index.html 的head部分添加了这个

<script>
  var global = global || window;
  var Buffer = Buffer || [];
  var process = process || {
    env: { DEBUG: undefined },
    version: []
  };
</script>

现在,我收到此警告/错误

[Vue warn]: Failed to resolve component: amplify-sign-out 
[Vue warn]: Failed to resolve component: amplify-authenticator 

您可以在此处查看完整的日志: 在此处输入图像描述

4

2 回答 2

4

通过在应用程序根目录中创建vue.config.js文件并添加以下内容,我能够修复解析组件错误:

module.exports = {
  chainWebpack: config => {
    config.module
      .rule('vue')
      .use('vue-loader')
      .tap(options => {
        options.compilerOptions = {
          ...(options.compilerOptions || {}),
          isCustomElement: tag => tag.startsWith('amplify-')
        };
        return options;
      });
  }
};
于 2021-04-28T12:03:19.000 回答
0

根据AWS Amplify doc,您需要添加app.config.isCustomElement = tag => tag.startsWith('amplify-');到您的main.ts文件中。

由于您使用的是 vite,因此您也可以按照vite 示例进行操作。该vite.config.ts文件应该像

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";

export default defineConfig({
  plugins: [
    vue({
      template: {
        compilerOptions: {
          isCustomElement: (tag) => tag.startsWith("amplify-"),
        },
      },
    }),
  ],
});

于 2021-06-27T05:11:22.223 回答