'Subscription'类型的参数不能分配给'Function'类型的参数

问题描述 投票:0回答:1

我是Angular的新手,实际上是在尝试通过参数中的管道回调来定义函数。

该函数正在工作,但会引发错误:

错误TS2345:类型'Subscription'的参数不能分配给'功能'类型的参数。类型'Subscription'缺少类型'Function'的以下属性:应用,调用,绑定,原型以及另外5个。

这是我的函数定义:

checkNetworkThenExecute(callback: Function) {
      Network.getStatus().then((status) => {
        if (status.connected) {
          callback();
        } else {
          if (this.networkHandler == null) {
            this.networkHandler = Network.addListener(
              "networkStatusChange",
              (status) => {
                if (status.connected) {
                  this.networkHandler.remove();

                  callback();
                }
              }
            );
          }
          alert("Merci de vous connecter à un réseau.");
        }
      });
    }

    searchWord(query: string) {
      const url = "https://api.mapbox.com/geocoding/v5/mapbox.places/";
      return this.http
        .get(
          url +
            query +
            ".json?types=address&access_token=" +
            environment.mapbox.accessToken
        )
        .pipe(
          map((res: MapboxOutput) => {
            return res.features;
          })
        );
    }

而且我这样称呼这个功能:

this.mapboxService.checkNetworkThenExecute(
          this.mapboxService
            .searchWord(searchTerm)
            .subscribe((features: Feature[]) => {
              this.addresses = features.map((feat) => feat.place_name);
              console.log(this.addresses);
            })
        );

我在做什么错了?

感谢您的帮助。

angular typescript callback subscription
1个回答
0
投票

尝试一下:

this.mapboxService.checkNetworkThenExecute( () => {
      this.mapboxService
        .searchWord(searchTerm)
        .subscribe((features: Feature[]) => {
          this.addresses = features.map((feat) => feat.place_name);
          console.log(this.addresses);
        });
       }
    );

P.S .:别忘了unsubscribe

© www.soinside.com 2019 - 2024. All rights reserved.