Vue3响应式和封装

发布于 2025-01-10 08:31:32 字数 1618 浏览 2 评论 0原文

我是 Vue 新手,目前正在使用 Vue3。作为学习练习,我正在制作一个简单的登录插件,并且进展顺利。我正在将reactive()与下面的类的实例一起使用,但是如果属性更改是在内部分配的,则在更新属性更改时会遇到一些麻烦。这是有问题的代码:

class dtAuthHandler {
  private _IsLoggedIn: boolean = false;
  public get IsLoggedIn(): boolean {
    return this._IsLoggedIn;
  }
  public set IsLoggedIn(value:boolean)
  {
    this._IsLoggedIn = value;
  }

  Login = () => {
    // More stuff here, internal state, etc.
    this.IsLoggedIn = true;
  }

  Logout = () => {
    // More stuff here, internal state, etc.
    this.IsLoggedIn = false;
  }
}

这是一个简单的函数,我用它来演示登录状态的切换。在第一种情况下,我可以直接设置属性并且 UI 按预期更新,非常棒。在第二种情况下,我想使用特定的 Login()/Logout() 函数,以便该类可以维护某种内部状态,与服务器通信等。问题是我不知道如何获取在内部设置“IsLoggedIn”属性时要更新的 UI。

toggleLogin: function () {
  // First Case:
  // Works great, makes sense to me.
  //this.$dtAuth.IsLoggedIn = !this.$dtAuth.IsLoggedIn;


  // Second Case:
  // This updates the state of the class just fine, but the UI doesn't get updated.
  // What to do?
  if (!this.$dtAuth.IsLoggedIn) {
    this.$dtAuth.Login();
  } else {
    this.$dtAuth.Logout();
  }

  // This seems like a hack....
  //this.$forceUpdate();
}

我最好的猜测是“reactive()”期望属性在外部设置,并且根本不知道它们何时在内部设置,或者我错过了一些明显的东西。处理此类情况的最佳方法是什么?

对于那些感兴趣的人,这就是插件的设置方式:

export default class dtAuth {

  private authState = new dtAuthHandler();
  install = (app: App) => {
    app.config.globalProperties.$dtAuth = reactive(this.authState);
  }
}

但是,应该知道,如果反应式实例是类组件中 data 的成员,我会得到相同的行为,例如

data() : return { auth: reactive(authState) };

I am new to Vue and I am currently using Vue3. As a learning exercise, I am making a simple login plugin and it is going well. I am using reactive() with an instance of the class below, but I am having a little bit of trouble with getting property changes to update if they are assigned internally. Here is the code in question:

class dtAuthHandler {
  private _IsLoggedIn: boolean = false;
  public get IsLoggedIn(): boolean {
    return this._IsLoggedIn;
  }
  public set IsLoggedIn(value:boolean)
  {
    this._IsLoggedIn = value;
  }

  Login = () => {
    // More stuff here, internal state, etc.
    this.IsLoggedIn = true;
  }

  Logout = () => {
    // More stuff here, internal state, etc.
    this.IsLoggedIn = false;
  }
}

Here is a simple function that I am using to demonstrate the toggling of the login status. In the first case, I can set the property directly and the UI updates as expected, so great. In the second case, I would like to use specific Login()/Logout() functions so that the class can maintain some kind of internal state, talk to servers, etc. The problem is that I can't figure out how to get the UI to update when the 'IsLoggedIn' property is set internally.

toggleLogin: function () {
  // First Case:
  // Works great, makes sense to me.
  //this.$dtAuth.IsLoggedIn = !this.$dtAuth.IsLoggedIn;


  // Second Case:
  // This updates the state of the class just fine, but the UI doesn't get updated.
  // What to do?
  if (!this.$dtAuth.IsLoggedIn) {
    this.$dtAuth.Login();
  } else {
    this.$dtAuth.Logout();
  }

  // This seems like a hack....
  //this.$forceUpdate();
}

My best guess is that 'reactive()' expects the properties to be set externally, and simply isn't aware when they are set internally, that or I am missing something obvious. What is the best way to handle these types of situations?

For those that are interested, this is how the plugin is setup:

export default class dtAuth {

  private authState = new dtAuthHandler();
  install = (app: App) => {
    app.config.globalProperties.$dtAuth = reactive(this.authState);
  }
}

However, it should be known that I get the same behaviour if the reactive instance is a member of data in a class component, e.g.

data() : return { auth: reactive(authState) };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文