Angular-示波器中的变量在订阅后未接收信息

发布于 2025-02-02 14:23:05 字数 1342 浏览 0 评论 0原文

我有这件代码从我们自己的服务中订阅:

method getVideo()

  getVideo() {
    this.watchService.getVideoByVideoString(this.videoId).subscribe(
      (Response) => {
        this.videoData = Response;
        console.log(Response);
      },
      (err) => console.log(err)
    );
  }

ngoninit方法

  ngOnInit() {

    this.getVideo();
    console.log(this.videoData);
}

但在 this.videodata 时变量我在上面安装了它,它返回为未定义的oo,

我知道这是一个愚蠢的错误,但是有人可以帮我吗?

谢谢

======

更新了

最终

,进行了一些测试,发现我可以操纵功能内部的数据,而不是在ngoninit Final 上操纵它回答变得这样:

  getVideo() {
    this.watchService.getVideoByVideoString(this.videoId).subscribe(
      (Response) => {
        this.videoData = Response;

        if (this.videoData.title == null) {
          this.titleService.setTitle('InternalVideoChannel');
        } else {
          this.titleService.setTitle('InternalVideoChannel - ' + this.videoData.title);
        }
        if (this.videoData.id == 0) {
          console.log('Video doesnt exist on the database');
          this.router.navigate(['/notFound']);
        }
        console.log(Response);
      },
      (err) => console.log(err)
    );
  }

谢谢!

I have this piece of code that's subscribing from our own service:

Method getVideo()

  getVideo() {
    this.watchService.getVideoByVideoString(this.videoId).subscribe(
      (Response) => {
        this.videoData = Response;
        console.log(Response);
      },
      (err) => console.log(err)
    );
  }

ngOnInit Method

  ngOnInit() {

    this.getVideo();
    console.log(this.videoData);
}

But the variable in case, this.videoData when I console.log on it, it returns as undefined Oo

I know it would be a silly mistake, but can someone help me?

Thanks

======

Updated

In the end, did some tests and discovered that I could manipulate the data inside of the function, instead of manipulating it on the NgOnInit

final answer it became like this:

  getVideo() {
    this.watchService.getVideoByVideoString(this.videoId).subscribe(
      (Response) => {
        this.videoData = Response;

        if (this.videoData.title == null) {
          this.titleService.setTitle('InternalVideoChannel');
        } else {
          this.titleService.setTitle('InternalVideoChannel - ' + this.videoData.title);
        }
        if (this.videoData.id == 0) {
          console.log('Video doesnt exist on the database');
          this.router.navigate(['/notFound']);
        }
        console.log(Response);
      },
      (err) => console.log(err)
    );
  }

Thanks !

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞清仓 2025-02-09 14:23:05

正确的语法是

 getVideo() {
this.watchService.getVideoByVideoString(this.videoId).subscribe(
  (Response) => {
      this.videoData = Response;
      console.log(Response);
      },
  (err) => console.log(err);
);

}

The correct syntax is

 getVideo() {
this.watchService.getVideoByVideoString(this.videoId).subscribe(
  (Response) => {
      this.videoData = Response;
      console.log(Response);
      },
  (err) => console.log(err);
);

}

月牙弯弯 2025-02-09 14:23:05

好吧,我认为我需要更多的上下文,情况是,如果您在订阅发生之前具有控制台日志,并且将值分配给变量,则始终将不确定为this.videodata的值。

如果您只想查看是否将值分配给属性。

注意:请注意,由于不从可观察到的不订阅而引起的任何内存泄漏。

编辑:
正如我们可以看到控制台的

ngOnInit():void {
   console.log(this.videoData) <- this will be undefined
   this.getVideo(); // <- async 
   console.log(this.videoData) <- this will be undefined
}

那样这样:

getVideo() {
this.watchService.getVideoByVideoString(this.videoId).subscribe(
  (Response) => {
    this.videoData = Response;
    console.log(Response);
    console.log(this.videoData); // your console.log 
  },
  (err) => console.log(err)
);

问题是可观察的,可观察的行为的实际行为,就像承诺一样,因此,一旦您运行了代码,请在实际分配发生之前运行console.log,如果您想在“存储值”上查看“ this.videodata“属性,您可能具有函数并将该功能绑定到单击事件或类似事件的功能,或者只是在订阅中添加Console.log。

第三次编辑。
好吧,如果您想访问该值,那么我不知道的解决方案;如果您想在应用程序中特定时间点上访问该值。

方法是创建一个函数,您每次要查看该值,也许是超时,也许是在单击事件中取决于您的函数。

logVideoDataValue():void {
   console.log(this.videoData) 
}

Well i think i would need a little bit of more context, the situation is that if you have console log before the subscription happens and the value gets assigned to the variable you'll always get undefined as the value for this.videoData.

if you only want to see if the value was assigned to the property this.videoData of you class the console.log has to be along side the assignation on the subscription.

note: be aware of any memory leaks caused by not unsubscribing from a observable.

edit:
as we you can see the console.log is out of the subscription scope, this means that you will be getting undefined, an observable subscription is by nature async

ngOnInit():void {
   console.log(this.videoData) <- this will be undefined
   this.getVideo(); // <- async 
   console.log(this.videoData) <- this will be undefined
}

the only way you can get the value is to console.log inside of the subscription hook like this:

getVideo() {
this.watchService.getVideoByVideoString(this.videoId).subscribe(
  (Response) => {
    this.videoData = Response;
    console.log(Response);
    console.log(this.videoData); // your console.log 
  },
  (err) => console.log(err)
);

The issue is the actual behavior of a observable, and observable behaves like a promise, so once you run the code the console.log is gonna run before the actual assignation happens, if you want to see the stored value on the "this.videoData" property you may have a function and bind that function to a click event or something like that or just add the console.log inside of the subscription.

Third edit.
Well if you want to access the value there isn't a solution that i know of but; if you want to access the value on a specific point of time in your application.

the approach would be to create a function that you would call manually each time you want to see the value, maybe a timeout, maybe on a click event that's up to you.

logVideoDataValue():void {
   console.log(this.videoData) 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文