繁体   English   中英

aws 放大是否适用于 NextJS?

[英]Does aws amplify work with NextJS?

我只是想用 NextJS 和 aws-amplify 运行一个基本的 hello world,但似乎是我 npm 安装这两个库的那一刻

aws-amplify 和 aws-amplify-react

我得到“缺少反应模块”& 窗口未定义。

import React from 'react'
import Amplify from 'aws-amplify';

Amplify.configure({
    Auth: {
        // REQUIRED - Amazon Cognito Identity Pool ID
        identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab',
        // REQUIRED - Amazon Cognito Region
        region: 'XX-XXXX-X',
        // OPTIONAL - Amazon Cognito User Pool ID
        userPoolId: 'XX-XXXX-X_abcd1234',
        // OPTIONAL - Amazon Cognito Web Client ID
        userPoolWebClientId: 'XX-XXXX-X_abcd1234',
    }
});

export default class extends React.Component {
    static async getInitialProps({ req }) {
        const userAgent = req ? req.headers['user-agent'] : navigator.userAgent
        return { userAgent }
    }

    render() {
        return (
            <div>
              Hello World
              <style jsx>{`

            `}</style>
            </div>
        )
    }
}

您需要制作某种 polyfill 以避免该window is not defined错误。 也可能您需要检查您的node_modules文件夹以查看是否正确安装了react

polyfill 示例:```

(<any>global).window = (<any>global).window || {};
(<any>global).localStorage = (<any>global).localStorage || {
    store: {},
    getItem(key){
        if (this.store[key]) {
            return this.store[key];
        }
        return null;
    },
    setItem(key, value){
        this.store[key] = value;
    },
    removeItem(key){ delete this.store[key]; }
};

``

试试这个。 安装tslib然后测试

npm 安装 tslib

暂无
暂无

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

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