简体   繁体   English

如何在不使用道具的情况下将数据从父组件传递到子组件

[英]How to pass data from parent to child component without using props

I want to code a reusable Vue tab component (I know there are a lot there but I'm doing this for the sake of a challenge). 我想编写一个可重复使用的Vue标签组件(我知道那里有很多,但是为了挑战,我正在这样做)。

The problem I am facing right now is passing data to child without using props. 我现在面临的问题是在不使用道具的情况下将数据传递给孩子。

The reason for this is very simple, I need the child tab element to know the current selected tab index and I don't want user who uses my component to always need to type in the props for every tab component. 原因很简单,我需要子选项卡元素知道当前选定的选项卡索引,并且我不希望使用我的组件的用户总是需要为每个选项卡组件输入道具。

For this very reason I have gone and observe how other Vue tabs library solve this issue ( bootstrap vue , vue tabs etc.etc.), but all I found are them accessing parent properties with this.$parent or this.$children to access child properies. 由于这个原因,我去观察了其他Vue标签库如何解决此问题( bootstrap vuevue标签等),但是我发现的全部是他们使用this.$parentthis.$children访问父级属性this.$parent儿童财产。 And I know this is not the Vue way. 而且我知道这不是Vue的方式。

I have looked into inject and provide, it's is great but it's not reactive. 我研究了注入和提供,这很棒,但是没有反应。

I also don't want to use Vuex because my project is too small for it and I want my component to be reusable. 我也不想使用Vuex,因为我的项目太小了,我希望组件可重用。

Is there a better way of doing this? 有更好的方法吗?

A simple solution is to create your own store without using Vuex: 一个简单的解决方案是在不使用Vuex的情况下创建自己的商店:

 class TabStore { constructor() { this.state = { currentIndex: 1 } } setIndex(value) { this.state.currentIndex = value } } let tabStore = new TabStore() Vue.component('tab-item', { template: '#tab', data() { return { state: tabStore.state } } }) new Vue({ el: "#app", data() { return { state: tabStore.state } }, methods: { changeIndex() { let index = Math.floor((Math.random() * 10) + 1) //<-- for the example tabStore.setIndex(index) } } }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script> <div id="app"> <p>Current Index from Main component: <strong>{{ state.currentIndex }}</strong></p> <tab-item></tab-item> <button @click="changeIndex">Change current index</button> </div> <template id="tab"> <p>Current Index from Tab component: <strong>{{ state.currentIndex }}</strong></p> </template> 

You can use setIndex() from the store to change the state.currentIndex and you have access to this one everywhere. 您可以从商店中使用setIndex()来更改state.currentIndex并且可以在任何地方访问此state.currentIndex

I believe learning Vuex may be of interest to you. 我相信您可能会对学习Vuex感兴趣。
Vuex in simple terms acts as a 'single source of truth' for data in your Vue app. 简单来说,Vuex充当Vue应用程序中数据的“单一事实来源”。
This means any data stored in the vuex 'store' can be accesses by any component in your app. 这意味着您的应用程序中的任何组件都可以访问vuex“商店”中存储的任何数据。 If you set the components to import your vuex store correctly, it also offers reactive bindings to any other component referencing the store which means you no longer worry about running into complex passing of properties through multiple components. 如果您将组件设置为正确导入vuex存储,它还将提供对任何其他引用该存储的组件的反应性绑定,这意味着您不再担心会遇到多个组件之间复杂的属性传递。 Instead each component references the root stores data. 而是每个组件都引用根存储数据。 (Hope that makes sense.) (希望如此。)

This is a very high level overview, but if it sounds like it would help your project, you should really check out the Official Documentation which is great! 这是一个非常高级的概述,但是如果听起来对您的项目有帮助,那么您应该真正查看一下官方文档 ,这很棒!

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

相关问题 Svelte:如何将数据或道具从子组件传递给父组件? - Svelte: How to pass data or props from a child component to the parent? 如何使用 React Native 中的 props 将数组从父组件传递到子组件? - How to pass an array from a parent component to child component using props in React Native? 无法将道具从父组件传递到子组件 - Unable to pass props from the parent to child component 如何将道具从子组件传递到父组件到 ReactJS 中的另一个子组件? - How to pass props from child component to parent component to another child component in ReactJS? 使用prop将子组件数据传递给父组件 - Passing child component data to parent using props 如果子组件为动态组件,如何将数据从子组件传递给父组件 - How to pass data from Child to Parent, if Child component dynamic component 如何在React Router上将Pros数据从父级传递给子级 - How to pass `props` data from parent to child on react router Vuejs - 使用道具将数据从父级传递给子级 - Vuejs - pass data from parent to child with props 如何使用组合模式将 React 道具从父母传递给孩子 - How to pass React props from parent to child using composition pattern 无法将道具从父组件传递给子组件 - Unable to pass props to child Component from Parent Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM