2

我目前正在重構從Angular 1到Angular 2的web應用程序,但我無法找到任何連接到來自Angular 2應用程序的Google Cloud Endpoints的示例。從Angular 2應用程序連接到Google Cloud Endpoints

我使用angular-cli來創建Angular 2應用程序。我想使用來自兩種不同服務的端點api,所以如果任何建議的答案考慮到這一點,那將是更可取的。

在角1我做了以下

在我的index.html:

<script> 
     function init() { 
      console.log("Initializing Cloud Endpoints.."); 
      window.init(); 
     }; 
    </script>   

    <script src="https://apis.google.com/js/client.js?onload=init"></script> 

,在我main.js:

function Main($scope, $window){ 
    var vm = this; 

    $window.init= function() { 
     $scope.$apply(vm.loadApi); 
    }; 

    vm.loadApi = function(){ 
     gapi.client.load('deviceStockApi', 'v1', function(){ 

      console.log("Cloud Endpoints are now available"); 

     }, 'https://name-of-my-endpoints-app.appspot.com/_ah/api'); 
    } 
} 

在角2我試過

編輯1:我應該注意到,我只是想讓api爲初學者加載。這就是爲什麼我在下面的例子中將所有代碼放在index.html文件中的原因。然而,最好的方式是,我希望有一個解決方案,以便從Angular 2應用程序中啓動和使用雲端點。

在我的index.html:

<script src="https://apis.google.com/js/client.js?onload=init"></script> 

<script> 
    function init() { 
    console.log("Init called"); 

    gapi.client.load('deviceStockApi', 'v1', function(){ 
     console.log("Cloud Endpoints are now available"); 
    }, 'https://name-of-my-endpoints-app.appspot.com/_ah/api'); 
    } 
</script> 

當我運行web應用程序,init函數不叫(無控制檯輸出)。但是,似乎腳本已加載。

enter image description here

對於如何解決這一問題的任何建議表示讚賞!另外,如果有人從谷歌或其他人那裏找到一些文檔詳細說明,請分享!

回答

1

您是否試過在這裏顛倒您的操作順序?就像您爲Angular 1所做的一樣,請嘗試將apis.google.com導入移至init代碼塊下方。

相關問題