【问题标题】:error TS2552: Cannot find name 'Windows' for @types/winrt-uwp错误 TS2552:找不到 @types/winrt-uwp 的名称“Windows”
【发布时间】:2018-06-14 09:37:54
【问题描述】:

在 Windows 10 操作系统中托管的 Angular 6 项目中,

我启动命令:

npm install --save @types/winrt-uwp

它应该允许我使用 winRT 功能和编写代码:

  public showToast(message, iconUrl) {
    if (
      typeof Windows !== "undefined" &&
      typeof Windows.UI !== "undefined" &&
      typeof Windows.UI.Notifications !== "undefined"
    ) {
      var notifications = Windows.UI.Notifications;
      var template = notifications.ToastTemplateType.toastImageAndText01;
      var toastXml = notifications.ToastNotificationManager.getTemplateContent(
        template
      );
      var toastTextElements = toastXml.getElementsByTagName("text");
      toastTextElements[0].appendChild(toastXml.createTextNode(message));
      var toastImageElements = toastXml.getElementsByTagName("image");
      var newAttr = toastXml.createAttribute("src");
      newAttr.value = iconUrl;
      var altAttr = toastXml.createAttribute("alt");
      altAttr.value = "toast graphic";
      var attribs = toastImageElements[0].attributes;
      attribs.setNamedItem(newAttr);
      attribs.setNamedItem(altAttr);
      var toast = new notifications.ToastNotification(toastXml);
      var toastNotifier = notifications.ToastNotificationManager.createToastNotifier();
      toastNotifier.show(toast);
    }
  }

但是当我想启动项目时,我收到了错误消息:

错误 TS2552:找不到名称“Windows”。您指的是“窗口”吗?

但是我可以创建一个文件node_modules/@types/winrt-uwp/index.d.ts,其中包含

 ...
declare namespace Windows {
...
}
..

你对这个问题有什么想法吗?

【问题讨论】:

    标签: angular typescript uwp windows-runtime angular6


    【解决方案1】:

    为了能够使用 winRT 功能,您必须将此代码添加到 maint.ts 文件的顶部

    /// <reference types="@types/winrt-uwp" />
    

    并以这种方式配置angular.json

        "options": {
                 ...
                "index": "src/index.html",
                "main": "src/main.ts",
                 ...
              },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      • 2019-03-11
      • 2019-11-25
      • 2020-04-24
      • 2020-03-27
      相关资源
      最近更新 更多