how to make bootstrap4 collapse work in vue?

前端 未结 3 974
长发绾君心
长发绾君心 2021-01-27 18:19

I am using bootstrap 4.3.1 and vue@2.6.10

I have this menu (is using collapse - and I don`t want to use JQuery):

 
  • <
  • 3条回答
    •  夕颜
      夕颜 (楼主)
      2021-01-27 18:42

      No jQuery or bootstrap-vue ...

      Create a function in the Component to handle the normal Bootstrap class and timing logic...

        data() {
          return {
            classArr: ['collapse'],
            styleObj: {}
          };
        },
        methods: {
          toggleCollapse(ref) {
              let show = this.classArr.indexOf('show')>-1?false:'show'
              this.classArr = ['collapsing']
              setTimeout(() => {
                  if (show){
                      let height = this.$refs[ref].firstChild.clientHeight + 'px';
                      this.styleObj = { height }
                  }
                  else {
                      this.styleObj = {}  
                  }
              }, 10)
              setTimeout(() => {
                  this.classArr = ['collapse', show]
              }, 340)
          }
        }
      

      In the component template, bind the class and style attrs to the data manipulated by the method. The ref of the specific collapse is passed in to the method...

        
      

      https://www.codeply.com/p/GA5CaNMzmc

      EDIT: I updated the demo to make it scaleable for multiple collapses

    提交回复
    热议问题