简体   繁体   English

为什么当我使用Vue.js时搜索不起作用?

[英]Why doesn't my search work when I use Vue.js?

I'm making a simple search engine for my Laravel 5 app. 我正在为Laravel 5应用程序创建一个简单的搜索引擎。 As it's only small, I'm just using a FULLTEXT index on my main table. 因为它很小,所以我只在主表上使用FULLTEXT索引。 I'm trying to use Vue.js to avoid a page reload. 我正在尝试使用Vue.js避免页面重新加载。

In my SearchController I simply have: 在我的SearchController中,我简单地拥有:

public function search(Request $request)
    {
        $search = $request->search;

        $results = Product::whereRaw("MATCH (product_name, product_description) AGAINST (? IN BOOLEAN MODE)", array($search))->get(array('product_name','slug'));

        return $results;
    }

Javascript: 使用Javascript:

getResults: function(e){
        e.preventDefault();
        this.$http.get('api/search', function(search){
            this.$set('searchResults', search);
        });
}

and the form: 以及形式:

{!! Form::open(['v-on' => 'submit: getResults($event)', 'method' => 'get', 'action' => 'SearchController@search']) !!}
        <div class="form-group">
            {!! Form::label('search','Search:') !!}
            {!! Form::text('search', null, ['class' => 'form-control', 'v-model' => 'search']) !!}
        </div>

        <div class="form-group">
            {!! Form::submit('Submit', ['class' => 'form-control btn btn-primary']) !!}
        </div>

{!! Form::close() !!}

If I remove Vue.js from the equation, the form submits and I get a nice JSON object of search results. 如果我从等式中删除Vue.js,则会提交表单,并且会得到一个不错的搜索结果JSON对象。 If I put it through Vue.js, I just get an empty object. 如果通过Vue.js进行输入,则只会得到一个空对象。 I'm using the same search term in both cases. 在两种情况下,我都使用相同的搜索词。

What am I doing wrong? 我究竟做错了什么?

EDIT: I've logged search to the console in my getResults function, and it's empty, which explains things a little. 编辑:我已经将search记录到我的getResults函数中的控制台中,并且它为空,这在getResults解释了事情。 But why is it empty? 但是为什么它是空的? If I echo out the data search is populated as I type by Vue's 2-way binding, so search definitely is not empty... 如果我在通过Vue的2向绑定输入时回显了数据搜索,那么search肯定不是空的...

It's because you don't send anything with vueJS. 这是因为您不使用vueJS发送任何内容。 No data are sent with the ajax request. 没有数据与ajax请求一起发送。 You need to do something like that. 您需要执行类似的操作。

this.$http.get('api/search', this.search , function(search){
        this.$set('searchResults', search);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM