微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何从 js 异步函数进入 Nuxt/Vue Store

如何解决我如何从 js 异步函数进入 Nuxt/Vue Store

我在 Nuxt 项目中使用了指纹模块,我是前端框架的新手。

我可以在控制台中获取用户指纹代码,但我无法使用 VuexStore 实例。

if (process.browser) {
  const fpPromise = FingerprintJS.load();
  (async () => {
      // Get the visitor identifier when you need it.
      const fp = await fpPromise
      const result = await fp.get()

      // This is the visitor identifier:
      const visitorId = result.visitorId
      console.log(visitorId)
  })()
}

如何在visitorId的实例中使用vuexStore?

解决方法

你在哪里有这个代码?

如果它在 Nuxt 插件中(例如 /plugins/my-plugin.js),那么你可以使用它

export default ({ store }) => {
  // your code
}

如下所示:https://nuxtjs.org/docs/2.x/concepts/context-helpers


如果您在 script 文件中的常规 .vue 部分,那么这应该足够了

if (this.$store.state.auth.loggedIn) {
  // your code
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。