【问题标题】:Vue js get parent property AFTER it's ready method has ranVue js在就绪方法运行后获取父属性
【发布时间】:2015-12-07 04:17:18
【问题描述】:

我知道我可以使用inherit 允许子组件获取其父组件的属性,但问题是……我需要在父准备方法运行后获取属性。为了获取在 ready 方法中设置的父组件的宽度和高度,我到处都有这个问题。

var Carousel = Vue.component('carousel', {
	template: '#carousel',

	replace: true,

	data: function() {
		return {
			current: 1,
			slideWidth: 600,
			count: 6,
			style: {
				width: 600,
				viewport: 600,
				marginLeft: 0
			}
		}
	},
    
    computed: {
		styles: function() {
			return {
				width: this.style.width + 'px',
				marginLeft: this.style.marginLeft + 'px'
			}
		},

		viewport: function() {
			return {
				width: this.style.viewport + 'px'
			}
		},

		rounds: Math.floor(this.count / this.show)
	},

	props: ['show', 'slideMargin'],

	ready: function() {
		this.slideWidth = $(this.$el).width();
		this.count = this.$children.length;
		this.style.width = (this.slideWidth * this.count) + (this.slideMargin * (this.count * 2));
		this.style.viewport = (this.slideWidth * this.show) + (this.slideMargin * (this.show * 2));
	}
});

var CarouselSlide = Vue.component('carouselslide', {
	template: '#slide',

	replace: true,

	data: function() {
		return {
			style: {
				width: 200
			}
		}
	},
    
    computed: {
		styles: function() {
			return {
				width: this.style.width + 'px'
			}
		}
	},

	ready: function() {
		this.style.width = this.$parent.$get('slideWidth');
	}
});

new Vue({
	el: '#testimonials'
});
#testimonials {
    width: 50%;
    margin: 0 auto;
    position: relative;
    float: left;
    min-height: 1px;
    padding-left: 1.25rem;
    padding-right: 1.25rem;
    display: block;
}

h3 {
    color: #b50937;
    text-transform: uppercase;
    margin: 0 0 20px;
    font-size: 1.75rem;
}

.carousel {
    position: relative;
    overflow: hidden;
}

.carousel .slides {
    overflow: hidden;
    margin: 0 auto;
}

.carousel .slides .viewport {
    overflow: hidden;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    transition: all 800ms cubic-bezier(0.77, 0, 0.175, 1);
    transition-timing-function: cubic-bezier(0.77, 0, 0.175, 1);
}

.carousel .slides .slide {
    position: relative;
    display: block;
    float: left;
    margin: 0 2px;
}

.carousel .slides .slide .box {
    background-color: #d1dbe5;
    box-sizing: border-box;
    padding: 15px 20px;
}

.view-all {
    text-align: right;
}

.arrows {
    position: relative;
    text-align: right;
    width: 100%;
}

.arrows .arrow {
    background-color: #d3d3d3;
    color: #fff;
    padding: 2px 13px;
    position: static;
    transition: 0.4s ease-in-out;
    display: inline-block;
    cursor: pointer;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="http://cdnjs.cloudflare.com/ajax/libs/vue/0.12.13/vue.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="x-template" id="carousel">
    <div class="carousel">
        <div class="slides" v-style="viewport">
            <div class="viewport" v-style="styles">
                <content></content>
            </div>
        </div>
        <div class="view-all"><a href="#" title="View all">View all <i class="fa fa-angle-double-right"></i></a></div>
        <div class="arrows">
            <div class="arrow prev" v-on="click: prevSlide"><i class="fa fa-chevron-left"></i></div>
            <div class="arrow next" v-on="click: nextSlide"><i class="fa fa-chevron-right"></i></div>
        </div>
    </div>
</script>
<script type="x-template" id="slide">
    <div class="slide" v-style="styles">
        <content></content>
    </div>
</script>

<section id="testimonials">
    <h3>What People Are Saying About Us</h3>
    <carousel show="1" slide-margin="2">
        <carouselslide>
            <div class="phrase">
                <div class="box">
                    We were looking to upgrade our equipment when we came across Ventrac. It was &quot;wow&quot; for 
                    us, why did we suffer for the first six years with these other pieces of equipment when we could of had this.
                </div>
            </div>
        </carouselslide>
        <carouselslide>
            <div class="phrase">
                <div class="box">
                    We were looking to upgrade our equipment when we came across Ventrac. It was &quot;wow&quot; for 
                    us, why did we suffer for the first six years with these other pieces of equipment when we could of had this.
                </div>
            </div>
        </carouselslide>
    </carousel>
</section><!-- END #TESTIMONIALS -->

这是我的 Vue 代码,因为它是唯一相关的部分,尽管您可以看到我在楼上遇到的问题^^(sn-p)

var Carousel = Vue.component('carousel', {
    template: '#carousel',

    replace: true,

    data: function() {
        return {
            current: 1,
            slideWidth: 600,
            count: 6,
            style: {
                width: 600,
                viewport: 600,
                marginLeft: 0
            }
        }
    },

    computed: {
        styles: function() {
            return {
                width: this.style.width + 'px',
                marginLeft: this.style.marginLeft + 'px'
            }
        },

        viewport: function() {
            return {
                width: this.style.viewport + 'px'
            }
        },

        rounds: Math.floor(this.count / this.show)
    },

    props: ['show', 'slideMargin'],

    ready: function() {
        this.slideWidth = $(this.$el).width();
        this.count = this.$children.length;
        this.style.width = (this.slideWidth * this.count) + (this.slideMargin * (this.count * 2));
        this.style.viewport = (this.slideWidth * this.show) + (this.slideMargin * (this.show * 2));
    }
});

var CarouselSlide = Vue.component('carouselslide', {
    template: '#slide',

    replace: true,

    data: function() {
        return {
            style: {
                width: 200
            }
        }
    },

    computed: {
        styles: function() {
            return {
                width: this.style.width + 'px'
            }
        }
    },

    ready: function() {
        this.style.width = this.$parent.$get('slideWidth');
    }
});

new Vue({
    el: '#testimonials'
});

我需要从父级获取它的原因是因为 clientWidth 包含我不能的填充。所以我不能在数据或计算属性数据中做$(this.$el).width(),因为$el 尚不可用。从我的孩子那里,我需要在 ready 方法触发后获得这个宽度。

感谢您的任何见解。

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    没有仔细查看您的代码,我首先想到的是在孩子中获取父数据是:

    computed: {
        val: this.$parent.val;
    }
    

    但我不确定这是否适合你。或者,您可以将您父母的 ready 方法更改为 compiled 以便它在孩子之前运行。

    【讨论】:

      猜你喜欢
      • 2010-12-05
      • 1970-01-01
      • 2017-11-03
      • 2019-08-03
      • 2020-01-25
      • 2020-10-02
      • 2016-10-17
      • 2017-04-21
      • 1970-01-01
      相关资源
      最近更新 更多