如何从另一个vue文件中获取数据?
如下所示,A.Vue文件具有元素数据返回一些数字值
<template></template>
<script>
export default {
data(){
return{
element: [
{
number:'11'
}
{
number:'22'
}
]
}
}
}
</script>
,现在我想从 a.vue 到 b.vue 。有办法做到吗?我看到了单击按钮的解决方案,但我不想使用按钮传递数据。
b.vue文件
<template>
<div>I want to get element.length here</div>
</template>
As the code show below, A.vue file has element data return some number values
<template></template>
<script>
export default {
data(){
return{
element: [
{
number:'11'
}
{
number:'22'
}
]
}
}
}
</script>
Now I want to get element.length
from A.vue to B.vue. Is there a way to do that? I saw a solution with button click but i dont want to use button to pass data.
B.vue file
<template>
<div>I want to get element.length here</div>
</template>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过将prop(包含元素数组的长度)从
a.vue
组件传递到b.vue
组件来实现它。这是现场演示:You can simply achieve it by passing prop (which contains the length of the element array) from
A.vue
component toB.vue
component. Here is the live demo :如果可能的话,只需将数据作为Prop从B传递到A,这样您就可以在数据上实现任何逻辑。
如果不是这样,则应将VUEX使用用于数据存储,因此任何组件都可以访问它。
If it's possible, just pass the data as a prop from B to A, this way you can implement any logic on the data.
If it's not, you should use vuex for data storage, so any component can access it.