【问题标题】:How can i get in Nuxt/Vue Store from js async function我如何从 js 异步函数进入 Nuxt/Vue Store
【发布时间】:2021-08-27 09:26:12
【问题描述】:

我在 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?

【问题讨论】:

    标签: javascript node.js vue.js nuxt.js fingerprint


    【解决方案1】:

    这段代码你在哪里?

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

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

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


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

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

    【讨论】:

    • 无需会员登录。我想在没有会员资格的情况下获得指纹。所以我在 store/index.js (Store) 中使用代码
    • “登录”只是示例的一部分。如果您已经在商店中,只需在其中写下您的东西。你想在这里用 Vuex 实现什么?
    • 我希望能够反复使用它或将其分配给一个状态。例如:我想在向数据库添加 upvote 时添加访问者 ID。但我得到承诺作为回应。当我尝试在 nuxtServerInit 中使用它时它不起作用。
    • @Orates 我在这里没有得到你想要的。
    猜你喜欢
    • 2021-08-23
    • 2021-01-24
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    相关资源
    最近更新 更多