【发布时间】:2020-06-20 06:48:49
【问题描述】:
我正在构建一个 Trivia 应用程序,并且我有一个 <h1> 元素,该元素每 10 秒更改一次。我想将单选按钮组组件的宽度(b-form-group 是Bootstrap-Vue component)与<h1> 元素的宽度相匹配。像这样的:
每当question 的值发生变化时,我都尝试使用watchers 更新buttonWidth。但是,使用此代码,按钮组的宽度似乎与前一个问题的宽度匹配,而不是当前问题。我还尝试了其他属性,例如updated、mounted、created。我只是无法让它正常工作。有没有更简单的方法来匹配两个元素的宽度?
<template>
<div>
<div id="question">
<h1 ref="quest">{{ question }}</h1>
</div>
<b-form-group>
<b-form-radio-group :style=getWidth
ref="opts"
:options="options"
buttons
stacked
button-variant="outline-primary"
size="lg"
name="radio-btn-stacked"
></b-form-radio-group>
</b-form-group>
</div>
</template>
在我的脚本下我有:
export default {
props: ['question', 'options'],
data() {
return {
buttonWidth: '0 px',
};
},
methods: {
},
watch: {
question() {
// console.log('Updating button width to ', this.$refs.quest.clientWidth);
this.buttonWidth = `width: ${this.$refs.quest.clientWidth}px;`;
},
// immediate: true,
},
computed: {
getWidth() {
return this.buttonWidth;
},
},
};
【问题讨论】:
标签: vue.js vue-component bootstrap-vue