这次给大家带来vue2.x双向绑定封装详解,vue2.x双向绑定封装的注意事项有哪些,下面就是实战案例,一起来看一下。
上代码:
nbsp;html> <title>vue select2 封装</title> <link> <script></script> <script></script> <script></script> <style> .content{ text-align: center; padding:50px; } .content *{ text-align: left; } .select{ width: 350px; } </style> <p> <select></select> <br> <span>结果:{{ selectValue }}</span> </p> <script> Vue.directive('select2', { inserted: function (el, binding, vnode) { let options = binding.value || {}; $(el).select2(options).on("select2:select", (e) => { // v-model looks for // - an event named "change" // - a value with property path "$event.target.value" el.dispatchEvent(new Event('change', { target: e.target })); //说好的双向绑定,竟然不安套路 }); }, update: function(el, binding, vnode) { $(el).trigger("change"); } }); var vueApp = new Vue({ el: "#vue-example", data: { selectValue: '你还没有选值', options: { data: [ { id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' } ] } } }); </script>
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上就是vue2.x双向绑定封装详解的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号