如何防止变量值重置?

问题描述 投票:0回答:1

从子组件在表格中滚动时会触发以下

setData
函数,

this.buttonDisabled:boolean = true;

setData(value: boolean) {
  this.name = value;
  this.buttonDisabled = false;
}

但是,如果我返回另一个页面并再次返回旧页面,

this.buttonDisabled
值将重置。如何防止该值被重置?

angular angularjs
1个回答
0
投票

您可以将最后一个值存储在服务中并在需要时获取它,请确保在不再需要时重置该值,以避免出现错误。

ts

import { Component, OnInit } from '@angular/core';
import {
  ActivatedRoute,
  NavigationEnd,
  Router,
  RoutesRecognized,
} from '@angular/router';
import { filter, pairwise } from 'rxjs/operators';
import { DummyService } from '../dummy.service';
import { RouterExtService } from '../router-ext.service';

@Component({
  selector: 'app-student',
  templateUrl: './student.component.html',
  styleUrls: ['./student.component.css'],
})
export class StudentComponent implements OnInit {
  prevUrl: string;
  currentUrl: string;

  setData(value: boolean) {
    this.dummyService.buttonDisabled = value;
  }

  get buttonDisabled() {
    return this.dummyService.buttonDisabled;
  }

  constructor(
    private routerExtService: RouterExtService,
    private dummyService: DummyService
  ) {}
  ngOnInit() {}

  showUrl = () => {
    let previous = this.routerExtService.getPreviousUrl();
    console.log('previous', previous);

    let getCurrentUrl = this.routerExtService.getCurrentUrl();
    console.log('getCurrentUrl', getCurrentUrl);
  };
}

服务

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

@Injectable({
  providedIn: 'root',
})
export class DummyService {
  buttonDisabled: boolean = false;
  constructor() {}
}

堆栈闪电战

© www.soinside.com 2019 - 2024. All rights reserved.