简体   繁体   English

使用Vue.js和jQuery自动完成

[英]Use both Vue.js and jQuery autocomplete

I want to use both Vue.js and jQuery to make an autocomplete field displayed dynamically. 我想使用Vue.js和jQuery来动态显示自动完成字段。

 new Vue({ el: "#el", data: { toggle: false } }); var tags = [ "ActionScript","AppleScript","Asp","BASIC","C","C++","Clojure","COBOL","ColdFusion", "Erlang","Fortran","Groovy","Haskell","Java","JavaScript","Lisp","Perl","PHP","Python", "Ruby","Scala","Scheme" ]; $("#autocompleteSearch").autocomplete({ source: tags }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script src="https://unpkg.com/vue"></script> <div id="el"> <input v-if="toggle" id="autocompleteSearch" type="search"> <button @click="toggle=!toggle">Toggle</button> </div> 

As you can see, it cannot work because of the v-if on the input. 正如您所看到的,由于输入上的v-if ,它无法工作。 If I remove the v-if it works, but it is not displayed dynamically. 如果我删除了v-if它可以工作,但它没有动态显示。 How should I do? 我应该怎么做?

There are a couple things to note here. 这里有几点需要注意。

$(selector) looks up existing DOM elements at that point in time $(selector)在那个时间点查找现有的DOM元素

For jQuery to find an element, it has to exist in the DOM when you perform the lookup. 为了让jQuery找到一个元素,当你执行查找时,它必须存在于DOM中。 There are caveats to this regarding DOM Fragments, but that's not relevant for this particular question. 关于DOM片段有一些警告,但这与这个特定问题无关。 So keep in mind that for jQuery to find something, it has to exist in the DOM. 所以请记住,为了让jQuery找到一些东西,它必须存在于DOM中。

Many jQuery UI methods initialize and transform elements 许多jQuery UI方法初始化和转换元素

When you execute a jQuery UI method, like autocomplete, jQuery changes and creates markup on the page, related to the elements you are targeting. 当您执行jQuery UI方法(如自动完成)时,jQuery会更改并在页面上创建与您要定位的元素相关的标记。 These methods can also keep internal variables related to the element as part of its initialization. 这些方法还可以将与元素相关的内部变量作为其初始化的一部分。

v-if creates and destroys elements v-if创建并销毁元素

The purpose of the v-if is to say that, an element should only exist if some conditional is true. v-if的目的是说,只有某些条件成立时,元素才应该存在。 So if the conditional is false, it will not exist in the DOM. 因此,如果条件为假,则它不会存在于DOM中。 Also, if this conditional is subject to change, the element can potentially be created and destroyed many times. 此外,如果此条件可能会发生更改,则可能会多次创建和销毁该元素。


So taking into account how v-if works, we can reflect on the two previous points. 因此,考虑到v-if如何工作,我们可以反思前两点。

  • If a v-if makes it so that an element does not exist when the jQuery selector runs, it will not find the element to initialize 如果v-if使得当jQuery选择器运行时元素不存在,它将找不到要初始化的元素
  • If a v-if destroys an element, the jQuery UI widget may not function properly because it was related to the element that it initialized, and not a future element that is created to replace the previously created element. 如果v-if销毁元素,则jQuery UI小部件可能无法正常运行,因为它与其初始化的元素相关,而不是为替换先前创建的元素而创建的未来元素。

With that in mind, when working with Vue and jQuery you have to keep in mind the needs of both, and how they may conflict with each other, such as in this case. 考虑到这一点,在使用Vue和jQuery时,您必须牢记两者的需求,以及它们如何相互冲突,例如在这种情况下。 The use of v-if is not necessarily the best directive to use when the element it controls is also used as part of a state in another library, such as jQuery. 使用v-if不一定是当它控制的元素也被用作另一个库中的状态的一部分时使用的最佳指令,例如jQuery。

In order to fix that, you can choose to use another directive, such as v-show or v-hide . 为了解决这个问题,您可以选择使用其他指令,例如v-showv-hide

v-show and v-hide are two sides to the same coin. v-showv-hide是同一枚硬币的两面。 They determine if an element should be visible or not to the user. 它们确定元素是否应该对用户可见。 The distinct difference between these two directives and v-if is that v-show and v-hide do not remove the element from the DOM. 这两个指令和v-if之间的明显区别是v-showv-hide不会从DOM中删除该元素。 They simply change the display state of the element. 它们只是改变元素的显示状态。

So in relation to another library that relies on an element existing and using that element as part of some state management, this is great! 因此,与依赖于现有元素并将该元素作为某些状态管理的一部分使用的另一个库相关,这非常棒! You can control when your users see an element, and also potentially avoid conflicting with the secondary library, such as jQuery UI. 您可以控制用户何时查看元素,还可以避免与辅助库冲突,例如jQuery UI。


Having said that, this does not mean you should not use v-if . 话虽如此,这并不意味着你不应该使用v-if v-if is still useful for elements that should not exist at certain points in time. v-if对于在某些时间点不应存在的元素仍然有用。 All this means is that you should be aware of the situation you are making, and consider any other logic in your application that may rely on those elements in order to minimize the chances of creating a bug. 所有这些意味着您应该了解自己所处的情况,并考虑应用程序中可能依赖这些元素的任何其他逻辑,以便最大限度地减少创建错误的可能性。


TL;DR; TL; DR;

Here is the solution to your problem: 以下是您的问题的解决方案:

 new Vue({ el: "#el", data: { toggle: false } }); var tags = [ "ActionScript","AppleScript","Asp","BASIC","C","C++","Clojure","COBOL","ColdFusion", "Erlang","Fortran","Groovy","Haskell","Java","JavaScript","Lisp","Perl","PHP","Python", "Ruby","Scala","Scheme" ]; $("#autocompleteSearch").autocomplete({ source: tags }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script src="https://unpkg.com/vue"></script> <div id="el"> <!-- Just replace v-if by v-show --> <input v-show="toggle" id="autocompleteSearch" type="search"> <button @click="toggle=!toggle">Toggle</button> </div> 

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

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