在Angular 2中传递两个组件(页面)之间的值

Hon*_*iao 9 typescript angular2-routing angular

我正在使用Angular 2(TypeScript)

我有两个组件(实际上它们也是两个页面).他们不是父母和孩子的关系.

当我单击第一个组件(页面)中的链接时,我想将值传递给第二个组件(页面).

我通过路由器知道"URL参数"的方式.但我不想在链接中显示此参数.还有其他方法吗?

谢谢!

Mar*_*yer 15

规范方法是建立injectable服务并将服务注入到需要数据的任何组件中.由于服务是单例,因此组件将访问相同的数据.它们在不同的页面上并不重要.这里的人物.

编辑:这是一个更为复杂的示例,通过订阅服务上的eventEmitter来显示组件之间的双向通信:Plunker

import {Component, Injectable} from 'angular2/core'

// your shared service 
// provide reference in parent component or bootstrap 
@Injectable()
export class myService {
  sharedData:string = "String from myService"
}

@Component({
  selector: 'page1',
  providers: [],
  template: `
    <div>
      <b>Component Page1 - shared data:</b> {{SharedData}}
    </div>
  `,
  directives: [],
})
export class Page1 {
  constructor(aService: myService) {
    this.SharedData = aService.sharedData
  }
}

@Component({
  selector: 'page2',
  providers: [],
  template: `
    <div>
      <b>Component Page2 - shared data:</b> {{SharedData}}
    </div>
  `,
  directives: [],
})
export class Page2 {
  constructor(aService: myService) {
    this.SharedData = aService.sharedData
  }
}
Run Code Online (Sandbox Code Playgroud)

  • @HongboMiao,上面的代码不起作用.由于Page1和Page2都"提供"服务myService,因此每个服务都获得自己的服务实例.(MarkM,服务不一定是Angular 2中的单例 - 它取决于你提供它们的位置.)但是Plunker是正确的,因为myService是在bootstrap/root组件级别提供的,因此Page1和Page2获得相同的实例. (2认同)

gta*_*ero 7

谢谢MarkM!它有效,但对我来说很难,因为我把它全部放在不同的文件中,而且使用了角度cli的最后一个angular2 ...所以我在这里解释一下:

首先:您需要创建一个文件,其中包含您要与其他组件共享的类:data.service.ts并将其设为"@Injectable":

import { Injectable } from '@angular/core';

@Injectable()
export class myService {
  public sharedData:string;

  constructor(){
    this.sharedData = "String from myService";
  }

  setData (data) {
    this.sharedData = data;
  }
  getData () {
    return this.sharedData;
  }
}
Run Code Online (Sandbox Code Playgroud)

确保在app.module.ts文件中仅在'providers'上设置创建的类(myService)并导入可注入文件:data.service.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { routing, appRoutingProviders } from './app.routing';
import { AppComponent } from './app.component';
import { AppContent } from './components/content.component';
import { AppInsertContent } from './components/insert.component';
import { myService } from './services/data.service';

@NgModule({
  declarations: [
    AppComponent,
    AppContent,
    AppInsertContent,
  ],
  imports: [
    BrowserModule,
    routing
  ],
  providers: [appRoutingProviders, myService],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

无论你想在哪里使用你的共享类,都要使用构造函数从类中创建一个对象(由于@Injectable装饰器,这个对象对你的组件来说是唯一的).记得在你使用它的每个文件上导入它!...

在我的例子中,此视图使用文本输入设置对象的数据:input.component.ts

import { Component } from '@angular/core';
import { myService } from '../services/data.service';

@Component({
  selector: 'insert-content',
  template: `
   <input id="textbox" #textbox type="text">
   <button (click)="this._myService.setData(textbox.value); SharedData = textbox.value;">SAVE</button>
   Object data: {{SharedData}}

    <div class="full-button">
      <a routerLink="/" routerLinkActive="active">BACK</a>
    </div>
  `
})

export class AppInsertContent {
  SharedData: string;
  constructor (private _myService: myService){
    console.log(this._myService.getData());
  }
}
Run Code Online (Sandbox Code Playgroud)

在这个视图中,我只看到对象上设置的数据:content.component.ts

import { Component } from '@angular/core';
import { myService } from '../services/data.service';

@Component({
  selector: 'app-content',
  template: `
  <div style="float:left; clear:both;"> 
    {{_myService.getData()}} 
  </div>

  <div class="full-button">
    <a routerLink="/insert" routerLinkActive="active">INSERT</a>
  </div>
  `
})
export class AppContent {
  constructor (private _myService: myService){
    console.log(this._myService.getData());
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望这些信息有用!

  • 伙计,你救了我的命!只是说:"确保在app.module.ts上只在'providers'上设置创建的类(myService)".非常感谢,我无法将我的数据保存到服务中,因为我把它放在每个组件上而不是只在app.module.ts中! (2认同)