﻿
{"id":7478,"date":"2018-04-01T12:21:12","date_gmt":"2018-04-01T12:21:12","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=7478"},"modified":"2018-04-01T12:21:12","modified_gmt":"2018-04-01T12:21:12","slug":"angular-4-ile-providers-ile-singleton-instance-yaratmak","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/angular-4-ile-providers-ile-singleton-instance-yaratmak\/","title":{"rendered":"Angular 4 \u0130le Providers \u0130le Singleton \u0130nstance Yaratmak"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Angular 4 uygulamalar\u0131nda yap\u0131n\u0131n genel ya da \u00f6zel olarak kullanaca\u011f\u0131 bir nesneyi providers \u00f6zelli\u011fini kullanarak yaratmam\u0131z ve tutmam\u0131z bizim i\u00e7in performans ve maliyet a\u00e7\u0131s\u0131ndan olduk\u00e7a \u00f6nem arz etmektedir. \u015eimdi bu konuyu \u00f6rnek bir senaryo \u00fczerinden ele alacak, a\u00e7acak ve kullan\u0131m\u0131n\u0131 ve ihtiyac\u0131n\u0131 teorik ve pratik olarak netle\u015ftirece\u011fiz.<\/p>\n<p>\u0130lk olarak &#8220;ornek&#8221; ad\u0131nda bir component olu\u015ftural\u0131m ve &#8220;ornek.component.ts&#8221; dosyas\u0131n\u0131 a\u00e7\u0131p i\u00e7eri\u011fini a\u015fa\u011f\u0131daki gibi \u015fekillendirelim.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { Component, OnInit } from '@angular\/core';\r\nimport { Personel } from '..\/entitys\/personel';\r\n@Component({\r\n  selector: 'app-ornek',\r\n  templateUrl: '.\/ornek.component.html',\r\n  styleUrls: &#x5B;'.\/ornek.component.css']\r\n})\r\nexport class OrnekComponent implements OnInit {\r\n  _personel: Personel;\r\n  constructor(private personel: Personel) { }\r\n  ngOnInit() {\r\n    this.personel.Adi = &quot;Gen\u00e7ay&quot;;\r\n    this.personel.SoyAdi = &quot;Y\u0131ld\u0131z&quot;;\r\n    this._personel = this.personel;\r\n  }\r\n}\r\n<\/pre>\n<p>Dikkat ederseniz e\u011fer, &#8220;ornek&#8221; componentimiz &#8220;Personel&#8221; tipinde bir s\u0131n\u0131f\u0131 dinlemektedir. Bu component \u00e7al\u0131\u015ft\u0131r\u0131laca\u011f\u0131 zaman constructer\u0131nda dinledi\u011fi bu &#8220;Personel&#8221; nesnesi bir yerde olu\u015fturulmu\u015f olmal\u0131d\u0131r.<\/p>\n<p>E\u011fer ki mevcut durumda projeyi kaydedip, yay\u0131nlarsak taray\u0131c\u0131da hi\u00e7bir sonu\u00e7 alamayaca\u011f\u0131z ve hatta sayfaya sa\u011f t\u0131klayarak &#8220;\u0130ncele&#8221; men\u00fcs\u00fcne t\u0131klad\u0131\u011f\u0131n\u0131z zaman \u00e7\u0131kan pencerede &#8220;Console&#8221; kategorisini a\u00e7arsan\u0131z hatay\u0131 net bir \u015fekilde g\u00f6rebileceksiniz.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/04\/Angular-4-\u0130le-Providers-\u0130le-Singleton-\u0130nstance-Yaratmak.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/04\/Angular-4-\u0130le-Providers-\u0130le-Singleton-\u0130nstance-Yaratmak.png\" alt=\"Angular 4 \u0130le Providers \u0130le Singleton \u0130nstance Yaratmak\" width=\"586\" height=\"242\" class=\"aligncenter size-full wp-image-7481\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/04\/Angular-4-\u0130le-Providers-\u0130le-Singleton-\u0130nstance-Yaratmak.png 586w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/04\/Angular-4-\u0130le-Providers-\u0130le-Singleton-\u0130nstance-Yaratmak-300x124.png 300w\" sizes=\"auto, (max-width: 586px) 100vw, 586px\" \/><\/a><\/p>\n<p>Hatada &#8220;Personel&#8221; nesnesinin hi\u00e7bir providerda tan\u0131mlanmad\u0131\u011f\u0131ndan\/bulamad\u0131\u011f\u0131ndan bahsetmektedir.<\/p>\n<p>\u0130\u015fte Angular 4 compiler\u0131 bu durumda iki yere bakmaktad\u0131r.<\/p>\n<p>1. &#8220;app.module.ts&#8221; dosyas\u0131 i\u00e7erisindeki yani mod\u00fcl i\u00e7erisindeki providers \u00f6zelli\u011fine,<br \/>\n2. &#8220;ornek.component.ts&#8221; dosyas\u0131 i\u00e7erisindeki yani component i\u00e7erisindeki providers \u00f6zelli\u011fine.<\/p>\n<p>Bu iki noktadan birinde e\u011fer bu nesne tan\u0131mlanm\u0131\u015f ise &#8220;OrnekComponent&#8221; s\u0131n\u0131f\u0131n\u0131n constructer\u0131ndaki parametre ilgili nesneyi rahat bir \u015fekilde yakalay\u0131p, dinleyebilecektir.<\/p>\n<p>\u00d6rnek &#8220;ornek.component.ts&#8221; i\u00e7eri\u011fi;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { Component, OnInit } from '@angular\/core';\r\nimport { Personel } from '..\/entitys\/personel';\r\n@Component({\r\n  selector: 'app-ornek',\r\n  templateUrl: '.\/ornek.component.html',\r\n  styleUrls: &#x5B;'.\/ornek.component.css'],\r\n  providers: &#x5B;Personel]\r\n})\r\nexport class OrnekComponent implements OnInit {\r\n  _personel: Personel;\r\n  constructor(private personel: Personel) { }\r\n  ngOnInit() {\r\n    this._personel = this.personel;\r\n  }\r\n}\r\n<\/pre>\n<p>\u00d6rnek &#8220;app.module.ts&#8221; i\u00e7eri\u011fi;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { BrowserModule } from '@angular\/platform-browser';\r\nimport { NgModule } from '@angular\/core';\r\nimport { AppComponent } from '.\/app.component';\r\nimport { OrnekComponent } from '.\/ornek\/ornek.component';\r\nimport { Personel } from '.\/entitys\/personel';\r\n@NgModule({\r\n  declarations: &#x5B;\r\n    AppComponent,\r\n    PersonelComponent,\r\n    OrnekComponent\r\n  ],\r\n  imports: &#x5B;\r\n    BrowserModule\r\n  ],\r\n  providers: &#x5B;Personel],\r\n  bootstrap: &#x5B;AppComponent]\r\n})\r\nexport class AppModule { }\r\n<\/pre>\n<p>Bu \u015fekilde bir tan\u0131mlamadan sonra &#8220;ornek&#8221; componentimizi constructer\u0131nda &#8220;Personel&#8221; nesnesi dinlerken rahatl\u0131kla kullanabilmekteyiz.<\/p>\n<p>\u015eimdi as\u0131l sorulmas\u0131 gereken soruya gelelim.<\/p>\n<h5>Nerede Mod\u00fcl Providers&#8217;\u0131, Nerede Component Providers&#8217;\u0131 Kullan\u0131lmal\u0131d\u0131r?<\/h5>\n<p>E\u011fer ki, Singleton instance olarak ayarlanacak nesne t\u00fcm uygulama taraf\u0131ndan kullan\u0131lacaksa o nesneyi &#8220;app.module.ts&#8221; dosyas\u0131ndaki yani mod\u00fcl i\u00e7erisindeki providers \u00f6zelli\u011finden olu\u015fturman\u0131z gerekecektir.<\/p>\n<p>Yok sadece tek bir componente \u00f6zel bir singleton instance olacaksa ilgili componentin providers \u00f6zelli\u011finde olu\u015fturulmas\u0131 daha do\u011fru olacakt\u0131r.<\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Angular 4 uygulamalar\u0131nda yap\u0131n\u0131n genel ya da \u00f6zel olarak kullanaca\u011f\u0131 bir nesneyi providers \u00f6zelli\u011fini kullanarak yaratmam\u0131z ve tutmam\u0131z bizim i\u00e7in performans ve maliyet a\u00e7\u0131s\u0131ndan olduk\u00e7a \u00f6nem arz etmektedir. \u015eimdi bu konuyu \u00f6rnek bir&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":7246,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2144],"tags":[2215,2217,2216,2218],"class_list":["post-7478","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular-4","tag-angular-4-providers","tag-angular-4-providers-singleton-instance-yaratmak","tag-angular-4-singleton-instance","tag-angular-4-singleton-providers"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/7478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=7478"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/7478\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/7246"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=7478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=7478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=7478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}