【问题标题】:Connect to Google Cloud Endpoints from an Angular 2 app从 Angular 2 应用连接到 Google Cloud Endpoints
【发布时间】:2016-11-10 20:04:08
【问题描述】:

我目前正在将一个网络应用从 Angular 1 重构为 Angular 2,但我很难找到任何从 Angular 2 应用连接到 Google Cloud Endpoints 的示例。

我使用angular-cli 创建了 Angular 2 应用程序。我想使用来自几个不同服务的端点 api,所以如果有任何建议的答案考虑到这一点,那将是更好的选择。

在 Angular 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');
    }
}   

在 Angular 2 中我尝试过

Edit1:我应该注意,我只是想让 api 为初学者加载。这就是为什么我在下面的示例中将所有代码放在 index.html 文件中的原因。但是,在最佳情况下,我更喜欢从 Angular 2 应用程序启动和使用 Cloud Endpoints 的最佳实践的解决方案。

在我的 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>

当我运行 webapp 时,init 函数没有被调用(没有控制台输出)。但是,似乎脚本已加载。

感谢任何有关如何解决此问题的建议!另外,如果有人从 Google 或其他人那里找到了一些对此进行详细说明的文档,请分享!

【问题讨论】:

    标签: angular typescript google-api google-cloud-platform google-cloud-endpoints


    【解决方案1】:

    您是否尝试过在这里颠倒您的操作顺序?就像您对 Angular 1 所做的那样,尝试将 apis.google.com 导入移到 init 代码块下方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 2018-12-18
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 2020-05-27
      • 1970-01-01
      相关资源
      最近更新 更多