【发布时间】:2021-11-11 01:09:41
【问题描述】:
我有一个具体的问题。 我只想共享一个变量,其中包含从组件 1 到组件 2 的主数组的 de 数组长度。该变量显示在页面挂载时在方法中计算,所以我使用 mount() 来调用方法函数。 这部分没问题,所以我必须将此变量发送给子组件,以便它可以在标签上使用。
解释一下:我确实有一个组件,我们将它命名为“progressBar”,它的代码很简单,就像在 bootstrap-vue 上一样:
<template>
<div id="main-progress">
<div class="progress-wrapper">
<b-progress
v-model="value"
max="100"
/>
</div>
</div>
</template>
<script>
import { BProgress } from 'bootstrap-vue'
export default ({
components: {
BProgress,
},
data(){
return {
value: 0,
}
}
})
</script>
然后我在组件 1 上有一个名为“trailCard”的主数组
data(){
return{
trail_card: [
{
title: 'Card 1',
description: 'Before you get into the nitty-gritty of coming up with a perfect title, start with a rough draft: your working title. What is that, exactly? A lot of people confuse working titles with topics. A lot of people confuse working titles with topics. A lot of people confuse working titles with topics. A lot of people confuse working titles with topics.',
video: 'https://lp.planodevida.com/lps/o-que-e-etf/videos/o-que-e-etf_video_intro.mp4',
video_thumb: require('@/assets/images/banner/jornada-thumb.png'),
activeLike: false,
activeFavorite: false,
enable: true,
completed: false,
social: {
like: 10,
comments: 10,
favorite: 10,
},
author:{
avatar: '',
name: 'Fulano de tal',
slug: 'perfil-do-autor',
},
},
{
title: 'Card 2',
description: 'Before you get into the nitty-gritty of coming up with a perfect title, start with a rough draft: your working title. What is that, exactly? A lot of people confuse working titles with topics. A lot of people confuse working titles with topics. A lot of people confuse working titles with topics. A lot of people confuse working titles with topics.',
video: 'https://lp.planodevida.com/lps/o-que-e-etf/videos/o-que-e-etf_video_intro.mp4',
video_thumb: require('@/assets/images/banner/jornada-thumb.png'),
activeLike: false,
activeFavorite: false,
enable: true,
completed: false,
social: {
like: 10,
comments: 10,
favorite: 10,
},
author:{
avatar: '',
name: 'Fulano de tal',
slug: 'perfil-do-autor',
},
},
]
arrayLength: 0,
}
}
我正在使用以下方法计算数组长度:function() return arrayLength = this.trail_card.length 主要问题: 如何将此变量从 trailCard(组件 1)发送到 progressBar(组件 2)以在组件 2 上的方法或任何我需要的方法上使用它
【问题讨论】:
-
他们有同一个父母吗?
-
“无论我需要什么”,我假设您可以在整个应用程序中使用 arrayLength。说我建议您使用 vuex 以避免耦合您的组件。在您的情况下,trailCard 组件应该水合您的 vuex 状态,任何其他需要使用 arrayLength 的组件都可以使用 mapGetters
标签: vue.js vuejs2 vue-component vuex