Ionic 5 Cordova插件EmailComposer NullInjectorError [英] Ionic 5 Cordova Plugin EmailComposer NullInjectorError
问题描述
我正在使用Ionic 5和电容器做一个应用程序.
I'm doing an App using Ionic 5 and Capacitor.
package.json
package.json
"dependencies": {
"@angular/common": "~9.1.6",
"@angular/core": "~9.1.6",
"@angular/fire": "^6.0.2",
"@angular/forms": "~9.1.6",
"@angular/platform-browser": "~9.1.6",
"@angular/platform-browser-dynamic": "~9.1.6",
"@angular/router": "~9.1.6",
"@capacitor/android": "^2.4.0",
"@capacitor/core": "^2.4.0",
"@capacitor/ios": "^2.4.0",
"@ionic-native/core": "^5.27.0",
"@ionic-native/email-composer": "^5.28.0",
"@ionic/angular": "^5.2.3",
"cordova-plugin-email-composer": "^0.9.2",
"rxjs": "~6.5.1",
"tslib": "^1.10.0",
"zone.js": "~0.10.2"
}
我已经安装了插件电子邮件编辑器以打开用户的电子邮件客户端发送电子邮件.
I have installed the plugin Email Composer to open the email client of the user to send an email.
使用电容器安装
npm install cordova-plugin-email-composer
npm install @ionic-native/email-composer
ionic cap sync
我创建了一个服务来实现发送电子邮件的逻辑:
I have created a service to implement the logic of sending emails:
import {Injectable} from '@angular/core';
import {EmailComposer, EmailComposerOptions} from "@ionic-native/email-composer/ngx";
@Injectable()
export class EmailService {
constructor(
private emailComposer: EmailComposer
) {
}
private get isEmailClientConfigured(): Promise<boolean> {
return this.emailComposer.isAvailable();
}
/**
* Tries to open an email client and populate the fields.
*/
intentToSend(to: string) {
if (this.isEmailClientConfigured) {
const email: EmailComposerOptions = {
to: to,
isHtml: false
};
this.emailComposer.open(email);
} else {
console.log('Could not find an email configured.');
}
}
}
控制台错误:
ERROR错误:未捕获(承诺):NullInjectorError:R3InjectorError(ContentModule)[EmailService->EmailComposer->EmailComposer->EmailComposer->EmailComposer]:
NullInjectorError:没有EmailComposer的提供程序!NullInjectorError:R3InjectorError(ContentModule)[EmailService->EmailComposer->EmailComposer->EmailComposer->EmailComposer]:
NullInjectorError:没有EmailComposer的提供程序!
ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(ContentModule)[EmailService -> EmailComposer -> EmailComposer -> EmailComposer -> EmailComposer]:
NullInjectorError: No provider for EmailComposer! NullInjectorError: R3InjectorError(ContentModule)[EmailService -> EmailComposer -> EmailComposer -> EmailComposer -> EmailComposer]:
NullInjectorError: No provider for EmailComposer!
看到此信息后,我将其添加到了ngModule中:
After seeing this, I have added it on my ngModule:
import {NgModule} from '@angular/core';
import {EmailComposer} from "@ionic-native/email-composer";
@NgModule({
declarations: [
],
imports: [
],
providers: [
EmailComposer
]
})
export class SharedModule {
}
现在我在控制台中收到此错误:
Now I receive this error in console:
core.js:6228错误错误:未捕获(承诺中):错误:无效NgModule'SharedModule'的提供程序-仅提供程序的实例和Type被允许,得到:[...,...,...,...,...,?[object对象]?]错误:NgModule'SharedModule'的提供程序无效-仅允许Provider和Type的实例,得到:[...,...,...,...,...,?[object Object]?]
core.js:6228 ERROR Error: Uncaught (in promise): Error: Invalid provider for the NgModule 'SharedModule' - only instances of Provider and Type are allowed, got: [..., ..., ..., ..., ..., ?[object Object]?] Error: Invalid provider for the NgModule 'SharedModule' - only instances of Provider and Type are allowed, got: [..., ..., ..., ..., ..., ?[object Object]?]
任何想法我该如何解决?
Any idea how can I fix this?
我的目标是在我的项目中正确安装和实现插件EmailComposer.
My goal is to install and implement the plugin EmailComposer correctly on my project.
推荐答案
要添加到上述Praveen Patel的答案中,请尝试从更改导入代码
To add onto the answer of Praveen Patel above, try changing the import code from
import {EmailComposer} from "@ionic-native/email-composer";
对此:(请注意'/ngx')
import {EmailComposer} from "@ionic-native/email-composer/ngx";
这篇关于Ionic 5 Cordova插件EmailComposer NullInjectorError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!