【问题标题】:Import TNS Modules in the same typescript file of the angular web app在 Angular Web 应用程序的同一个打字稿文件中导入 TNS 模块
【发布时间】:2026-01-25 00:15:01
【问题描述】:

Nativescript Angular 以其代码共享属性而闻名。我试图通过仅使用 1 个打字稿文件而不​​是拆分为 .ts.tns.ts 文件来简化我的设计。

我试图在.ts 中使用import { Page } from "tns-core-modules/ui/page";。在 Android 上运行时,代码可以完美运行,但如果我 ng serve 用于 Web 应用程序,它会显示为 Module not found: Error: Can't resolve 'tns-core-modules/ui/page'

之所以要导入页面模块是因为设置了动作栏属性

constructor(private page: Page) { 

  if (isAndroid) {
    console.log("This is Android");
    this.page.actionBarHidden = true;
  }
}

我希望将tns-core-modules/ui/page 和其他一些 tns-core-modules 导入到与 Angular Web 应用程序相同的文件中。有可能这样做吗?还是必须拆分成.ts.tns.ts 文件?

【问题讨论】:

    标签: nativescript angular2-nativescript


    【解决方案1】:

    您必须使用特定于平台的 ts 文件,一个用于 web,一个用于 tns,Page 在浏览器中运行时将无效(ng serve)。

    如果您更喜欢重用大部分代码,请尝试编写一个 common/base ts 组件,从 common/base ts 组件扩展特定于平台的 ts 文件,仅在 tns 特定 ts 文件中注入 Page

    【讨论】: