Vue.js 如何在VueJS中调用axios.spread函数中的外部函数?
我不能在axios promise中调用任何外部函数。我的错误是 [未捕获(承诺中)类型错误:this.showNotification不是函数]Vue.js 如何在VueJS中调用axios.spread函数中的外部函数?,vue.js,axios,Vue.js,Axios,我不能在axios promise中调用任何外部函数。我的错误是 [未捕获(承诺中)类型错误:this.showNotification不是函数] showNotification是mixin中的一个外部函数,但我无法在userCompInfo函数中调用它 var mixin = { methods: { //function to get all user info userInfo:function(){ //alert(localStorage.getItem("sessionK
showNotification
是mixin中的一个外部函数,但我无法在userCompInfo
函数中调用它
var mixin = {
methods: {
//function to get all user info
userInfo:function(){
//alert(localStorage.getItem("sessionKey"))
if(localStorage.getItem("sessionKey")==null)
session = "null";
else
session= localStorage.getItem("sessionKey");
return axios.post("/api/user/info",{
user_id:localStorage.getItem("userId"),
session_key:session
});
},
//function to get all competition info
compInfo:function()
{ //calling API competition info to get the avaliable competition for now
return axios.post("/api/competition/info",{
lang:source.lang
});
},
userCompInfo:function()
{
axios.all([this.userInfo(),this.compInfo()])
.then(axios.spread(function (user, comp){
if(comp.data.result.errorcode == 0)
{
source.competition = comp.data.competiton_detail;
}
//set the user id in local storage
localStorage.setItem("userId",user.data.user_info.user_id);
//check on session key to save it in local storage
if(user.data.user_info.session_key != "") // for testing
localStorage.setItem("sessionKey",user.data.user_info.session_key);
// get user data successfuly
if(user.data.result.errorcode == 0)
{ //set response data in user
source.user = user.data.user_info;
//to set this user status
this.renderUserInfo();
///////////
//source.competition.is_active=1;
//check on user status to show notification
if(source.user.vip && source.user.suspended)
{
this.showNotification(context.suspended_vip.title+" "+source.user.msisdn, context.suspended_vip.body,["btn_notifi_ok"]);
}
//check if this valid competition is active or not to show notification according to it and to user status
if(source.competition.is_active==1)
{
if(source.user.registered && newUser==1)
{
this.showNotification(context.registered_firstTime.title, context.registered_firstTime.body, ["btn_notifi_start_play"]);
}
else if(source.user.free && source.user.newFreeUser)
{
this.showNotification(context.freeUser_firstTime.title, context.freeUser_firstTime.body, ["btn_notifi_start_play"]);
}
}
}
}));
},
// function to show notification modal
showNotification:function(title, body, buttons)
{
//to hide all modal buttons first
$("#notification").find(".btn").hide();
//set the modal title
$("#notification_title").html(title);
//set the modal body
$("#notification_body").html(body);
//for on buttons array to show all buttons that we want
for(i=0;i<buttons.length;i++)
{
$("#notification").find("#"+buttons[i]).show();
}
//to display modal
$("#notification").modal("show");
},
//function to show the current user statue VIP, registered,free or suspended
renderUserInfo:function()
{ // intialize all flags of user status to false
source.user.vip = false;
source.user.registered = false;
source.user.free = false;
//check on user mode and set the according flag to it to true
if(source.user.sub_mode == "vip")
{
source.user.vip = true;
}
else if(source.user.sub_mode == "sub")
{
source.user.registered = true;
}
else
{
source.user.free = true;
}
},}}
var mixin={
方法:{
//函数获取所有用户信息
userInfo:function(){
//警报(localStorage.getItem(“sessionKey”))
if(localStorage.getItem(“sessionKey”)==null)
session=“null”;
其他的
session=localStorage.getItem(“sessionKey”);
返回axios.post(“/api/user/info”{
user\u id:localStorage.getItem(“userId”),
会话密钥:会话
});
},
//用于获取所有竞赛信息的函数
compInfo:function()
{//调用API竞争信息以获取当前可用的竞争信息
返回axios.post(“/api/competition/info”{
lang:source.lang
});
},
userCompInfo:function()
{
axios.all([this.userInfo(),this.compInfo()]))
.then(axios.spread)(功能(用户、公司){
如果(comp.data.result.errorcode==0)
{
source.competition=公司数据.competition\u详情;
}
//在本地存储中设置用户id
setItem(“userId”,user.data.user\u info.user\u id);
//检查会话密钥以将其保存在本地存储器中
如果(user.data.user\u info.session\u key!=“”)//用于测试
setItem(“sessionKey”,user.data.user\u info.session\u key);
//成功获取用户数据
if(user.data.result.errorcode==0)
{//在用户中设置响应数据
source.user=user.data.user\u info;
//要设置此用户状态
这个.renderUserInfo();
///////////
//source.competition.is_active=1;
//检查用户状态以显示通知
if(source.user.vip&&source.user.suspended)
{
this.showNotification(context.suspended_vip.title+“”+source.user.msisdn,context.suspended_vip.body,[“btn_notifi_ok”]);
}
//检查此有效竞争是否处于活动状态,以根据它和用户状态显示通知
if(source.competition.is_active==1)
{
if(source.user.registered&&newUser==1)
{
this.showNotification(context.registered_firstTime.title,context.registered_firstTime.body,[“btn_notifi_start_play”]);
}
else if(source.user.free&&source.user.newFreeUser)
{
this.showNotification(context.freeUser_firstTime.title,context.freeUser_firstTime.body,[“btn_notifi_start_play”]);
}
}
}
}));
},
//函数显示通知模式
showNotification:功能(标题、正文、按钮)
{
//首先隐藏所有模式按钮
$(“#通知”).find(“.btn”).hide();
//设置模式标题
$(“#通知标题”).html(标题);
//设置模态体
$(“通知正文”).html(正文);
//用于打开按钮数组以显示所需的所有按钮
对于(i=0;i这是一个非常常见的错误。userCompInfo
中的This
不引用Vue,因为它们是您编写回调的方式
在代码中进行此更改
userCompInfo:function()
{
axios.all([this.userInfo(),this.compInfo()])
.then(axios.spread(function (user, comp){
// a bunch of code...
}.bind(this)));
},
或
请参见这是一个非常常见的错误。userCompInfo
中的
此不引用Vue,因为它们是您编写回调的方式
在代码中进行此更改
userCompInfo:function()
{
axios.all([this.userInfo(),this.compInfo()])
.then(axios.spread(function (user, comp){
// a bunch of code...
}.bind(this)));
},
或
看