Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.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 如何防止组件更新服务_Angular_Angular2 Services - Fatal编程技术网

Angular 如何防止组件更新服务

Angular 如何防止组件更新服务,angular,angular2-services,Angular,Angular2 Services,我是Angular 4的新手,正在尝试设置一个服务,该服务包含一些数据,这些数据不应该被它提供给的任何组件编辑。例如,以下是我的服务的简化版本: @Component({ providers: [ArgumentService] }) @Injectable() export class ArgumentService { pronouns: Object[]; constructor() { this.pronouns = [{'type': "1SG", 'value'

我是Angular 4的新手,正在尝试设置一个服务,该服务包含一些数据,这些数据不应该被它提供给的任何组件编辑。例如,以下是我的服务的简化版本:

@Component({
  providers: [ArgumentService]
})

@Injectable()
export class ArgumentService {
  pronouns: Object[];
  constructor() {

   this.pronouns = [{'type': "1SG", 'value': 'n', "series": "I"},
                    {'type': "2SG", 'value': 'm', "series": "I"},
                    {'type': "1SG", 'value': "'y", "series": "II"},
                    {'type': "2SG", 'value': 'n', "series": "II"},
                    {'type': "1SG", 'value': "'nii'y", "series": "III"},
                    {'type': "2SG", 'value': "'niin", "series": "III"}];

  }

  getPronouns(){
    return this.pronouns
  }

}
对象数组不应更改。但是,下面组件中的示例函数会更改数组中的第一个对象:

import { Sentence } from '../sentence.model'
import { VerbService, ArgumentService } from '../../services/'
import { RadioButtonComponent } from '../radio-button/radio-button.component'


@Component({
  selector: 'app-sentence',
  templateUrl: './sentence.component.html',
  styleUrls: ['../../bootstrap.min.css', './sentence.component.scss', '../app.component.scss'],
  providers: [VerbService, ArgumentService]
})

export class SentenceComponent implements OnInit {

  constructor(public verbService: VerbService, public argumentService: ArgumentService) {

    this.example()

  }


  example(){
    let arg = this.argumentService.getPronouns()[0]
    arg['position'] = 'S'
    console.log(this.argumentService.getPronouns()) // this gives {'type': "1SG", 'value': 'n', "series": "I", "position": "S"} when it should give {'type': "1SG", 'value': 'n', "series": "I"}
  }

  ngOnInit() {
  }

}
也许我认为这种数据应该放在服务中是错误的?服务是否总是应该从其组件更新?我真的非常感谢任何关于这方面的建议或指导

提前谢谢

getPronouns(){
    return this.pronouns.map(p => Object.assign({}, p))
}

这将不仅返回数组的副本,而且返回其所有内容的副本。(我以前的尝试,this.degens.slice(),复制数组,但用对相同对象的引用填充数组)

发生这种情况是因为对象是可变的,并且通过引用传递。如果要避免这种情况,请使用Observable,在这种情况下,
BehaviorSubject
是合适的。为您服务:

从'rxjs/BehaviorSubject'导入{BehaviorSubject};
// ...
代词r=[…];
//不要使用“任何”,请键入您的数据!:)
私人代词:行为主语
=新的行为主体(this.degenerator);
公共代词$=this.degens.asObservable();
然后,在组件中,您可以订阅此可观察的:

构造函数(
公共verbService:verbService,
公共辩论服务:辩论服务
) {
argumentService.degens$.subscribe(degens=>{
console.log(代词)
//做你喜欢做的事。。。
})
}

您可以使用javascript.Hm的getter函数。我将服务中的函数更改为返回此.degens.slice(),但仍然存在相同的问题。很抱歉,我的错误。将更新答案。。。我想出来了。谢谢,这也行得通,但另一个解决方案是我真正需要的。