【问题标题】:Syntax Conflict in Angular App When Declaring Variable声明变量时Angular App中的语法冲突
【发布时间】:2018-04-19 22:21:10
【问题描述】:

我有一个函数用于在我的 Angular 应用程序中处理分页。它按预期工作 - 我订阅了 url 参数,然后使用路由器根据这些参数进行导航,同时将页码作为传入值。第一个参数是一个布尔值,指示过滤器当前是否处于活动状态,第二个参数是过滤器的值本身。

这是工作版本:

public paginate(page) {
  this.route.params.subscribe(
    (params: any) => {
      this.pn_location_e = params['pn_location_e'];
      this.pn_location_v = params['pn_location_v'];
    }
  );

  this.router.navigate(
    ['/clients', {
      page: page,
      pn_location_e: this.pn_location_e,
      pn_location_v: this.pn_location_v,
    }]);

  let fn = resRecordsData => {
    this.records = resRecordsData;
    let data = resRecordsData.data;
  };

  this.dataService.filterByInput(
    page - 1, this.pagesize, this.location, fn);
}

上面的一切都按预期工作。

但是,最近一位同事将过滤器语法从使用“_”更改为使用“.”。所以它是从这个开始的:

this.pn_location_e = params['pn_location_e'];

到这里:

this.pn_location.e = params['pn_location.e'];

问题是,在我的 Angular 组件中,我无法使用该语法初始化变量。当我尝试像这样初始化时:

pn_location.e

...我收到语法错误。我也试过这样做pn_location['.e'],但这也行不通(也会导致语法错误)。

有没有办法解决这个问题?还是我们只需要返回使用下划线语法来为我们的过滤器参数使用?

【问题讨论】:

  • 你能提供你得到的确切语法错误吗?

标签: javascript angular typescript


【解决方案1】:

用引号将属性名称括起来将允许赋值:

public paginate(page) {
  this.route.params.subscribe(
    (params: any) => {
      this.pn_location_e = params['pn_location.e'];
      this.pn_location_v = params['pn_location.v'];
    }
  );

  this.router.navigate(
    ['/clients', {
      page: page,
      'pn_location.e': this.pn_location_e,
      'pn_location.v': this.pn_location_v,
    }]);
}

【讨论】:

  • 完美。谢谢@ConnorsFan!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 1970-01-01
  • 2017-07-20
  • 2017-07-15
  • 2021-08-11
  • 1970-01-01
相关资源
最近更新 更多