本文介绍了使动态组件适合:边距/填充变化:更好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望警报显示在我的静态引导程序v4导航栏中/之内.

所以我有这个简单的服务:

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

@Injectable()
export class AppService {
  private _navbarPadding: number = 50;
  navbarPaddingChange: Subject<number> = new Subject();

  constructor() {
    this.navbarPadding = this._navbarPadding;
  }

  get navbarPadding(): number {
    return this._navbarPadding;
  }

  set navbarPadding(val: number) {
    this._navbarPadding = val;
    this.navbarPaddingChange.next(this._navbarPadding);
  }
}

我在哪里都注入了内容,包括侧边栏(在下面)和主体":

import { Component, OnInit, OnDestroy } from '@angular/core';
import { AppService } from '../app.service';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent implements OnInit, OnDestroy {
  navbarPadding: number;
  subNavbarPadding: Subscription;

  constructor(public appService: AppService) {}

  ngOnInit() {
    this.navbarPadding = this.appService.navbarPadding;
    this.subNavbarPadding = this.appService.navbarPaddingChange.subscribe(val =>
      this.navbarPadding = val
    );
  }

  ngOnDestroy() {
    this.subNavbarPadding.unsubscribe();
  }
}

然后我有了这个功能:

addAlert() {
    this.appService.navbarPadding += 81;
    this.alertsService.alerts.push({
      type: 'info',
      msg: 'INFO'
    })
}

侧边栏html(第一行):

<div class="col-sm-3 col-md-2 sidebar" [style.margin-top.px]=navbarPadding>

...而且效果很好.但是...这肯定是一个糟糕的主意.它到处都有很重的耦合.正确的Angular2方法是什么?

解决方案

通常,这是一种有效的方法,但是,我建议保持控制器尽可能简单,并在工作时使用async -pipe与Observables:

您的服务(使用BehaviorSubject而不是Subject,尽管imho的getter和setter也可以使用,然后在addAlert中您可以只使用this.appService.navbarPadding$.next(131)):

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

@Injectable()
export class AppService {
  private _navbarPadding: number = 50;
  navbarPadding$: BehaviorSubject<number> = new BehaviorSubject(this._navbarPadding);

  constructor() {
  }

  get navbarPadding(): number {
    return this._navbarPadding;
  }

  set navbarPadding(val: number) {
    this._navbarPadding = val;
    this.navbarPadding$.next(this._navbarPadding);
  }
}

该组件的数量最少(手动订阅):

import { Component, OnInit, OnDestroy } from '@angular/core';
import { AppService } from '../app.service';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent implements OnInit, OnDestroy {
  constructor(public appService: AppService) {}
}

您的模板(异步管道会自动处理订阅):

<div class="col-sm-3 col-md-2 sidebar" [style.margin-top.px]="appService.navbarPadding$ | async">

由于您的方法类似于 ngrx ,因此您可能需要查看 ngrx-store ,它为您提供了一种很好的方式来处理这些应用程序状态,例如填充./p>

I want alerts to appear within/above my static bootstrap v4 navbar.

So I've got this simple service:

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

@Injectable()
export class AppService {
  private _navbarPadding: number = 50;
  navbarPaddingChange: Subject<number> = new Subject();

  constructor() {
    this.navbarPadding = this._navbarPadding;
  }

  get navbarPadding(): number {
    return this._navbarPadding;
  }

  set navbarPadding(val: number) {
    this._navbarPadding = val;
    this.navbarPaddingChange.next(this._navbarPadding);
  }
}

Which I inject everywhere, including sidebar (below) and 'main body':

import { Component, OnInit, OnDestroy } from '@angular/core';
import { AppService } from '../app.service';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent implements OnInit, OnDestroy {
  navbarPadding: number;
  subNavbarPadding: Subscription;

  constructor(public appService: AppService) {}

  ngOnInit() {
    this.navbarPadding = this.appService.navbarPadding;
    this.subNavbarPadding = this.appService.navbarPaddingChange.subscribe(val =>
      this.navbarPadding = val
    );
  }

  ngOnDestroy() {
    this.subNavbarPadding.unsubscribe();
  }
}

Then I've got this function:

addAlert() {
    this.appService.navbarPadding += 81;
    this.alertsService.alerts.push({
      type: 'info',
      msg: 'INFO'
    })
}

Sidebar html (first line):

<div class="col-sm-3 col-md-2 sidebar" [style.margin-top.px]=navbarPadding>

…and it works just fine. But… this must be a terrible idea. It has heavy coupling everywhere. What's the correct Angular2 approach?

解决方案

In general, this is a valid approach, however, I would recommend keeping the controller as simple as possible and making use of the async-pipe when working with Observables:

Your Service (uses BehaviorSubject instead a a Subject, though imho the getter and setter could probably go as well, and in the addAlert you then could just use this.appService.navbarPadding$.next(131)):

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

@Injectable()
export class AppService {
  private _navbarPadding: number = 50;
  navbarPadding$: BehaviorSubject<number> = new BehaviorSubject(this._navbarPadding);

  constructor() {
  }

  get navbarPadding(): number {
    return this._navbarPadding;
  }

  set navbarPadding(val: number) {
    this._navbarPadding = val;
    this.navbarPadding$.next(this._navbarPadding);
  }
}

The component is kept to a bare minimum (no manual subscriptions):

import { Component, OnInit, OnDestroy } from '@angular/core';
import { AppService } from '../app.service';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent implements OnInit, OnDestroy {
  constructor(public appService: AppService) {}
}

Your template (the async-pipe automatically handles the subscription):

<div class="col-sm-3 col-md-2 sidebar" [style.margin-top.px]="appService.navbarPadding$ | async">

Since your approach is similar to ngrx you might want to check out the ngrx-store, which provides you with a very nice way of handling exactly those kind of application-states, like your padding.

这篇关于使动态组件适合:边距/填充变化:更好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:24