我刚刚开始理解Angular(?)我正在尝试构建一个使用GameSparks作为数据提供者(SDK)的应用程序。
我的项目中有一个路由器,如下所示:
import { NgModule } from '@angular/core';
import { Routes, RouterModule, PreloadAllModules } from '@angular/router';
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{
path: 'home',
loadChildren: '../pages/home/home.module#HomePageModule'
},
{
path: 'list',
loadChildren: '../pages/list/list.module#ListPageModule'
}
];
@NgModule({
imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],
exports: [RouterModule]
})
export class AppRoutingModule { }现在,我已经在我的项目中的"scr/assets/ GameSparks“文件夹中添加了gamesparks。
在这里我有一个初始化.js脚本,如下所示:
var gamesparks = new GameSparks();
var gsKey = "XXXXXXXXX";
var gsSecret = "XXXXXXXXXXXXXXXXXXXXXXX";
var gsCredentials = "";
var isLive = true;
//Initialse the SDK
function init() {
if (isLive) {
gamesparks.initLive({
key: gsKey,
secret: gsSecret,
//credential: gsCredentials,
onNonce: onNonce,
onInit: onInit,
onMessage: onMessage,
logger: console.log,
});
} else {
gamesparks.initPreview({
key: gsKey,
secret: gsSecret,
//credential: gsCredentials,
onNonce: onNonce,
onInit: onInit,
onMessage: onMessage,
logger: console.log,
});
}
}
//Callback function to hmac sha256 a nonce with the secret. It's assumed you will have your own method of securing the secret;
function onNonce(nonce) {
return CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(nonce, gsSecret));
}
//Callback to handle when the SDK is initialised and ready to go
function onInit() {
console.log("Initialised");
// NOW I AM READY TO START THE APP, BUT NOT SURE HOW?!?!
}当onInit()被调用时,我应该能够继续前进,但是我不确定如何在路由器之前调用这个脚本,或者它是否在我应该关注的地方,以及如何使GameSparks SDK在我所有的页面中都可用?
真心希望得到一些指导或帮助,并提前表示感谢:-)
/*编辑*
按照瑞的建议做了,如下所示:
import { Injectable } from '@angular/core';
var gamesparks = new GameSparks();
var gsKey = "XXXXXXXXXX";
var gsSecret = "XXXXXXXXXXXXXXXXXXXX";
var gsCredentials = "";
@Injectable({
providedIn: 'root'
})
export class GamesparksService {
isLive = false;
init() {
if (this.isLive) {
gamesparks.initLive({
key: gsKey,
secret: gsSecret,
//credential: gsCredentials,
onNonce: this.onNonce,
onInit: this.onInit,
onMessage: this.onMessage,
logger: console.log,
});
} else {
gamesparks.initPreview({
key: gsKey,
secret: gsSecret,
//credential: gsCredentials,
onNonce: this.onNonce,
onInit: this.onInit,
onMessage: this.onMessage,
logger: console.log,
});
}
}
//Callback function to hmac sha256 a nonce with the secret. It's assumed you will have your own method of securing the secret;
onNonce(nonce) {
return CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(nonce, gsSecret));
}
//Callback to handle when the SDK is initialised and ready to go
onInit() {
console.log("Initialised");
}
//Callback to handle async messages from the gamesparks platform
onMessage(message) {
console.log("onMessage: " + JSON.stringify(message["scriptData"]));
}
}现在我有一些其他的问题。这个脚本叫做gamesparks.service.ts,是文件夹"./app/services/“
现在,我的游戏资源文件位于“./ .js / GameSparks /.js/”文件夹中。
在上面的脚本中,var gamesparks = new GameSparks();在"GameSparks()“和”CryptoJS“下面有一个红色下划线。?!?
如何在服务脚本中引用这一点?
希望得到帮助;-)
发布于 2019-10-04 03:53:59
不如将GameSparks初始化代码放到服务类中,在构造函数中初始化它,用getter公开GameSparks,然后将服务注入到您想要使用的组件中。
https://stackoverflow.com/questions/58225890
复制相似问题