2
 initializeApp(){
     this.platform.ready().then(() => {
       {
     document.addEventListener("deviceready", onDeviceReady, false);
      }

   function onDeviceReady() {
       console.log(navigator.camera);
       console.log("Cordova");
      }

 }

We are trying to run the onDeviceReady function and console.log will not print out

4

2 回答 2

2
 initializeApp(){
     this.platform.ready().then(() => {
         console.log(navigator.camera);
         console.log("Cordova");
     }
 }

Platform.ready()在触发时解析deviceready,因此无需再次收听。在这种情况下,在它触发后监听它会导致该处理程序永远不会执行。

于 2016-02-11T15:03:41.547 回答
0

我相信您遇到了deviceready之前开火的竞争条件platform.ready

deviceready我的建议是处理之前platform.ready和相反的两种情况。为此,您应该遵循此模式。

角度世界之外的某个地方,所以可能在你的启动 js 文件中。

document.addEventListener("deviceready", () => window['isCordovaReady'] = true, false);

然后你可以在你的initializeApp方法中使用它

initializeApp(){
  this.platform.ready().then(() => {
    if(!!window['isCordovaReady']){
      onDeviceReady();
    } else {        
      document.addEventListener("deviceready", onDeviceReady, false);
    }

    function onDeviceReady() {
      console.log(navigator.camera);
      console.log("Cordova");
    }
  }
}

此外,您发布的代码中存在语法错误。

initializeApp(){
  this.platform.ready().then(() => {
    { <-- This is extra and should be removed.
      document.addEventListener("deviceready", onDeviceReady, false);
    } <-- Missing a ');'

  function onDeviceReady() {
    console.log(navigator.camera);
    console.log("Cordova");
  }
 }
于 2016-02-09T23:24:42.553 回答