Ionic 2:Cordova 不可用.确保包含 cordova.js 或在设备/模拟器中运行(在模拟器中运行) [英] Ionic 2: Cordova is not available. Make sure to include cordova.js or run in a device/simulator (running in emulator)

查看:34
本文介绍了Ionic 2:Cordova 不可用.确保包含 cordova.js 或在设备/模拟器中运行(在模拟器中运行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚设置了我的第一个 ionic 2 应用程序(我已经相当广泛地使用了 ionic 1).我正在尝试使用

更新... index.html

(只是ionic生成的标准的)

<html lang="en" dir="ltr"><头><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 需要cordova 应用程序--><script src="cordova.js"></script><!-- 取消注释此代码以启用服务工作者<脚本>如果(导航器中的'serviceWorker'){navigator.serviceWorker.register('service-worker.js').then(() => console.log('服务工作者安装')).catch(err => console.log('Error', err));}</script>--><link href="build/main.css" rel="样式表"><身体><!-- Ionic 的根组件以及应用程序的加载位置--><ion-app class="trans"></ion-app><!-- polyfills js是在构建过程中生成的--><script src="build/polyfills.js"></script><!-- bundle js是在构建过程中生成的--><script src="build/main.js"></script>

解决方案

livereload 插件在开发过程中无法提供 cordova.js 文件并提供//mock cordova 文件.

修复:你需要去 node_modules/@ionic/app-scripts/dist/dev-server/serve-config.js

并替换

exports.ANDROID_PLATFORM_PATH = path.join('platforms', 'android', 'assets', 'www');

exports.ANDROID_PLATFORM_PATH = path.join('platforms', 'android', 'app', 'src', 'main', 'assets', 'www');

I just set up my first ionic 2 app (I've used ionic 1 fairly extensively). I'm trying to use the ionic-native camera preview plugin.

The setup was pretty straight forward:

npm install -g ionic cordova
ionic start timesnap --v2
ionic platform add android
ionic platform add ios
ionic plugin add cordova-plugin-camera-preview --save

Then I copied and pasted the example code into the about page:

import { CameraPreview, CameraPreviewRect } from 'ionic-native';

// camera options (Size and location)
let cameraRect: CameraPreviewRect = {
  x: 100,
  y: 100,
  width: 200,
  height: 200
};


// start camera
CameraPreview.startCamera(
  cameraRect, // position and size of preview
  'front', // default camera
  true, // tap to take picture
  false, // disable drag
  true, // send the preview to the back of the screen so we can addoverlaying elements
  1 //alpha
);

I launched the app using the following commands:

ionic emulate android -lcs

ionic emulate ios -lcs --target='iPhone-6'

At first the camera just wasn't showing up then I ran chrome://inspect and saw warnings about Cordova missing "try running in an emulator", but this was while running in an android emulator. I tried iOS too and saw the same results.

Any ideas why cordova isn't loading?

Here is the full error log from chrome://inspect while running in an android emulator:

Update... index.html

(it's just the standard one generated by ionic)

<!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 class="trans"></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>

解决方案

The livereload plugin fails to serve cordova.js file and serves // mock cordova file during development.

FIX: You need go to node_modules/@ionic/app-scripts/dist/dev-server/serve-config.js

and replace

exports.ANDROID_PLATFORM_PATH = path.join('platforms', 'android', 'assets', 'www');

to

exports.ANDROID_PLATFORM_PATH = path.join('platforms', 'android', 'app', 'src', 'main', 'assets', 'www');

这篇关于Ionic 2:Cordova 不可用.确保包含 cordova.js 或在设备/模拟器中运行(在模拟器中运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆