0

I am trying to add a autocomplete field by using jQuery UI to my Vue.js project.

Here is my approach so far. In my template I have:

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags" />
</div>

In my script I have:

   <script>
  import httpVueLoader from 'http-vue-loader'
  import $ from 'jquery'
  import 'jquery-ui';
  
  export default {
    name: 'ResultList',
    components: {
      'ResultItem': httpVueLoader(window.__env.config.HTML_TEMPLATE)
    },
    props: {
      results: Object,
      showAllResults: Boolean,
     },
    watch: {
      results: function () {
        this.refresh_key++
      }
    },
    data: function () {
      return{
        refresh_key:0
      }
    },
    computed: {
      num_results: function(){
        if(this.showAllResults){
            return window.__env.config.NUM_ALL_RESULTS;
        } else {
            return window.__env.config.NUM_PREVIEW_RESULTS;
        }
      }
    },
    mounted: function() {

    
       $(document).ready(function(){
           $(function(){
                console.log("Version would be: " + $().jquery);

                var availableTags = [
              "ActionScript",
              "AppleScript",
              "Asp",
              "BASIC",
              "C",
              "C++",
              "Clojure",
              "COBOL",
              "ColdFusion",
              "Erlang",
              "Fortran",
              "Groovy",
              "Haskell",
              "Java",
              "JavaScript",
              "Lisp",
              "Perl",
              "PHP",
              "Python",
              "Ruby",
              "Scala",
              "Scheme"
            ];
            $("#tags") . autocomplete ({
              source: availableTags
            });

          });
      });


    
   
    }

    
  }
</script>

What went wrong? I am constantly getting the error:

.autocomplete' is undefined) (2) Any help is appreciated! I wrapped the autocomplete function into a document.ready function but it doesn't help.

When I check separately if the autocomplete function exists it throws that it doesn't.

if ($.fn.autocomplete) {
              console.log("autocomplete exists");
          } else {
            console.log("autocomplete not found");
          }
Simon Giesen
  • 169
  • 1
  • 2
  • 12
  • Why do you have a space there? Tried with `$("#tags").autocomplete` (with no extra-space)? Also, why do you use jQuery with Vue? Is it anyhow mandatory in your case, you don't want to do that in Vue? – kissu Sep 19 '22 at 14:03
  • Yes, I tried with no extra-space. And yes, I have to use jQuery as well. – Simon Giesen Sep 19 '22 at 14:05
  • Welcome to Stack Overflow. The error indicates that the jQuery UI Library has not loaded. Please review your Console and the Source to ensure that jQuery UI Library is loaded properly in the HEAD. – Twisty Sep 19 '22 at 21:09
  • Yes, the jQuery UI Library was installed via npm and imported. Any other hints for me? – Simon Giesen Sep 20 '22 at 09:11
  • Did you installed it like that? https://stackoverflow.com/a/68414170/8816585 – kissu Sep 20 '22 at 09:47
  • Yes! I installed it correctly. – Simon Giesen Sep 20 '22 at 14:50
  • Why are you importing it in the file here so? – kissu Sep 20 '22 at 15:47
  • You can import it globally or in the component file – Simon Giesen Sep 21 '22 at 06:08
  • No point into importing it locally. If you use jQuery, just go for the global import. Otherwise, don't use it. The middle ground seems to be quite too much engineered. – kissu Sep 21 '22 at 08:42

0 Answers0