首页 文章

Ionic 2运行时错误:Cordova未定义

提问于
浏览
2

当我尝试在浏览器中运行我的Ionic v3.14.0混合应用程序时,我收到了运行时错误"Cordova is undefined" . 我按照this post的回答并运行以下命令

ionic cordova platform add browser
ionic cordova run browser

上面的第一个命令将 Platforms/browser 添加到我的项目中,并在其下面有一个文件夹结构,我可以在 Platforms/browser/www 下找到 cordova.js . 但是,当我运行上面的第二个comman时,会打开一个空白的chrome浏览器实例(虽然我的默认设置是IE),并且在没有显示应用程序的情况下保持空白 . 下面是终端的堆栈跟踪 .

PS C:\Data\Per\Biz\Ionic\MyApp> ionic cordova run browser
Running app-scripts build: --platform browser --target cordova
[00:47:27]  build dev started ...
[00:47:27]  clean started ...
[00:47:27]  clean finished in 12 ms
[00:47:27]  copy started ...
[00:47:27]  deeplinks started ...
[00:47:28]  deeplinks finished in 113 ms
[00:47:28]  transpile started ...
[00:47:31]  transpile finished in 3.71 s
[00:47:31]  preprocess started ...
[00:47:31]  preprocess finished in 1 ms
[00:47:31]  webpack started ...
[00:47:32]  copy finished in 4.39 s
[00:47:41]  webpack finished in 9.49 s
[00:47:41]  sass started ...
[00:47:42]  sass finished in 1.40 s
[00:47:42]  postprocess started ...
[00:47:42]  postprocess finished in 16 ms
[00:47:42]  lint started ...
[00:47:42]  build dev finished in 15.21 s
> cordova run browser
Error loading cordova-browser

Running command: cmd "/s /c "C:\Data\Per\Biz\Ionic\MyApp\platforms\browser\cordova\build.bat""

Cleaning Browser project
[00:47:46]  lint finished in 4.18 s
Running command: cmd "/s /c "C:\Data\Per\Biz\Ionic\MyApp\platforms\browser\cordova\run.bat --nobuild""
Static file server running @ http://localhost:8000/index.html
CTRL + C to shut down
200 /index.html (gzip)
200 /cordova.js (gzip)
200 /build/main.js (gzip)
200 /build/polyfills.js (gzip)
200 /build/main.css (gzip)
200 /cordova_plugins.js (gzip)
200 /plugins/cordova-plugin-device/www/device.js (gzip)
200 /plugins/cordova-plugin-device/src/browser/DeviceProxy.js (gzip)
200 /plugins/com.salesforce/www/com.salesforce.plugin.oauth.js (gzip)
200 /plugins/com.salesforce/www/com.salesforce.plugin.network.js (gzip)
200 /plugins/com.salesforce/www/com.salesforce.plugin.sdkinfo.js (gzip)
200 /plugins/com.salesforce/www/com.salesforce.plugin.smartstore.js (gzip)
200 /plugins/com.salesforce/www/com.salesforce.plugin.smartstore.client.js (gzip)
200 /plugins/com.salesforce/www/com.salesforce.plugin.sfaccountmanager.js (gzip)
200 /plugins/com.salesforce/www/com.salesforce.plugin.smartsync.js (gzip)
200 /plugins/com.salesforce/www/com.salesforce.util.bootstrap.js (gzip)
200 /plugins/com.salesforce/www/com.salesforce.util.event.js (gzip)
200 /plugins/ionic-plugin-keyboard/www/browser/keyboard.js
200 /plugins/com.salesforce/www/com.salesforce.util.exec.js (gzip)
200 /plugins/com.salesforce/www/com.salesforce.util.logger.js (gzip)
200 /plugins/com.salesforce/www/com.salesforce.util.promiser.js (gzip)
200 /plugins/com.salesforce/www/com.salesforce.util.push.js (gzip)
200 /plugins/cordova-plugin-splashscreen/www/splashscreen.js (gzip)
200 /plugins/cordova-plugin-splashscreen/src/browser/SplashScreenProxy.js (gzip)
200 /config.xml (gzip)
404 /screen
200 /build/vendor.js (gzip)
200 /assets/icon/favicon.ico (gzip)

我在index.html中包含了 <script src="cordova.js"></script>

下面是我宣布Cordova的ts文件: data-service.ts

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';

declare let cordova:any;

/*
  Generated class for the DataServiceProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class DataServiceProvider {
  indexSpecs:any[];
  soupName = "menuItems"; 

  constructor(public http: Http) {
    console.log('Hello DataServiceProvider Provider');
  }

  getMainMenu(){
    return this.http.get('assets/data/mainmenu.json')
    .map((response:Response)=>response.json().Categories);
  }

  getMainMenuItems() {
    this.registerSmartStoreSoup();
  }

  registerSmartStoreSoup() {
    this.indexSpecs = [{path:"Name",type:"string"},{path:"Id",type:"string"}];
    cordova.require("com.salesforce.plugin.smartstore").registerSoup(this.soupName,this.indexSpecs,this.successCallback,this.errorCallback);
  }

  successCallback() {
    console.log("Soup "+this.soupName+" Created Successfully");
  }

  errorCallback(err) {
    console.log("Error Creating "+this.soupName+":"+err)
  }

}

上面的getMainMenuItems()在 app.component.ts 的构造函数中调用

constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, public dataService: DataServiceProvider) {
    this.initializeApp();

    this.dataService.getMainMenuItems();

我怀疑上面的堆栈跟踪中的输出 "Error loading cordova-browser" . 但不确定这里的问题 . 有人可以帮助我,我在这里被打了一会儿 .

2 回答

  • 0

    您所指的链接是使用Ionic I我猜 . 请尝试以下步骤 .

    • 删除节点模块文件夹并运行 npm install -g ionic@latest

    • 直接运行 ionic serve 以在浏览器中运行 .

    • 如果要运行设备,请安装cordova npm install -g cordova

    请参考official documentation for commands

  • 0

    com.salesforce.plugin Doesn't have support for Browser. 它仅支持移动平台(即Android和iOS) .

相关问题