vic*_*tor 4 javascript vue.js vue-resource surveyjs
我正在尝试将surveyjs 结果发送到我的API。
在mounted() 中,我提出一个GET请求vue-resource,从我的dB 那里得到问题,然后设置surveyjs。为了发送结果,我尝试this.$http.post在surveyJSonComplete函数中使用 , 但我得到了Cannot read property 'post' of undefined. 另外,我试图将手表放在result变量上,但没有用。
mounted() {
this.$http
.get("myAPI")
.then(res => res.json())
.then(questions => {
this.questions = questions;
this.survey = new SurveyVue.Model(this.questions.pesquisa);
this.survey.locale = "pt";
this.survey.onComplete.add(function(survey) {
this.result = survey.data;
this.$http
.post(
`myAPI`,
this.result,
{ headers: { "Content-Type": "application/json" } }
)
.then(response => {
console.log(response);
UIkit.notification({
message: "Success",
pos: "top-center",
status: "success"
});
})
.catch(error => {
console.log(error);
UIkit.notification({
message: "Erro",
pos: "top-center",
status: "danger"
});
});
});
})
.catch(error => {
console.log(error);
UIkit.notification({
message: "Error",
pos: "top-center",
status: "danger"
});
});
}
Run Code Online (Sandbox Code Playgroud)
要访问的参数this内部onComplete.add(),您可以用箭头函数替换常规函数:
this.survey.onComplete.add(survey => {
this.result = survey.data;
/* rest of your code... */
})
Run Code Online (Sandbox Code Playgroud)
另一种方法是this放入一个变量中,该变量可用于访问外部this:
const that = this;
this.survey.onComplete.add(function(survey) {
that.result = survey.data;
/* rest of your code... */
})
Run Code Online (Sandbox Code Playgroud)
阅读更多关于this.
它的要点是,在函数内部,函数this会覆盖组件的this,除非它是一个箭头函数,它故意没有一个,this所以外部函数是可用的。
| 归档时间: |
|
| 查看次数: |
1039 次 |
| 最近记录: |