Vue.js 在组件渲染后执行事件触发器
Vue.js does an event trigger after component has been rendered?
我在 Vue.js 中有一些自定义组件。在我拥有的一个组件中,有一个选择列表,我想将其呈现为"已选择"选择框。我将其与 jQuery 函数一起使用 $("select").chosen()
.
<template v-for="upload in uploads">
<new-upload-container :index="$index" :upload="upload" v-if="isNewUpload"></new-upload-container>
<existing-upload-container :index="$index" :upload="upload" v-if="isExistingUpload"></existing-upload-container>
</template>
将数据添加到 Vue 实例中的uploads
绑定数组后,视图会使用该组件的实例进行更新。不幸的是,当我尝试在选择字段上实例化Chosen
时,它不起作用。
我不确定在将项目添加到 DOM 实际更新的uploads
绑定数组后是否需要很短的时间。
<template id="existing-upload-container">
<select name="beats[@{{ index }}][existing_beat]" class="existing-beats">
<option selected="selected" value="">-- Select a linked beat --</option>
@foreach ($beats as $beat)
<option value="{{$beat->id}}">{{$beat->name}}</option>
@endforeach
</select>
</template>
是否有在组件完全渲染后触发的事件?
您可以在组件中尝试两件事,具体取决于哪一种适用于您的包。 在 Vue 对象中:
{
ready:function(){
// code here executes once the component is rendered
// use this in the child component
},
watch: {
uploads:function(){
//code here executes whenever the uploads array changes
//and runs AFTER the dom is updated, could use this in
//the parent component
}
}
}
正确的方法是自定义指令:
<select v-chosen name="beats[@{{ index }}][existing_beat]" class="existing-beats">
//insert/require() the following before the creation of your main Vue instance
Vue.directive("chosen",{
bind: function(){
$(this.el).chosen();
}
})
编辑:指令语法在 Vue 2.* 中更改:
Vue.directive("chosen",{
bind: function(el){
$(el).chosen();
}
})
使用mounted
回调并检查代码中的数据状态。
一个很好的方法是将 Chosen 插件包装在 Vue 组件中,如此处详细描述。
相关文章:
- 使用react js在跨域上执行事件javascript
- 如何使用cURL或其他方式在点击时执行事件
- javascript执行事件顺序
- While Scrolling:如果scrollTop()等于或达到一个值,则执行事件
- 如何使用gulp-jslint-simple成功执行事件
- 如果初始事件返回 false,则停止执行事件侦听器
- 我怎么能等待在javascript中执行事件
- 使用 AJAX 在 jQuery 和 C# 之间传递变量和执行事件
- 执行 C# 事件处理程序,然后执行该 js 函数
- Vue.js 在组件渲染后执行事件触发器
- 在事件的自然行为之前执行事件
- javascript更改类没有't执行事件
- 如何按顺序执行事件函数
- 在worker上执行事件对象.Onmessage从不包含任何有趣的数据之外的东西
- 选项更改,执行事件javascript
- 使用node.js在进程终止时执行事件
- 从此特定父类查找兄弟类以执行事件
- 使用jquery创建弹出窗口并执行事件
- Javascript执行事件点击
- 单击除一个元素之外的所有内容时执行事件