首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在nextjs中实现osano cookieconsent

如何在nextjs中实现osano cookieconsent
EN

Stack Overflow用户
提问于 2021-05-11 17:34:42
回答 1查看 455关注 0票数 0

到目前为止,我一直在尝试初始化根登录页面useEffect中的cc对象:

代码语言:javascript
复制
const CC = require( "CookieConsent")
....

useEffect(() => {
  const cc = new CC({
    container: document.getElementById("cookieconsent"),
    palette: {
    popup: { background: "#1aa3ff" },
    button: { background: "#e0e0e0" },
    },
    revokable: true,
    onStatusChange: function(status) {
    console.log(this.hasConsented() ?
      'enable cookies' : 'disable cookies');
    },
    "position": "bottom-left",
    "theme": "classic",
    "domain": "<myAppURL>",
    "secure": true,
    "content": {
      "header": 'Cookies used on the website!',
      "message": 'This website uses cookies to improve your experience.',
      "dismiss": 'Got it!',
      "allow": 'Allow cookies',
      "deny": 'Decline',
      "link": 'Learn more',
      "href": 'https://www.cookiesandyou.com',
      "close": '&#x274c;',
      "policy": 'Cookie Policy',
      "target": '_blank',
    }
  })
}, [])

return (
...
<div id="cookieconsent"></div>
...
)
....

当我这样做的时候,我得到了"ReferenceError: window is not defined“错误。有什么想法我可以实现它吗?

Osano git:https://github.com/osano/cookieconsent/

EN

回答 1

Stack Overflow用户

发布于 2021-07-15 22:45:52

您应该动态导入"CookieConsent“,以便它只在客户端加载,而不是在服务器上加载。例如:

代码语言:javascript
复制
// remove
const CC = require( "CookieConsent")
        
// add
const CC = dynamic(() => import("../node_modules/cookieconsent/src/cookieconsent"), {
  ssr: false
});

您必须确保从原始的require语句显式编写到"CookieConsent“的路径,就像我的动态导入示例一样。

然后,窗口对象将可用,因为窗口对象在浏览器中仅在客户端可用,作为窗口。

看看这里的文档以供参考:https://nextjs.org/docs/advanced-features/dynamic-import

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67484131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档