简体   繁体   English

如何在 vue js 中访问组件样式数组?

[英]How to access component style array in vue js?

I want to set a button's min-width to a specific value which would be it's width after being rendered to the HTML DOM;我想将按钮的最小宽度设置为特定值,该值将是呈现到 HTML DOM 后的宽度; so that when the button is clicked and a spinner appears at center, it's width remains constant.这样当单击按钮并且微调器出现在中心时,它的宽度保持不变。

In VanillaJS we can access like element.style.minWidth , but how to do so in VueJS component without using $refs while using component?在 VanillaJS 中,我们可以像element.style.minWidth一样访问,但是如何在 VueJS 组件中这样做而不使用 $refs 而使用组件呢?

My Vue Component我的 Vue 组件

Vue.component('submit-btn', {
    template: `
        <button v-on="clickListeners" class="btn btn-sm btn-primary font-blinker text-center" :style="styling">
            <span v-if="loading">
                <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
            </span>
            <span v-else>{{title}}</span>
        </button>
    `,
    data: function () {
        return {
            minWidth: '' 
        }
    },
    props: ['title', 'loading'],
    computed: {
        clickListeners: function () {
            var vm = this
              return Object.assign({},
                this.$listeners,
                {
                    click: function (event) {
                        vm.$emit('click', event)
                    }
                }
            )
        },
        styling: function () {
            return {
                minWidth: this.minWidth
            }
        }
    },
    created() {
        this.minWidth = this.$em.style.minWidth // Want to access width here and then set styling.
    }
})

A Vue 2.0 specific way would be done using vue's ref directive.使用vue 的 ref 指令可以完成 Vue 2.0 特定的方式。

Check out this JSFiddle example: https://jsfiddle.net/zt4j5o2f/查看这个 JSFiddle 示例: https://jsfiddle.net/zt4j5o2f/

All you need to do is add the ref="mybutton" attribute to the button, and then you should be able to access it using this.$refs.mybutton您需要做的就是将ref="mybutton"属性添加到按钮,然后您应该能够使用this.$refs.mybutton访问它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM