What's the best way to inject one service into another in angular 2 (Beta)?

前端 未结 7 1369
庸人自扰
庸人自扰 2020-11-22 05:25

I know how to inject a service into a component (via @Component), but how can I use DI to pass around services outside of components?

In other words, I don\'t want t

7条回答
  •  失恋的感觉
    2020-11-22 05:48

    Service is considered to be shared among components. So let's say if I have one service, I can use it in different components.

    Here In this answer I'm showing you one service which accepts data from one component and sends that data to other component.

    I have used concept of Routing, Shared-Service, Shared-Object. I hope this will help you to understand the basic of share-service.

    Note: @Injectable decorater is used to make service injectable.

    Answer

    Boot.ts

    import {Component,bind} from 'angular2/core';
    
    import {bootstrap} from 'angular2/platform/browser';
    
    import {Router,ROUTER_PROVIDERS,RouteConfig, ROUTER_DIRECTIVES,APP_BASE_HREF,LocationStrategy,RouteParams,ROUTER_BINDINGS} from 'angular2/router';
    
    import {SharedService} from 'src/sharedService';
    
    import {ComponentFirst} from 'src/cone';
    import {ComponentTwo} from 'src/ctwo';
    
    
    @Component({
      selector: 'my-app',
      directives: [ROUTER_DIRECTIVES],
      template: `
        

    Home

    `, }) @RouteConfig([ {path:'/component-first', name: 'ComponentFirst', component: ComponentFirst} {path:'/component-two', name: 'ComponentTwo', component: ComponentTwo} ]) export class AppComponent implements OnInit { constructor(router:Router) { this.router=router; } ngOnInit() { console.log('ngOnInit'); this.router.navigate(['/ComponentFirst']); } } bootstrap(AppComponent, [SharedService, ROUTER_PROVIDERS,bind(APP_BASE_HREF).toValue(location.pathname) ]);

    FirstComponent

    import {Component,View,bind} from 'angular2/core';
    import {SharedService} from 'src/sharedService';
    import {Router,ROUTER_PROVIDERS,RouteConfig, ROUTER_DIRECTIVES,APP_BASE_HREF,LocationStrategy,RouteParams,ROUTER_BINDINGS} from 'angular2/router';
    @Component({
      //selector: 'f',
      template: `
        
    `, }) export class ComponentFirst { constructor(service:SharedService,router:Router){ this.service=service; this.router=router; } send(str){ console.log(str); this.service.saveData(str); console.log('str'); this.router.navigate(['/ComponentTwo']); } }

    SecondComponent

    import {Component,View,bind} from 'angular2/core';
    import {SharedService} from 'src/sharedService';
    import {Router,ROUTER_PROVIDERS,RouteConfig, ROUTER_DIRECTIVES,APP_BASE_HREF,LocationStrategy,RouteParams,ROUTER_BINDINGS} from 'angular2/router';
    @Component({
      //selector: 'f',
      template: `
        

    {{myName}}

    SharedService and shared Object

    import {Component, Injectable,Input,Output,EventEmitter} from 'angular2/core'
    
    // Name Service
    export interface myData {
       name:string;
    }
    
    
    
    @Injectable()
    export class SharedService {
      sharingData: myData={name:"nyks"};
      saveData(str){
        console.log('save data function called' + str + this.sharingData.name);
        this.sharingData.name=str; 
      }
      getData:string()
      {
        console.log('get data function called');
        return this.sharingData.name;
      }
    } 
    

提交回复
热议问题