【问题标题】:Trying to add a class wrapped in a module declaration to angular尝试将包装在模块声明中的类添加到角度
【发布时间】:2021-09-15 16:01:21
【问题描述】:

我使用 Typewriter 从我的 C# 代码中创建了一个打字稿类。当我尝试通过导入在我的 Angular 项目中引用它时,它说如果我使用命名空间,模块不存在或命名空间无法识别。

将类添加到我的项目以便我可以在其他文件中使用它的正确方法是什么?

module Budget.Infrastructure.Model {
        // $Classes/Enums/Interfaces(filter)[template][separator]
        // filter (optional): Matches the name or full name of the current item. * = match any, wrap in [] to match attributes or prefix with : to match interfaces or base classes.
        // template: The template to repeat for each matched item
        // separator (optional): A separator template that is placed between all templates e.g. $Properties[public $name: $Type][, ]
    
        // More info: http://frhagn.github.io/Typewriter/
    
        
        export class User {
            
            // ID
            public id: string = null;
            // FIRSTNAME
            public firstName: string = null;
            // LASTNAME
            public lastName: string = null;
            // EMAIL
            public email: string = null;
        }
    }

【问题讨论】:

标签: angular typescript typewriter


【解决方案1】:

你必须导出模块

export module Budget.Infrastructure.Model {
        // $Classes/Enums/Interfaces(filter)[template][separator]
        // filter (optional): Matches the name or full name of the current item. * = match any, wrap in [] to match attributes or prefix with : to match interfaces or base classes.
        // template: The template to repeat for each matched item
        // separator (optional): A separator template that is placed between all templates e.g. $Properties[public $name: $Type][, ]
    
        // More info: http://frhagn.github.io/Typewriter/
    
        
        export class User {
            
            // ID
            public id: string = null;
            // FIRSTNAME
            public firstName: string = null;
            // LASTNAME
            public lastName: string = null;
            // EMAIL
            public email: string = null;
        }
}

你可以像这样导入

import {Budget} from "./test";
import User = Budget.Infrastructure.Model.User;

【讨论】:

  • 由于类是由打字机自动生成的,我认为我没有在模块之前添加 export 关键字的选项。有没有其他方法可以做到这一点,还是我不走运?
  • 据我所知,没有其他办法:(
  • 我能够更改打字机中的语句,但似乎在识别它时遇到了一些额外的问题。对于导入,为什么是 Budget 而不是 Budget.Infrastructure.Model?
猜你喜欢
  • 1970-01-01
  • 2018-05-15
  • 2017-09-30
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 2021-02-19
  • 1970-01-01
相关资源
最近更新 更多