我在 NuxtJS+Firebase 项目 VuexStore 中使用了指纹。
当我在客户端调用该函数时可以获得访客 ID。但是如果我像 nuxtServerInit 一样在服务器端使用,我就无法获得。
const fpPromise = FingerprintJS.load();
const abc = (async() => {
const fp = await fpPromise
const result = await fp.get()
const visitorId = result.visitorId
return visitorId;
})()
abc.then(
function(value) {
state.visitorId = value
},
function(error) {
return error
}
)
有什么解决办法吗?
答案 0 :(得分:0)
来自NuxtJS documentation(关于服务器渲染):
<块引用>因为您在 Node.js 环境中,所以您可以访问 Node.js req 和 res 等对象。您无权访问窗口或 文档对象,因为它们属于浏览器环境。你可以 但是通过使用 beforeMount 或 mount 来使用窗口或文档 钩子。
FingerprintJS 严重依赖(例如 here)浏览器(因此浏览器指纹识别)。这意味着它需要例如window
对象在服务器端渲染上下文中不可用。
我对 NuxtJS 不是很有经验,但是,according to the documentation,您应该将指纹代码添加到 .vue
文件中,例如
if (process.client) {
require('external_library')
}
祝你好运!