1
data() {
    return {
      perPage:2,
      swiperOption: {
        slidesPerView: perPage,
        loop: true,
        pagination: {
          el: '.swiper-pagination',
          clickable: true,
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
      },
    }
},

i just want to access perPage prop into swiperOption object

pls help me

3 Answers 3

1

You cannot directly assign a data property which is not declared. Instead you can set perPage to swiperOption in created hook

just add a created hook

data() {
    return {
        perPage:2,
        swiperOption: {
            slidesPerView: 0,
            loop: true,
            pagination: {
                el: '.swiper-pagination',
                clickable: true,
            },
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },
        },
    } 
}, 

created() {
    this.swiperOption.slidesPerView = this.perPage;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I don't think you can access an object's property while declaring it, but since data is a function you can just assign perPage to a variable:

data(){
    var perPage = 2;

    return {
      perPage:perPage,
      swiperOption: {
        slidesPerView: perPage,
        loop: true,
        pagination: {
          el: '.swiper-pagination',
          clickable: true,
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
      },
    }
}

Comments

0

Is perPage a constant or it will change depending on something? If it's a constant, there is no need to declare it inside data().

But if it changes, one way to do that is setting a watcher on it and update swiperOption with the new value everytime it changes. Something like:

watch(){
   perPage(newVal){
      this.swiperOption.slidesPerView = newVal;
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.