gpt4 book ai didi

angular - 从 Angular 2 应用程序连接到 Google Cloud Endpoints

转载 作者:搜寻专家 更新时间:2023-10-30 21:52:36 24 4
gpt4 key购买 nike

我目前正在将 Web 应用程序从 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 应用启动和使用云端点的最佳实践的解决方案。

在我的 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 函数(没有控制台输出)。但是,脚本似乎已加载。

enter image description here

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

最佳答案

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

关于angular - 从 Angular 2 应用程序连接到 Google Cloud Endpoints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40536093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com