Typescript + Vue 响应式导出变量

huangapple go评论72阅读模式
英文:

Typescript + Vue reactive exported variable

问题

设置

我有一个Vue3 + Firebase项目。
在其main.ts中,我像这样导出了一个名为userInfo的对象:

export var userInfo :any = {}

> 不要在意:any,它是为了解决Typescript的*some-property* does > note exist on type Object问题。

这个对象是由一个钩子设置的,也在main.ts内运行,当用户被验证时如下:

auth.onAuthStateChanged(async () => {
    if(!auth.currentUser) return

    userInfo = await getUserInfo()
})

现在当然,只有在onAuthStateChanged钩子触发后,该对象才会被填充,这是在我的.vue文件中处理导入之后。

问题

当在某个.vue文件中导入我的userInfo时,使用import {userInfo} from '@/main'
该文件当然会导入仍然是空的对象。
onAuthStateChanged钩子触发并填充该对象之后的某个时候。

然而,组件不会刷新导入,这意味着我在我的.vue文件中没有我需要的数据。

问题

我希望我的userInfo的收集是集中管理的,然后只需将其导入到我的.vue组件中,而不是每次需要使用它时都手动收集它。

如何解决这个在导入处理之后填充对象的问题?

英文:

Setup

I have a Vue3 + Firebase project.
In its main.ts i am exporting an object called userInfo like this:


export var userInfo :any = {}

> Don't mind the :any, it is to fix Typescripts *some-property* does
> note exist on type Object
.

This object is set by a hook, also inside main.ts that runs when a user is authenticated as follows:

auth.onAuthStateChanged(async () => {
    if(!auth.currentUser) return

    userInfo = await getUserInfo()
})

Now of course, the object is only filled when the onAuthStateChanged hook fires, which is after imports are handled in my .vue files.

Problem

When importing my ´userInfo´ in some .vue file with import {userInfo} from '@/main'
the file of course imports the still empty object.
Sometime after the onAuthStateChanged hook fires and fills the object.

The component however does not refresh the import, meaning i do not have the data that i need inside my .vue file.

Question

I would like the gathering of my userInfo centralized and then just import it into my .vue components instead of manually gathering it everytime i need to use it.

How do i work around this problem of the object being filled after imports are handled?

答案1

得分: 1

其他评论和答案建议使用一个存储库,这应该是您在项目中正在寻找的东西。由于您正在使用Vue3和Typescript,我建议使用Pinia,这是最新的Vue应用程序的当前存储库管理器,而不是VueX,它是较旧的存储库管理器。

您可以在文档中找到非常好的示例。我个人在一个类似的个人项目中使用了Pinia 类似的方式,也许这可以帮助您。

英文:

Other comments and answers are suggesting to use a store and it should be what you are searching for in your project. As you are using Vue3 and Typescript, I would recommend using Pinia which is the current up to date store manager for recent Vue applications and not VueX which is the older store manager.

You can find really good examples in the documentation. I personally used Pinia in a similar fashion in a personal project, maybe this can help you.

答案2

得分: 0

你可以使用 Vuex 来创建一个突变。因此,所有子组件和父组件都可以从任何地方进行交互。
示例:https://jasonwatmore.com/post/2018/07/14/vue-vuex-user-registration-and-login-tutorial-example

此外:

export async function getAuthUserID(): Promise<string | undefined> {
  return await new Promise((resolve, reject) => {
    firebase.auth().onIdTokenChanged(async (user) => {
      if (!user) {
        resolve(undefined);
      } else {
        resolve(user.uid);
      }
    })
  }).then((uid: string | undefined) => uid).catch(function (e) {
    throw (e);
  });
}
英文:

You can create a mutation using vuex. So all child children and parent parents can interact from anywhere
example : https://jasonwatmore.com/post/2018/07/14/vue-vuex-user-registration-and-login-tutorial-example

Moreover :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

export async function getAuthUserID(): Promise&lt;string | undefined&gt; {
  return await new Promise( (resolve, reject) =&gt; {
    firebase.auth().onIdTokenChanged(async (user) =&gt; {
      if (!user) {
        resolve(undefined);
      } else {
        resolve(user.uid);
      }
})
  }).then((uid: string | undefined)=&gt;uid).catch(function(e) {
    throw (e);
  });
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 21:37:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定