Vue.js 组件渲染后是否触发事件?
Vue.js does an event trigger after component has been rendered?
我在 Vue.js 中有一些自定义组件。在我拥有的组件之一中有一个 select 列表,我想将其呈现为 Chosen select 框。我将其与 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
绑定数组后,视图会更新组件的实例。不幸的是,当我尝试在 select 字段上实例化 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
}
}
}
正确的方法是 custom directive:
<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();
}
})
执行此操作的一个好方法是将 Chosen 插件包装在 Vue 组件中,如 here 中所述。
使用 mounted
回调并检查代码中的数据状态。
我在 Vue.js 中有一些自定义组件。在我拥有的组件之一中有一个 select 列表,我想将其呈现为 Chosen select 框。我将其与 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
绑定数组后,视图会更新组件的实例。不幸的是,当我尝试在 select 字段上实例化 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
}
}
}
正确的方法是 custom directive:
<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();
}
})
执行此操作的一个好方法是将 Chosen 插件包装在 Vue 组件中,如 here 中所述。
使用 mounted
回调并检查代码中的数据状态。