'Subscription' 类型的参数不能分配给 'Function' 类型的参数
Argument of type 'Subscription' is not assignable to parameter of type 'Function'
我是 Angular 的新手,实际上我正在尝试在参数中定义一个带有管道回调的函数。
该函数正在运行但抛出错误:
error TS2345: Argument of type 'Subscription' is not assignable to parameter of type 'Function'.
Type 'Subscription' is missing the following properties from type 'Function': apply, call, bind, prototype, and 5 more.
这是我的函数定义:
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);
})
);
请问我做错了什么?
感谢您的帮助。
试试这个:
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
我是 Angular 的新手,实际上我正在尝试在参数中定义一个带有管道回调的函数。
该函数正在运行但抛出错误:
error TS2345: Argument of type 'Subscription' is not assignable to parameter of type 'Function'. Type 'Subscription' is missing the following properties from type 'Function': apply, call, bind, prototype, and 5 more.
这是我的函数定义:
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);
})
);
请问我做错了什么?
感谢您的帮助。
试试这个:
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