gpt4 book ai didi

javascript - 用 setInterval 替换 vue.js 模板

转载 作者:搜寻专家 更新时间:2023-10-30 22:19:00 25 4
gpt4 key购买 nike

我一直在尝试使用 vue.js 在给定的时间间隔内刷新 div 的内容,但到目前为止几乎没有成功。这是我目前所拥有的:

var vm = new Vue({
el: '#feature',
template: '<div id={{content}}></div>',
data:
featureList: [{
content: 'a'
}, {
content: 'b'
}, {
content: 'c'
}]
}
});

在我的 html 中,我有以下内容:

<div id="feature"></div>

我这里的方法是遍历这个数组并以给定的时间间隔替换模板中的内容。问题是我不确定该怎么做。

这是我在 data 中尝试使用数组的替代方法:使 content 成为具有 setter 函数的计算属性,在 中使用单个字符串数据。像这样:

var vm = new Vue({
el: '#feature',
template: '<div id={{content}}></div>',
data: {
content: 'a'
},
computed: {
setTemplate: {
set: function(newValue) {
var values= newValue
this.content = values
}
}
}
});

vm.setTemplate = 'c'

(jsfiddle here)

现在,我该如何离开这里?如何在特定时间间隔更改一组给定字符串的内容

最佳答案

我想你可以使用 lifecycle hooks为此,特别是 ready 钩子(Hook):

var vm = new Vue({
el: '#feature',
template: '<div id={{content}}>{{content}}</div>',
data: {
content: 'hi there',
features: ['a', 'b', 'c'],
currentIndex: 0
},

created: function() {
var self = this
setTimeout(function cycle() {
self.content = self.features[self.currentIndex++]
self.currentIndex %= self.features.length
setTimeout(cycle, 2000)
}, 2000)
}
});

参见 the updated fiddle .

关于javascript - 用 setInterval 替换 vue.js 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35133719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com