Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 控制台抛出“;错误类型错误:this.cipherTextService.storeCipher不是函数; 一些背景_Angular_Service_Components - Fatal编程技术网

Angular 控制台抛出“;错误类型错误:this.cipherTextService.storeCipher不是函数; 一些背景

Angular 控制台抛出“;错误类型错误:this.cipherTextService.storeCipher不是函数; 一些背景,angular,service,components,Angular,Service,Components,我正在开发一个密码解码应用程序。我目前正处于项目的最开始阶段,对于Angular来说,我是一个比较新的人。我只是想检查是否可以创建服务并将其注入组件,以及调用我的storeCipher(cipher)方法 问题 当调用我的logCipherText(cipher)方法时,下面的错误消息将在我的控制台中输出。我正在运行最新版本的Chrome 错误消息 错误类型错误:this.cipherTextService.storeCipher不是函数 我并没有尝试过太多不同的东西,因为我非常确定我的代码与A

我正在开发一个密码解码应用程序。我目前正处于项目的最开始阶段,对于Angular来说,我是一个比较新的人。我只是想检查是否可以创建
服务
并将其注入
组件
,以及调用我的
storeCipher(cipher)
方法

问题 当调用我的
logCipherText(cipher)
方法时,下面的错误消息将在我的控制台中输出。我正在运行最新版本的Chrome

错误消息
错误类型错误:this.cipherTextService.storeCipher不是函数

我并没有尝试过太多不同的东西,因为我非常确定我的代码与Angular自己的示例非常接近。但是,我确实记录了注入的服务实例的类型,这给了我:
服务类型:[对象]
所以它肯定不是未定义的

在我的服务中,我也对我的密码字符串执行了相同的操作,并将其记录在日志中:
服务密码变量类型:未定义

密码文本.service.ts
从'@angular/core'导入{Injectable};
@注射的({
providedIn:'根'
})
导出类密文服务{
密码=“”;
//问题在哪里!!!
存储密码(密码){
this.cipher=密码;
}
getCipher(){
返回此.cipher;
}
clearCipher(){
this.cipher='';
}
}
密码输入.component.ts注入
CipherTextService
从'@angular/core'导入{Component,OnInit};
从“./ciphertext.service”导入{CipherTextService};
@组成部分({
选择器:“应用密码输入”,
templateUrl:'./cipher input.component.html',
styleUrls:['./cipher input.component.css']
})
导出类CipherInputComponent实现OnInit{
构造函数(私有cipherTextService:cipherTextService){
}
恩戈尼尼特(){
}
//问题在哪里!!!
对数密文(密码){
log(“服务类型:“+this.cipherTextService”);
log(“服务密码变量类型:“+this.cipherTextService.cipher”);
this.cipherTextService.storeCipher(cipher);
log(“存储密码:+this.cipherTextService.getCipher());
}
}
cipher input.component.html调用
logCipherText()

我所期望的 我希望得到
this.cipherTextService.cipher
类型的
String
,然后我希望我的
storeCipher()
方法将输入文本框中的值存储到
this.cipherTextService.cipher
中。我最后的日志应该打印出来
stored cipher:“无论键入什么”

您需要公开该函数

  public storeCipher(cipher: String) {
    this.cipher = cipher;
  }

我试着应用这个解决方案,但是我仍然遇到同样的问题。