首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Angular 7.如何从另一个页面(dashboard.page.ts-in.page.ts)控制页面上的变量

在Angular 7中,可以通过使用服务(service)来实现从另一个页面控制页面上的变量。以下是一种常见的实现方式:

  1. 创建一个共享服务(shared service),用于在不同的组件之间共享数据和状态。可以使用Angular的@Injectable装饰器来定义该服务。
代码语言:txt
复制
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class SharedService {
  public variable: any;

  constructor() { }
}
  1. 在dashboard.page.ts(或其他需要控制变量的页面)中,注入共享服务,并在需要的地方修改变量的值。
代码语言:txt
复制
import { Component } from '@angular/core';
import { SharedService } from 'path/to/shared.service';

@Component({
  selector: 'app-dashboard',
  templateUrl: 'dashboard.page.html',
  styleUrls: ['dashboard.page.scss'],
})
export class DashboardPage {
  constructor(private sharedService: SharedService) {}

  updateVariable() {
    this.sharedService.variable = 'New value';
  }
}
  1. 在in.page.ts(或其他需要访问变量的页面)中,同样注入共享服务,并通过访问共享服务的变量来获取或修改变量的值。
代码语言:txt
复制
import { Component } from '@angular/core';
import { SharedService } from 'path/to/shared.service';

@Component({
  selector: 'app-in',
  templateUrl: 'in.page.html',
  styleUrls: ['in.page.scss'],
})
export class InPage {
  constructor(private sharedService: SharedService) {}

  getVariable() {
    console.log(this.sharedService.variable);
  }
}

通过以上步骤,你可以在dashboard.page.ts中通过修改共享服务的变量来控制页面上的变量,并在in.page.ts中通过访问共享服务的变量来获取或修改该变量的值。

请注意,以上示例中的路径 'path/to/shared.service' 应该替换为实际的共享服务文件的路径。此外,还可以根据具体需求在共享服务中添加更多的功能和方法。

推荐的腾讯云相关产品:腾讯云云服务器(CVM)、腾讯云云数据库MySQL版、腾讯云对象存储(COS)等。你可以通过访问腾讯云官方网站获取更多关于这些产品的详细信息和介绍。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券