Javascript-离子2:ReferenceError:未定义webpackJsonp

我是Ionic的新手。 我已经开始使用超级模板进行项目。 但是,当我尝试在浏览器中运行该应用程序时。 它抛出一个错误,说:

ReferenceError: webpackJsonp is not defined
    at http://localhost:8100/build/main.js:1:1

我试过将vendor.js放到index.html中,但是没有用。

这是index.html文件。 我删除了vendor.js,因为它不起作用。

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="UTF-8">
  <title>Ionic App</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <meta name="format-detection" content="telephone=no">
  <meta name="msapplication-tap-highlight" content="no">

  <link rel="icon" type="image/x-icon" href="assets/icon/favicon.ico">
  <link rel="manifest" href="manifest.json">
  <meta name="theme-color" content="#4e8ef7">

  <!-- cordova.js required for cordova apps -->
  <script src="cordova.js"></script>

  <!-- un-comment this code to enable service worker
  <script>
    if ('serviceWorker' in navigator) {
      navigator.serviceWorker.register('service-worker.js')
        .then(() => console.log('service worker installed'))
        .catch(err => console.log('Error', err));
    }
  </script>-->

  <link href="build/main.css" rel="stylesheet">

</head>
<body>

  <!-- Ionic's root component and where the app will load -->
  <ion-app></ion-app>

  <!-- The polyfills js is generated during the build process -->
  <script src="build/polyfills.js"></script>

  <!-- The bundle js is generated during the build process -->
  <script src="build/main.js"></script>

</body>
</html>
Vishal Singh asked 2020-01-14T10:22:46Z
8个解决方案
109 votes

从字面上看,您经历了同样的事情。 我在/src/index.html中的main.js之前添加了vendor.js脚本-现在它在本地运行。

  <!-- The polyfills js is generated during the build process -->
  <script src="build/polyfills.js"></script>

  <script src="build/vendor.js"></script>

  <!-- The bundle js is generated during the build process -->
  <script src="build/main.js"></script>
Eric Winterstine answered 2020-01-14T10:23:07Z
56 votes

这是Ionic-App脚本的重大变化

[https://github.com/ionic-team/ionic-app-scripts/releases/tag/v2.0.0]

src / index.html必须修改为包括新的供应商脚本标签。

...
<body>

  <!-- Ionic's root component and where the app will load -->
  <ion-app></ion-app>

  <script src="cordova.js"></script>

  <!-- The polyfills js is generated during the build process -->
  <script src="build/polyfills.js"></script>

  <!-- all code from node_modules directory is here -->
  <script src="build/vendor.js"></script>

  <!-- The bundle js is generated during the build process -->
  <script src="build/main.js"></script>

</body>
...
VRPF answered 2020-01-14T10:23:36Z
17 votes

< your application directory > /src/index.html中的脚本标签内添加vendor.js路径

<script src="build/vendor.js"></script>

同时在< your application directory >/src/service-worker.js文件中进行更改-在precache部分中包括vendor.js

// pre-cache our key assets
self.toolbox.precache(
    [
      './build/main.js',
      './build/vendor.js',   // <===  Add vendor.js
      './build/main.css',
      './build/polyfills.js',
      'index.html',
      'manifest.json'
    ]
);
sijo vijayan answered 2020-01-14T10:24:00Z
16 votes

当我开始使用ionic 3开发旧的ionic 2项目时,我面临着同样的问题。请按照以下步骤为我工作。opne src\index.html把这条线

<script src="build/vendor.js"></script>

之前

<script src="build/main.js"></script>

之后

<script src="build/polyfills.js"></script>

像这样

<!DOCTYPE html>
...
<body>

  <!-- Ionic's root component and where the app will load -->
  <ion-app>
  </ion-app>
  <!-- The polyfills js is generated during the build process -->
  <script src="build/polyfills.js"></script>
  <script src="build/vendor.js"></script>  <---- here
  <!-- The bundle js is generated during the build process -->
  <script src="build/main.js"></script>

</body>

</html>
Kishan Oza answered 2020-01-14T10:24:33Z
0 votes
npm install -g ionic@v3.0.1

要么

yarn add -g ionic@v3.0.1
yacine benzmane answered 2020-01-14T10:24:53Z
0 votes

离子版本问题兄弟。

检查版本。

npm install -g ionic@v3.0.1
npm install -g ionic@v2.0.1
npm install -g ionic@v1
Dinesh answered 2020-01-14T10:25:17Z
0 votes

遇到此错误时,我正在从事ReactJs项目。 这可能是缺少package.json文件中的依存关系的情况,该文件最终以OP报告的错误形式冒泡。 在我们的案例中,缺少对omitJs npm软件包的引用。 我在package.json文件的“依赖项”部分中添加以下内容的那一刻,所有这些都开始工作:

"dependencies": {
.....other dependencies
"omit.js": "1.0.0"
}
RBT answered 2020-01-14T10:25:37Z
0 votes

我只是遇到了这个问题,在我的情况下,文件polyfills / vendor / main的顺序并不需要做任何事情,但这只是vendor.js文件的大小。

我确实意识到了这一点,因为它可以在我的本地计算机上运行,因此,我确实发现vendor.js为5MB,因此,我再次使用--prod参数构建了该应用程序:

ionic cordova build ios --prod
gersonmontenegro answered 2020-01-14T10:26:02Z
translate from https://stackoverflow.com:/questions/44988166/ionic-2-referenceerror-webpackjsonp-is-not-defined