【问题标题】:TS file generated by Typewriter is not a moduleTypewriter 生成的 TS 文件不是模块
【发布时间】:2019-03-08 18:00:54
【问题描述】:

我使用 Visual Studio 中的 Typewriter 扩展来生成模型 (Account.ts),但是当我尝试将模型导入另一个类时它失败了。我做错了什么?

import { Account } from '../../models/greencard/Account';

错误

'C:/Users/me/Desktop/_REPOS/stuff/ClientApp/src/app/models/greencard/Account.ts' is not a module.   

打字机文件

${
    // Enable extension methods by adding using Typewriter.Extensions.*
    using Typewriter.Extensions.Types;

    // Uncomment the constructor to change template settings.
    //Template(Settings settings)
    //{
    //    settings.IncludeProject("Project.Name");
    //    settings.OutputExtension = ".tsx";
    //}

    // Custom extension methods can be used in the template by adding a $ prefix e.g. $LoudName
    string LoudName(Property property)
    {
        return property.Name.ToUpperInvariant();
    }
}
module InvWebOps.EFModels.TypewriterTSTFiles {

templates e.g. $Properties[public $name: $Type][, ]

    // More info: http://frhagn.github.io/Typewriter/

$Classes(Account)[
    export class $Name {
        $Properties[
        // $LoudName
        public $name: $Type = $Type[$Default];]
    }]
}

自动生成的 Account.ts

module InvWebOps.EFModels.TypewriterTSTFiles {

    // More info: http://frhagn.github.io/Typewriter/


    export class Account {

        // ID
        public id: number = 0;
     ......

【问题讨论】:

    标签: c# typescript typewriter


    【解决方案1】:

    简答

    您可以通过using its namespace 访问其他文件中的Account 类。

    namespace InvWebOps.EFModels.TypewriterTSTFiles {
        const account = new Account();
    }
    
    // the deprecated `module` keyword also works
    module InvWebOps.EFModels.TypewriterTSTFiles {
        const account = new Account();
    }
    
    // a fully qualified name also works
    const account = new InvWebOps.EFModels.TypewriterTSTFiles.Account();
    

    更多详情

    我做错了什么?

    关键字modulehas been deprecated in favor ofnamespace。这两个关键字的含义相同,但第二个关键字不那么令人困惑。官方 TypeScript 文档对命名空间这么说:

    命名空间只是全局命名空间中命名的 JavaScript 对象。这使得命名空间成为一个非常简单易用的构造。它们可以跨越多个文件... [并且] ...可以成为在 Web 应用程序中构建代码的好方法...

    生成的Account 代码做了两件事:

    1. InvWebOps.EFModels.TypewriterTSTFiles 命名空间添加到全局范围。
    2. 从该命名空间中导出 Account 类。

    从命名空间导出的任何内容都只能在该命名空间内访问。因此,每当一行代码需要访问Account 类时,该行代码就需要使用Account 类的命名空间。简短的回答显示了三种方法。

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 2018-09-19
      • 1970-01-01
      相关资源
      最近更新 更多