javascript - jQuery 需要 0ms 的超时时间才能在 vue.js 组件中查找元素
问题描述
我在 vue 组件中使用 Jquery。我需要#container
通过 jquery 分配另一个函数。仅当#container
类型不是默认值时才存在。这是一个真正的性能密集型功能,所以我真的只希望它在需要时分配它。它的工作时间timeout
为 0ms。
但是有更好/更清洁的方法吗?
Vue.component( 'component', {
data: function() {
return {
typeOpt: "default"
}
},
watch: {
typeOpt: function( newVal ) {
if( newVal !== "default" )
{
setTimeout(function () { //When I remove this it will not update the e.g. background color
$('#container').css( "background", "red" );
}, 0);
}
}
},
template: `
<div class='component'>
<select v-on:change="changeType">
<option :selected="typeOpt === 'default'" value="default">Default</option>
<option :selected="typeOpt === 'type1'" value="type1">Type1</option>
</select>
<div class='mainContainer'>
<div v-if="typeOpt == 'default'">default</div>
<div v-if="typeOpt == 'type1'">
<div id='container'>content 123 ABC</div>
</div>
</div>
</div>
`,
methods: {
changeType() {
this.typeOpt = $('select').val();
}
}
});
解决方案
推荐阅读
- python-3.x - 计算算法的大 O 表示法
- javascript - setState 没有更新状态,即使在它的回调 react-native
- android - 我如何知道选定项目在微调器中的位置?
- javascript - threejs 爆炸/内爆模型
- android - 防止微调器下划线在单击期间变为粗体
- bash - Unix 查找命令忽略丢失的文件或目录
- javascript - Building a checkbox list using an asp placeholder html grid in ASP.NET C#
- css - How to use UnCSS
- java - a simple Guess letters game in java
- javascript - 如何将多个对象的值合并到一个数组对象中?