首页 文章

声明变量时Angular App中的语法冲突

提问于
浏览
1

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

这是工作版本:

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'] ,但这也行不通(也会导致语法错误) .

有没有解决的办法?或者我们是否只需要使用下划线语法来处理过滤器参数?

1 回答

  • 2

    使用引号在属性名称周围将允许分配:

    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,
        }]);
    }
    

相关问题