我有一个帮助过滤数据的函数。当用户更改选择时,我正在使用v-on:change
,但我还需要在用户选择数据之前调用该函数。我也做了同样用AngularJS
以前使用ng-init
但据我所知,没有这样的vue.js
如何在页面加载时调用vue.js函数
这一个指令是我的函数:
getUnits: function() {
var input = {block: this.block, floor: this.floor, unit_type: this.unit_type, status: this.status};
this.$http.post('/admin/units', input).then(function (response) {
console.log(response.data);
this.units = response.data;
}, function (response) {
console.log(response)
});
}
在blade
文件我用刀片的形式来执行过滤器:
<div class="large-2 columns">
{!! Form::select('floor', $floors,null, ['class'=>'form-control', 'placeholder'=>'All Floors', 'v-model'=>'floor', 'v-on:change'=>'getUnits()' ]) !!}
</div>
<div class="large-3 columns">
{!! Form::select('unit_type', $unit_types,null, ['class'=>'form-control', 'placeholder'=>'All Unit Types', 'v-model'=>'unit_type', 'v-on:change'=>'getUnits()' ]) !!}
</div>
当我选择一个特定的项目时,这工作正常。然后,如果我点击所有可以说all floors
,它的作品。我需要的是当页面加载时,它调用getUnits
方法,它将执行$http.post
空输入。在后端,我处理请求的方式是,如果输入为空,则会提供所有数据。
如何在vuejs2
中做到这一点?
我的代码:http://jsfiddle.net/q83bnLrx
我已经尝试了钩,但没有工作 –
@PhillisPeters你可以把更多的代码,或它的小提琴。 – Saurabh
这里是我的完整代码https://jsfiddle.net/q83bnLrx/ –