如何在Javascript中调用它之外的vue实例
问题描述:
如何在javascript中调用测试vue?这是我的代码,我想在 javascript 函数中执行某些操作时调用 test.
How can I call the test vue in javascript? Here is my code, I want to call test when I do something in javascript function.
function clickit() {
this.test.fetchTestData();
}
var test = new Vue({
el: '#viewport',
data: {
test_data: []
},
mounted: function () {
this.fetchTestData();
},
methods: {
fetchTestData: function () {
$.get(test.json, function (data) {
this.test_data = data;
alert(this.test_data.isActive);
});
}
}
});
答
您正在尝试在 clickit()
where this
内使用 this
指的是window
,所以你只需要去掉this
,它应该调用视图模型内部的方法:
You are attempting to use this
inside clickit()
where this
refers to window
, so you just need to remove this
and it should call the method inside the view model:
function clickit() {
test.fetchTestData();
}