Answer the question
In order to leave comments, you need to log in
Why do routing errors occur?
There is a block component with product cards, which accepts an array of products in props, respectively, and generates product cards using v-for:
<template>
<div class="block">
<Item
v-for="item in items"
:key="item.id"
:item="item"
:bigImg="item.imgCatalogBig"
:variantsBig="item.variantsBig"
/>
</div>
</template>
<script>
import Item from './Item'
export default {
name: 'Block',
props: {item: Array},
components: {
Item
}
}
</script>
{
"id": 1,
"name": "name",
"imgCatalogBig": "name-catalog-big.jpg",
"imgSmall": ["...jpg.","....jpg","....jpg","....jpg"],
"variantsBig": ["....jpg","....jpg","....jpg","....jpg"],
"title": "TITLE",
}
<article class="catalog__item" @mouseover="stopAnim" @mouseleave="startAnim">
<div class="catalog__item-img">
<div class="catalog__item-imgBig">
<img
:src="require('@/img/Catalog/'+imgDefault)"
:alt="item.name"
>
</div>
<div
v-if="item.imgSmall.length"
class="catalog__item-variants"
>
<div
v-for="(img, index) in item.imgSmall"
:key="index"
:data-id="index+1"
class="catalog__item-imgSmall"
@click="changeBigImg(index)"
ref="arr"
>
<img :src="require('@/img/Catalog/'+img)" :alt="index">
</div>
</div>
</div>
<div class="catalog__item-content">
<h3 class="catalog__item-title" v-html="item.title"></h3>
<router-link
:to="{name: 'Product', params: {id: item.id}}"
class="catalog__item-btn"
>Подробнее</router-link>
</div>
</article>
export default {
name: 'CatalogItem',
props: { item: Object, bigImg: String, variantsBig: Array },
data () {
return {
imgDefault: this.bigImg,
idx: null,
change: null
}
},
methods: {
changeBigImg (index) {
this.imgDefault = this.variantsBig[index]
this.$refs.ar.find(el => el.classList.contains('active')).classList.remove('active')
this.$refs.ar[index].classList.add('active')
this.idx = index
},
stopAnim () {
if (this.variantsBig) {
clearInterval(this.change)
}
},
startAnim () {
if (this.variantsBig) {
this.change = setInterval(() => {
if (this.idx === this.variantsBig.length) {
this.idx = 0
}
this.changeBigImg(this.idx)
this.idx++
}, 2000)
}
}
},
mounted () {
if (this.variantsBig) {
this.$refs.ar[0].classList.add('active')
this.idx = this.$refs.ar.indexOf(this.$refs.ar.find(el => el.classList.contains('active')))
this.startAnim()
}
}
}
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
...,
{
path: '/product/:id',
name: 'Product',
component: () => import('../views/Product'),
props: true
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes,
scrollBehavior (to, from, savedPosition) {
return { x: 0, y: 0 }
}
})
import Vue from 'vue'
import App from './App.vue'
import router from './router'
Vue.config.productionTip = false
new Vue({
router,
render: h => h(App)
}).$mount('#app')
Answer the question
In order to leave comments, you need to log in
// CatalogItem.vue
export default {
beforeDestroy () {
this.stopAnim();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question