如何使角度 4 中的字符串常数?

2024-05-05

在我的服务中,我正在使用http邮政。我想将 URL 设置为常量。

return this.http.get(this.config.API_URL+'users', options).map(res=>res.json());

我尝试过一项服务:

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

@Injectable()
export class AppService {
  API_URL :String;

  constructor() {
    this.API_URL = 'some url';
  }
}

有没有其他方法可以在 Angular4 中设置常量值?


我不确定我是否理解你的问题,但如果你想创建常量,你可以在不同的类中创建并导入它。

常量.ts

export class Constants {
  public static get HOME_URL(): string { return "sample/url/"; };
}

样本.组件.ts

   import { Constants } from "./constants";
    @Component({

    })
     export class SampleComponent {
      constructor() {
       let url = Constants.HOME_URL;
      }
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使角度 4 中的字符串常数? 的相关文章

随机推荐