怎么样定义一个外部变量?
像这样:
this.platform.ready().then(() => {
var myToken;
FCMPlugin.getToken(
function (token) {
console.log(token);
myToken= token;
},
function (err) {
console.log('error retrieving token: ' + err);
}
);
console.log(myToken); //I would to pass the token value to here
}
如果为gettoken是异步,ü可以触发事件或者(仅回调)做回调
this.platform.ready().then(() => {
FCMPlugin.getToken(
function (token) {
console.log(token);
myCallback(token);
$('html').trigger('tokenReady',token);
},
function (err) {
console.log('error retrieving token: ' + err);
}
);
function myCallback(myToken){
console.log(myToken);
}
}
$('html').on('tokenReady',function(e,token){
myCallback(token);
});
第三个版本:
this.platform.ready().then(() => {
FCMPlugin.getToken(
function (token) {
console.log(token);
myCallback(token);
},
function (err) {
console.log('error retrieving token: ' + err);
}
);
function myCallback(myToken){
console.log(myToken);
}
}
有没有办法移动成功回调内的代码?在我看来,它甚至可能在获得令牌之前运行你的代码。 –
可能重复的[如何返回来自异步调用的响应?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) –