[英]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.