javascript - 如何使用队列方法制作动态列表
问题描述
我正在处理 vue.js 呈现动态列表,当用户单击按钮时,相应输入字段中的链接会呈现,然后我再次将链接粘贴到输入字段中并单击添加它会将值添加到我没有的第二个索引'不想让我知道我有使用queue
概念,但在 ui 上我不知道如何实现它
我做了什么
new Vue({
el: '#app',
data() {
return {
anchorTags: [{
url: '',
value: ''
}]
}
},
methods: {
validateYouTubeUrl() {
var url = document.getElementById('youtubeUrl').value
this.anchorTags.push({
url: url,
value: url
});
}
}
})
.as-console-wrapper { max-height: 3em !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.9/vue.js"></script>
<div id=app>
<div class="container-fluid row">
<div class="form-group col-xs-12 col-sm-12 col-md-8 col-lg-8 col-xl-8" id="first">
<div class="input-group">
<input type="text" class="form-control" id="youtubeUrl" placeholder="Paste link here...">
<div class="input-group-append">
<button class="btn btn-primary" type="button" @click="validateYouTubeUrl">Add</button>
</div>
</div>
<hr>
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-4 col-lg-4 col-xl-4" id="second">
<h3 class="form-control">Playlist</h3>
<hr>
<ul class="list-group">
<li v-for="(anchorTag, k) in anchorTags" :key="k" class="list-group-item">
<a :href="anchorTag.url" @click.prevent="playVideo($event)">{{anchorTag.value}}</a>
<span v-if="anchorTag.url!==''"><i class="fas fa-trash-alt"></i></span>
</li>
</ul>
</div>
</div>
</div>
所以我想做的是
像 FIFO 这样的 quee 那样做,当我输入一些东西时,它在顶部,我再次输入最新的东西应该在顶部
解决方案
此外,我允许自己进行一些额外的更改,以便给您一个答案(我使用了v-model
):
new Vue({
el: "#app",
data() {
return {
youtubeUrl: null,
anchorTags: []
};
},
methods: {
validateYouTubeUrl() {
this.anchorTags.push({
url: this.youtubeUrl,
value: this.youtubeUrl
});
}
},
computed: {
anchorList() {
return this.anchorTags.slice().reverse();
}
}
});
.as-console-wrapper {
max-height: 3em !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id=app>
<div class="container-fluid row">
<div class="form-group col-xs-12 col-sm-12 col-md-8 col-lg-8 col-xl-8" id="first">
<div class="input-group">
<input type="text" class="form-control" v-model="youtubeUrl" placeholder="Paste link here...">
<div class="input-group-append">
<button class="btn btn-primary" type="button" @click="validateYouTubeUrl">Add</button>
</div>
</div>
<hr>
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-4 col-lg-4 col-xl-4" id="second">
<h3 class="form-control">Playlist</h3>
<hr>
<ul class="list-group">
<li v-for="(anchorTag, k) in anchorList" :key="k" class="list-group-item">
<a :href="anchorTag.url" @click.prevent="playVideo($event)">{{anchorTag.value}}</a>
<span v-if="anchorTag.url!==''"><i class="fas fa-trash-alt"></i></span>
</li>
</ul>
</div>
</div>
</div>
如果这不是您的意思,请编辑您的问题并尝试以更病态的方式描述它。
推荐阅读
- vue.js - Vuex状态数组在变异时转换代理对象
- visual-studio - 部署 SSIS 包时 Visual Studio 2012 崩溃
- r - 从 Reshape 转换为 Reshape 2 时融化不起作用
- c# - 当我将我的程序放入不同的方法中,并将它们组合成一个循环时,它们不能正确地相互读取。有谁知道为什么?
- flutter - Flutter,如何从子级更改当前标签栏视图或页面或索引
- google-cloud-platform - Google Cloud IoT - 注册表中所有设备的单个 MQTT 客户端实例
- azure - 如果用户具有 MFA,则 Azure AD B2C Graph API 检索
- java - 如何拆分带有圆括号的字符串?
- r - rstudio数据表我想改造数据库
- python - 减少 cpu 的 python 监控任务使用的工作负载