简体   繁体   English

vue.js,主要el外部的v模型

[英]vue.js, v-model outside main el

Is there any way to bind the v-model input from outside of the main el? 有什么方法可以绑定来自主要el外部的v模型输入? I will use an example from the vue website: 我将使用Vue网站上的示例:

new Vue({
   el: '#demo',
   data: {
      message: 'Hello Vue.js!'
   }
})


<div id="demo">
   <p>{{message}}</p>
   <input v-model="message">
</div>

<input v-model="message"> // <- how to bind this

Almost anything can be done if you don't mind hacky solutions! 如果您不介意骇人的解决方案,几乎可以做任何事!

One way to make this work using jQuery, is to start the "message" input inside the Vue object's scope, then move it after the object is loaded. 使用jQuery进行此工作的一种方法是,在Vue对象的作用域内启动“消息”输入,然后在对象加载后将其移动。

new Vue({
   el: '#demo',
   data: {
      message: 'Hello Vue.js!'
   },
   ready: function(){
      $(this.$els.msg).appendTo('#new-location');
   }
})


<div id="demo">
   <p>{{message}}</p>
   <input v-model="message" v-el:msg />
</div>
<div id="new-location">
<!-- target location -->
</div>

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

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