Ionic “new”表达式(其目标缺少construc签名)隐式具有“any”类型

nbysray5  于 2023-06-27  发布在  Ionic
关注(0)|答案(1)|浏览(107)

我有一个文件notifications. service. ts,它请求推送通知的令牌:

import { useEffect } from 'react';
import { PushNotifications, PushNotificationSchema, PushNotificationToken } from '@capacitor/push-notifications';
import { Plugins } from '@capacitor/core';
const { Capacitor } = Plugins;

export function NotificationsService() {
  useEffect(() => {
    initPush();
  }, []);

  function initPush() {
    if (Capacitor.getPlatform() !== 'web') {
      registerPush();
    }
  }

  function registerPush() {
    PushNotifications.requestPermissions().then((permission) => {
      if (permission.receive === 'granted') {
        PushNotifications.register();
      } else {
        // If permission is not granted
      }
    });

    PushNotifications.addListener('registration', (token: PushNotificationToken) => {
      console.log(token.value);
    });

    PushNotifications.addListener('registrationError', (err) => {
      console.log(err);
    });

    PushNotifications.addListener('pushNotificationReceived', (notification: PushNotificationSchema) => {
      console.log(notification);
    });
  }

  return null; // or you can return a JSX component if needed
}

但是在App.tsx中,当我尝试初始化它时,出现了一个错误:

import { NotificationsService } from './services/notification.service'; 
...
useEffect(() => {
    const pushNotifications = new NotificationsService();
    pushNotifications.initPush();
  }, []);

错误:目标缺少构造签名的“new”表达式隐式具有“any”类型。
谢谢你的时间,我很感激你的帮助:)
根据我的理解,关键是这个表达式的类型是any???

export function NotificationsService(this:any) {
  useEffect(() => {
    initPush();
  }, []);
5ssjco0h

5ssjco0h1#

您没有尝试使用new关键字创建NotificationsService函数的示例。但你不应该这么用删除new

变更:

const pushNotifications = new NotificationsService();

收件人:

const pushNotifications = NotificationsService();

相关问题