【问题标题】:javascript import issues with import vs requirejavascript导入问题与import vs require
【发布时间】:2019-09-27 12:57:03
【问题描述】:

我正在使用 Electorn 和 angular 8 构建桌面应用程序。我正在尝试在 index.html 中导入一个 javascript 文件,其内容如下。

import ipcRenderer from 'electron';

import {
    START_NOTIFICATION_SERVICE,
    NOTIFICATION_SERVICE_STARTED,
    NOTIFICATION_SERVICE_ERROR,
    NOTIFICATION_RECEIVED,
    TOKEN_UPDATED,
} from 'electron-push-receiver/src/constants';

所以当我使用上面的代码时,我得到错误Uncaught SyntaxError: Unexpected identifier

当我使用以下代码时,我得到Uncaught ReferenceError: require is not defined

 const { ipcRenderer } = require ('electron')
 const {
     START_NOTIFICATION_SERVICE,
     NOTIFICATION_SERVICE_STARTED,
     NOTIFICATION_SERVICE_ERROR,
     NOTIFICATION_RECEIVED,
     TOKEN_UPDATED,
 } = require ('electron-push-receiver/src/constants')

有什么解决办法?

所有角度导入都适用于上面的第一个 sn-ps。他们没有require。所以我假设第一个 sn-p 应该在我以角度导入它时工作。

我通过在 angular.json 文件中指定以下内容来导入文件。

"scripts": [
              "../path/to/.js",
            ]

【问题讨论】:

  • import 适用于 ES6 模块。您是否将应用程序的脚本标记类型设置为模块,如下所示:<script type="module" src="...">?还是 Angular 会自动为您做到这一点?没有这个,导入将不起作用。来源:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 您是否为import 获得了Unexpected identifierrequire 是特定于节点的,如果您使用的是 requirejs 模块 laoder。
  • angular 可能会自动进行@user95227,因为ts 文件中的其他导入工作。
  • @ambianBeing 当我使用 import 我得到 Unexpected Identifier 但我不确定哪个确切的标识符是。
  • 意外的标识符可能是import

标签: javascript angular ecmascript-6 electron


【解决方案1】:

要使 ipcRenderer 的导入工作,您必须像这样建立它:

import { ipcRenderer } from "electron";

ipcRenderer 是您从 electron 库中提取的模块。

【讨论】:

    猜你喜欢
    • 2021-03-16
    • 1970-01-01
    • 2021-11-12
    • 2016-12-19
    • 1970-01-01
    • 2021-12-19
    • 2014-02-14
    • 2019-04-16
    • 1970-01-01
    相关资源
    最近更新 更多