简体   繁体   English

Angular5:将变量从一个组件传递到另一个打字稿文件

[英]Angular5: Passing Variable From One Component to Another Typescript File

I am trying to pass the value I am storing in one of my components to another component so that my new component can use the selected value from my original. 我试图将我存储在一个组件中的值传递给另一个组件,以便我的新组件可以使用从原始组件中选择的值。 Right now I have a file as follows: 现在我有一个文件,如下所示:

symbol-picker.component.ts 符号picker.component.ts

 import {Component, Input, OnInit} from '@angular/core'; @Component({ selector: 'app-symbol-picker', templateUrl: './symbol-picker.component.html', styleUrls: ['./symbol-picker.component.scss'] }) export class SymbolPickerComponent implements OnInit { selectionChoice: string; equalsTxt = '='; impliesTxt = '=>'; constructor() { } ngOnInit() { } } 

Where I am setting the value of 'selectionChoice' in my html as follows: 我在html中按如下所示设置“ selectionChoice”的值的地方:

symbol-picker.component.html 符号picker.component.html

 <button (click)="selectionChoice = 'equals'">{{equalsTxt}}</button> <button (click)="selectionChoice = 'implies'">{{impliesTxt}}</button> 

I want to pass the value held in 'selectionChoice' to a new file to use it. 我想将“ selectionChoice”中保存的值传递给新文件以使用它。 For instance I am trying to get the value right now in this file: 例如,我正在尝试立即在此文件中获取值:

symbolPicker.ts symbolPicker.ts

 import {SymbolPickerComponent} from '../symbol-picker/symbol-picker.component'; export interface Config { container: string; selector: 'equals'|'implies'|'followsFrom'; } export interface QuillInstance { on: Function; getText: Function; } export default class SymbolPicker { symbolSelected = SymbolPickerComponent.selectionChoice; quill: QuillInstance; options: Config; constructor(quill, options) { this.quill = quill; this.options = options; const container = document.querySelector(this.options.container); switch (this.options.selector) { case 'equals': { container.addEventListener('click', function() { console.log('FRANK: EQUALS PRESSED'); quill.insertText(quill.getSelection(), '\\n= 〈 〉'); }); break; } case 'implies': { container.addEventListener('click', function() { console.log('FRANK: IMPLIES PRESSED'); quill.insertText(quill.getSelection(), '\\n=> 〈 〉'); }); break; } default: { console.log('FRANK: selectionChoice set to non-understood value'); break; } } } } 

How can I set my newly declared 'symbolSelected' variable in symbolPicker.ts to the value of selectionChoice in symbol-picker.component.ts? 如何将symbolPicker.ts中新声明的'symbolSelected'变量设置为symbol-picker.component.ts中的selectionChoice的值? I am ultimate trying to do this so in my editor.components.ts file I can reference this value as well for my 'selector' section in symbolPicker as follows: 我最终试图做到这一点,因此在我的editor.components.ts文件中,我也可以在symbolPicker的“选择器”部分中引用此值,如下所示:

 this.modules = { formula: true, toolbar: true, counter: { container: '#counter', unit: 'word' }, symbolPicker: { container: '#symbolCounter', selector: this.symbolSelected } }; 

The idea would be that this selector value would dynamically change as I press the buttons to change between 'equals' and 'implies'. 想法是,当我按下按钮在“等于”和“隐含”之间切换时,该选择器值将动态变化。

There are many ways to share data between components. 有许多方法可以在组件之间共享数据。 For your particular scenario, your best choice is a service. 对于您的特定情况,最好的选择是服务。 Build a service to retain the value(s) that you want to share. 构建服务以保留您想要共享的价值。

Then inject the service into any component that needs to set the value or read the value. 然后将服务注入到需要设置值或读取值的任何组件中。

I have an example here: https://github.com/DeborahK/MovieHunter-communication/tree/master/MH-Take4 我在这里有一个例子: https : //github.com/DeborahK/MovieHunter-communication/tree/master/MH-Take4

In my case, I share the currently selected movie. 就我而言,我共享当前选择的电影。 Below are some snippets of my code. 以下是我的代码片段。 (See the above URL for the complete code example.) (有关完整的代码示例,请参见上面的URL。)

Movie service: 电影服务:

@Injectable()
export class MovieService {

    currentMovie: IMovie | null;
    // Other code here.
}

Movie List component 电影列表组件

In this component, the user selects a movie: 在此组件中,用户选择电影:

export class MovieListComponent {
    constructor(private movieService: MovieService) { }

    onSelected(movie: IMovie): void {
        this.movieService.currentMovie = movie;
    }
    // Other code here
}

Movie Detail component 电影详细信息组件

In this component, the bindings change automatically when the user selects a different movie in the Movie List component. 在此组件中,当用户在“电影列表”组件中选择其他电影时,绑定会自动更改。

The UI is bound to the movie property defined in the component below. UI绑定到下面组件中定义的movie属性。

export class MovieDetailComponent {

    get movie(): IMovie | null {
        return this.movieService.currentMovie;
    }

    constructor(private movieService: MovieService) {}
}

Angular's change detection tracks when the currentMovie changes in the service and rebinds the values, calling the getter shown above, and getting the current value of the currentMovie . Angular的更改检测将跟踪currentMovie在服务中的更改时间并重新绑定值,调用上面显示的getter,并获取currentMovie的当前值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM