gpt4 book ai didi

javascript - 如何在 vue.js 2 上循环模板标签?

转载 作者:行者123 更新时间:2023-11-30 15:33:20 24 4
gpt4 key购买 nike

我的代码是这样的:

<template>
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
@for(i=0;i<total;i++)
<li><a href="#">{{i}}</a></li>
@endfor
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</template>
<script>
export default{
props:['total', 'data'],
created: function(){
console.log(this.$store.state.product.next_page);
},
computed:{
next:function(){
return this.$store.state.product.next_page
}
}
}
</script>

我试试代码。但它不起作用。

存在错误:

[Vue warn]: Property or method "i" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

例如变量total = 5,则显示如下:

<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>

我该怎么做?

最佳答案

来自 Range v-for

<li v-for="i in total">
<a href="#">{{i}}</a>
</li>

只需确保total 是整数而不是字符串。例如,而不是

<component-name total="5"...

使用

<component-name :total="5" ...

参见 https://v2.vuejs.org/v2/guide/components.html#Literal-vs-Dynamic

关于javascript - 如何在 vue.js 2 上循环模板标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41947497/

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