【问题标题】:Angular tsconfig not able to access the module specified in the pathsAngular tsconfig 无法访问路径中指定的模块
【发布时间】:2021-05-22 14:07:07
【问题描述】:

我使用的是 Angular 版本 11。所以我在应用程序下创建了常量文件夹以在整个应用程序中使用公共变量。为了访问它,我应该像下面这样导入 "../../constants/user.constants"

为了避免这种情况,我更新了 tsconfig.json 中的值,如下所示

"baseUrl": "./",
    "paths": {
      "@constants/*": ["src/app/constants/*"]
},

这不起作用。我收到以下错误

import { USER_TYPE_COMPANY_USER } from '@constants/user.constants';

"baseUrl": "./",
        "paths": {
          "@constants/*": ["app/constants/*"]
    },

我也在尝试这个,但没有工作

谁能帮忙解决这个问题?

提前致谢

【问题讨论】:

    标签: angular tsconfig


    【解决方案1】:

    如果您想在整个应用程序中创建公共变量,那么您可以使用这些:

    1.你可以创建共享服务:import { Injectable } from '@angular/core';

    @Injectable()  
    export class DataService {  
      
      private data = {};  
      
     setOption(option, value) {      
        this.data[option] = value;  
      }  
      
      getOption() {  
        return this.data;  
      }  
    }  
    

    并像这样使用它

       constructor(_dataService: DataService) { 
          }
    

    有关共享服务的更多信息,您可以关注:https://developerslogblog.wordpress.com/2019/04/23/how-to-use-angular-services-to-share-data-between-components/

    1. 您可以创建 constants.ts 文件并从那里导出变量并在需要的地方导入

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2023-03-18
      • 2019-04-09
      • 2016-12-24
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      相关资源
      最近更新 更多