我使用 Visual Studio 中的 Typewriter 扩展来生成模型 (Account.ts),但是当我尝试在另一个类中导入模型时,它失败了。我究竟做错了什么?
import { Account } from '../../models/greencard/Account';
Error
'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;
......
简答
您可以访问Account
在其他文件中的类使用它的命名空间 https://www.typescriptlang.org/docs/handbook/namespaces.html.
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();
更多细节
我究竟做错了什么?
关键词module
已被弃用,取而代之的是 https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html namespace
。这两个关键字的含义相同,但第二个关键字不那么令人困惑。 TypeScript 官方文档对于命名空间是这样说的:
命名空间只是全局命名空间中命名的 JavaScript 对象。这使得命名空间成为一个非常简单的使用结构。它们可以跨越多个文件...[并且]...可以是在 Web 应用程序中构建代码的好方法...
生成的Account
代码做了两件事:
- 添加了
InvWebOps.EFModels.TypewriterTSTFiles
命名空间到全局范围。
- 出口一个
Account
来自该命名空间的类。
从命名空间导出的任何内容都只能从该命名空间内访问。因此,每当一行代码需要访问Account
类,该行代码需要使用Account
类的命名空间。简短的回答显示了三种方法来做到这一点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)