简体   繁体   English

Vue.js 如何将参数从一个组件传递到另一个组件?

[英]Vue.js How to pass params from one component to another component?

I am working on adding a refresh button.我正在努力添加一个刷新按钮。 How to do I pass the 1 in component A to component B?如何将组件 A 中的 1 传递给组件 B? I tried adding return NameAdmin.methods.refreshPage(1);我尝试添加return NameAdmin.methods.refreshPage(1); but it isn't being passed.但它没有被通过。 I can see the refresh adminName Page in the console.log.我可以在 console.log 中看到refresh adminName Page

here is component A: NameAdmin这是组件 A:NameAdmin

 methods: { refreshPage() // refreshPage(1) doesn't work { console.log('refresh adminName Page'); this.getData(1); }, }

here is component B这是组件B

 import NameAdmin from "./NameAdmin"; Computed: { refreshAction() { switch (this.$route.name) { case "office": return 'buildingData'; case "audienceadmin": return NameAdmin.methods.refreshPage(); case "testing": return null; } return null; }, refreshPage() { if (.this;refreshAction) return. if(typeof this.refreshAction === "function") this;refreshAction(). else this.$store.dispatch(this;refreshAction), }, }, }
 <template> <refresh-button id="refresh-button":class="refresh-style" @on-refresh="refreshPage" /> </template>

how can I fix my code or how should I code this differently, please let me know!我该如何修复我的代码或者我应该如何以不同的方式编码,请告诉我! Any advice is appreciated!任何建议表示赞赏!

Assuming that 2 of those components are not related to each other (not parent-child relation etc), you will have to use a state management like an user has stated.假设其中两个组件彼此不相关(不是父子关系等),您将必须像用户所说的那样使用 state 管理。

And since you are using vue2, you have to use vuex并且因为你使用的是 vue2,所以你必须使用vuex

A common example of a state management usage is shopping cart on E-commerce websites. state management用途的一个常见示例是电子商务网站上的购物车。

For example I went to Shop A , add XYZ product on my shopping cart, then go to Shop B , add LMN product to my shopping cart.例如我去Shop A ,将XYZ 产品添加到我的购物车,然后 go 到Shop B ,将LMN 产品添加到我的购物车。 When i view the cart, it will contain both XYZ product and LMN product .当我查看购物车时,它将同时包含XYZ 产品和 LMN 产品 That's thanks to state management like vuex provides.这要感谢vuex提供的state management

Read the docs for further information of how this works because it require setup.阅读文档以获取有关其工作原理的更多信息,因为它需要设置。

I hope this helps!我希望这有帮助!

暂无
暂无

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

相关问题 如何将数据(书籍数组长度)从一个组件传递到 vue.js 中的另一个组件? - How to pass data(books array length) from one component to another component in vue.js? 如何将数据从一个组件传递到另一个组件并将该数据存储到 vue.js 中的数组中? - How to pass data from one component to another component and store that data into an array in vue.js? 如何将方法从一个组件传递到另一个组件并在 Vue.js 中使用它? - How to pass a method from one component to another and use it onclick in Vue.js? 如何在 router.push() (Vue.js) 中将参数从一个组件传递到另一个组件? - How to pass parameters from one component to another in router.push() (Vue.js)? 如何将组件作为道具传递给Vue.js中的另一个组件 - How to pass component as prop to another component in Vue.js Vue.js 显示从另一个组件调用一个组件 - Vue.js Display call one component from another component 如何将参数从 div 传递到 Vue.js 中的单页组件? - How to pass params from div to single page component in Vue.js? 如何将道具传递给 vue.js 中的另一个组件 - How do I pass props to another component in vue.js vue.js中如何将一个组件的卡数据绑定到另一个组件卡? - How to bind card data from one component to another component card in vue.js? Vue.js:将数据从一个子组件传递到另一个具有相同父组件的组件? - Vue.js: pass data from one child component to another with the same parent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM