最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue - Setting the width of a component dynamically - Stack Overflow

programmeradmin1浏览0评论

I can't figure out how to dynamically set the width of a ponent.

I am using the ponent 'vue-burger-menu' -> .

To set the width one needs to set the prop width to a number. As per the example below:

<Slide
      ref="slideToggle"
      disable-esc
      class="slideToggle"
      width="470"
      right
      :burger-icon="false"
      :cross-icon="false"
      disable-outside-click
    >

It then sets a helper class - bm-menu width to the width. I have spent quite a while trying to figure out how to either set the prop's attribute dynamically or dynamically update the style.

For example I tried setting: this.$refs.slideToggle.$attrs.width = 1000 to no avail.

I can't bind a style to the bm-menu class as it's not visible.

How do I set the width so on the click of a button it changes (as per the button example below)?

Thanks for your help!

setDrawWidth() {
        this.lineDrawWidth = "200px";
    }

I can't figure out how to dynamically set the width of a ponent.

I am using the ponent 'vue-burger-menu' -> https://github./mbj36/vue-burger-menu.

To set the width one needs to set the prop width to a number. As per the example below:

<Slide
      ref="slideToggle"
      disable-esc
      class="slideToggle"
      width="470"
      right
      :burger-icon="false"
      :cross-icon="false"
      disable-outside-click
    >

It then sets a helper class - bm-menu width to the width. I have spent quite a while trying to figure out how to either set the prop's attribute dynamically or dynamically update the style.

For example I tried setting: this.$refs.slideToggle.$attrs.width = 1000 to no avail.

I can't bind a style to the bm-menu class as it's not visible.

How do I set the width so on the click of a button it changes (as per the button example below)?

Thanks for your help!

setDrawWidth() {
        this.lineDrawWidth = "200px";
    }
Share Improve this question asked Apr 29, 2020 at 20:38 tbowdentbowden 1,0581 gold badge23 silver badges47 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You just need binding which uses : before props:

<Slide
      ref="slideToggle"
      disable-esc
      class="slideToggle"
      :width="width"
      right
      :burger-icon="false"
      :cross-icon="false"
      disable-outside-click
>

And then in your data in js part:

export default {
     data:() => ({   width: '470'  }),
}

Now you just need to change width variable. For example:

export default {
     data:() => ({   width: '470'  }),
     methods:{
          changeWidth(){
               this.width = '1000';
          }
     }
}

You can read more about binding variables from the doc: Vue Props

Listen on window width event:

data: () => ({
  width: 470
})
created() {
  window.addEventListener("resize", this.changeWidth);
},
destroyed() {
  window.removeEventListener("resize", this.changeWidth);
},
methods: {
  changeWidth(e) {
    const screenWidth = window.innerWidth;
    this.width = screenWidth
  }
}

and set width in Slide ponent:

<Slide
      ref="slideToggle"
      disable-esc
      class="slideToggle"
      :width="width"
      right
      :burger-icon="false"
      :cross-icon="false"
      disable-outside-click
    >
发布评论

评论列表(0)

  1. 暂无评论