如何从javascript函数调用vue方法



我试图从javascript函数调用一个vue方法,但无法使其工作。我试图调用的方法是app.onChangePlant(val)。结果发现没有调用该值。

是因为我声明javascript函数时vue还不可用吗?

这是我收到的错误

plant-consumer:222 Uncaught TypeError: app.onChangePlant is not a function
at onChangePlantSelect (plant-consumer:222)
at HTMLSelectElement.onchange (plant-consumer:399)
at Object.trigger (app.js:14973)
at HTMLSelectElement.<anonymous> (app.js:15045)
at Function.each (app.js:6863)
at jQuery.fn.init.ea

代码:

<script type="module" defer>
const setting = {
url: {
findUsage: "{{ route('admin.plant-consumer.find-usage') }}",
usageCsvDownload: "{{ route('admin.plant-consumer.usage-csv-download') }}",
},
date: {
date_default: moment("{{ $defaultDate }}", 'YYYY/MM/DD').toDate()
}
};
const app = new Vue({
el: '#app',
data: {
url: setting.url,
plantId: '',
isLoading: false,
rangeFilter: 'daily',
byMonth: false,
csvDownloadUrl: '',
date: setting.date.date_default,
month: setting.date.date_default,
// datepicker用設定
DatePickerFormat: 'yyyy/MM/dd',
MonthPickerFormat: 'yyyy/MM',
disabledDates: {
from: new Date()
},
ja_vdp: vdp_translation_ja.js
},
components: {
'vuejs-datepicker': vuejsDatepicker
},
watch: {
rangeFilter: function () {
this.byMonth = this.rangeFilter === 'monthly';
this.getData(1);
},
},
methods: {
onChangePlant: function(event) {
this.plantId = event.target.value;
this.getData(1);
},
onChangeDate: function () {
this.getData(1);
},
onChangeMonth: function () {
this.getData(1);
},
getData: function(page) {
let that = this;
let date = that.date;
that.isLoading = true;
that.csvDownloadUrl = '';
if (that.byMonth) {
date = that.month;
}
if(that.plantId && ((date)))
{
let data = {
plant : that.plantId,
date : that.formatDateForRequest(date),
byMonth: that.byMonth,
page : page,
};
let url = that.url.usageCsvDownload
+ '?plant=' + data.plant
+ '&date=' + data.date
+ '&byMonth=' + data.byMonth;
// console.log(url);
$.ajax(
{
url: that.url.findUsage,
type: "get",
datatype: "html",
data: data,
cache: false
}).done(function(data){
$("#consumer_table_generate").empty().html(data);
that.csvDownloadUrl = url;
}).fail(function(jqXHR, ajaxOptions, thrownError){
alert('No response from server');
}).always(function() {
that.isLoading = false;
});
}
else
{
$("#consumer_ratio_body").empty();
that.isLoading = false;
}
}
},
mounted: function () {
let vueObj = this;
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(document).on('click', '.pagination a', function(event)
{
event.preventDefault();
let $this = $(this);
$('#consumer_table_generate li').removeClass('active');
$this.parent('li').addClass('active');
var page = $this.attr('href').split('page=')[1];
vueObj.getData(page);
});
}
});
$(document).ready(function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
});
</script>
<script>
function onChangePlantSelect(val) {
console.log(val);
app.onChangePlant(val);
};
</script>
const App = {
data: {
url: setting.url,
plantId: '',
isLoading: false,
rangeFilter: 'daily',
byMonth: false,
csvDownloadUrl: '',
date: setting.date.date_default,
month: setting.date.date_default,
// datepicker用設定
DatePickerFormat: 'yyyy/MM/dd',
MonthPickerFormat: 'yyyy/MM',
disabledDates: {
from: new Date()
},
ja_vdp: vdp_translation_ja.js
},
components: {
'vuejs-datepicker': vuejsDatepicker
},
watch: {
rangeFilter: function () {
this.byMonth = this.rangeFilter === 'monthly';
this.getData(1);
},
},
methods: {
onChangePlant: function(event) {
this.plantId = event.target.value;
this.getData(1);
},
onChangeDate: function () {
this.getData(1);
},
onChangeMonth: function () {
this.getData(1);
},
getData: function(page) {
let that = this;
let date = that.date;
that.isLoading = true;
that.csvDownloadUrl = '';
if (that.byMonth) {
date = that.month;
}
if(that.plantId && ((date)))
{
let data = {
plant : that.plantId,
date : that.formatDateForRequest(date),
byMonth: that.byMonth,
page : page,
};
let url = that.url.usageCsvDownload
+ '?plant=' + data.plant
+ '&date=' + data.date
+ '&byMonth=' + data.byMonth;
// console.log(url);
$.ajax(
{
url: that.url.findUsage,
type: "get",
datatype: "html",
data: data,
cache: false
}).done(function(data){
$("#consumer_table_generate").empty().html(data);
that.csvDownloadUrl = url;
}).fail(function(jqXHR, ajaxOptions, thrownError){
alert('No response from server');
}).always(function() {
that.isLoading = false;
});
}
else
{
$("#consumer_ratio_body").empty();
that.isLoading = false;
}
}
},
mounted: function () {
let vueObj = this;
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(document).on('click', '.pagination a', function(event)
{
event.preventDefault();
let $this = $(this);
$('#consumer_table_generate li').removeClass('active');
$this.parent('li').addClass('active');
var page = $this.attr('href').split('page=')[1];
vueObj.getData(page);
});
}
}
const app = Vue.createApp(App)
const vm = app.mount('#blog-posts-demo')
vm.remove(123456)

//通过这种方式,你可以保证应用程序在你调用时正确安装

我不知道你为什么要这么做,但如果必要的话,你可以尝试使用窗口事件侦听器:

https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events

在你的组件中,你可以添加一个调用你的方法的eventlistener,在js脚本中你可以激发一个事件,如果你的组件准备好了,它会调用该方法。

我以前从来没有这样做过——这只是一个理论:(

编辑:我试过了,但当你想在脚本标签中触发事件时,你的组件没有挂载

最新更新