如何从另一个组件访问 App.vue?
How to access App.vue from another component?
在我用 VueJs 2 编写的应用程序中,我进入了 Vue.app 这段代码:
export default {
name: 'app',
data () {
return {
title: 'Gestione fornitori',
idfornitore: ''
}
},
methods: {
loadFunction (route) {
this.$router.push(route)
}
}
}
</script>
我希望从另一个组件访问 属性 idfornitore
,我使用过:
mounted () {
this.$parent.idfornitore = ''
},
或者还有:
mounted () {
var Vue = require('vue')
Vue.app.idfornitore = ''
},
但是没有用。从另一个组件访问 属性 的正确方法是什么?
提前致谢。
使用道具在父子之间传递数据。
发出事件以在子级与父级之间进行通信
Parent.vue
<template>
<div>
<h2>Parent: {{idfornitore}}</h2>
<child :idfornitore="idfornitore" @changevalue="idfornitore = $event"></child>
//idfornitore - data sent to child from parent.
//changevalue - event emitted from child and received by parent
</div>
</template>
<script>
import Child from './compname.vue';
export default {
components:{
"child" : Child
},
data(){
return {
idfornitore : "34"
}
}
}
</script>
Child.vue
<template>
<div>
Child: {{idfornitore}}
<button @click="add()">Add</button>
</div>
</template>
<script>
export default {
props:["idfornitore"],
methods : {
add(){
this.idfornitore++; // mutating props directly will result in vuejs warning. i have used this code here to show how this works.
this.$emit("changevalue",this.idfornitore); //cascade the update to parent
}
}
}
</script>
- 如果您觉得通过 props 进行通信导致紧耦合,那么更方便的方法是使用 eventBus
组件之间的通信通过 props
或 events
完成。
如果您要访问的组件数据有子关系,您可以使用props
。但就您而言,您需要更改父数据。为此,您可以发出 events
。如果组件(您希望更新)是当前组件的直接父级,则事件将是干净的解决方案。
如果不是这样,请使用 EventBus 模式。使用此模式,您可以从任何组件发出事件并在任何其他组件中收听它们并相应地应用更改。
https://vuejs.org/v2/guide/components.html#Non-Parent-Child-Communication
https://alligator.io/vuejs/global-event-bus/
EventBus 模式会产生非常脏的代码,如果需要更频繁地在组件之间进行通信,调试就会变得非常困难。
要处理多个组件之间的共享状态,强烈建议使用vuex
。它使您的应用程序状态易于管理和维护。我相信每个真实世界的 Vue 应用程序都需要状态管理,这可以通过 vuex
(或其他类似工具)轻松实现,我建议您这样做。
在我用 VueJs 2 编写的应用程序中,我进入了 Vue.app 这段代码:
export default {
name: 'app',
data () {
return {
title: 'Gestione fornitori',
idfornitore: ''
}
},
methods: {
loadFunction (route) {
this.$router.push(route)
}
}
}
</script>
我希望从另一个组件访问 属性 idfornitore
,我使用过:
mounted () {
this.$parent.idfornitore = ''
},
或者还有:
mounted () {
var Vue = require('vue')
Vue.app.idfornitore = ''
},
但是没有用。从另一个组件访问 属性 的正确方法是什么?
提前致谢。
使用道具在父子之间传递数据。
发出事件以在子级与父级之间进行通信
Parent.vue
<template>
<div>
<h2>Parent: {{idfornitore}}</h2>
<child :idfornitore="idfornitore" @changevalue="idfornitore = $event"></child>
//idfornitore - data sent to child from parent.
//changevalue - event emitted from child and received by parent
</div>
</template>
<script>
import Child from './compname.vue';
export default {
components:{
"child" : Child
},
data(){
return {
idfornitore : "34"
}
}
}
</script>
Child.vue
<template>
<div>
Child: {{idfornitore}}
<button @click="add()">Add</button>
</div>
</template>
<script>
export default {
props:["idfornitore"],
methods : {
add(){
this.idfornitore++; // mutating props directly will result in vuejs warning. i have used this code here to show how this works.
this.$emit("changevalue",this.idfornitore); //cascade the update to parent
}
}
}
</script>
- 如果您觉得通过 props 进行通信导致紧耦合,那么更方便的方法是使用 eventBus
组件之间的通信通过 props
或 events
完成。
如果您要访问的组件数据有子关系,您可以使用props
。但就您而言,您需要更改父数据。为此,您可以发出 events
。如果组件(您希望更新)是当前组件的直接父级,则事件将是干净的解决方案。
如果不是这样,请使用 EventBus 模式。使用此模式,您可以从任何组件发出事件并在任何其他组件中收听它们并相应地应用更改。
https://vuejs.org/v2/guide/components.html#Non-Parent-Child-Communication https://alligator.io/vuejs/global-event-bus/
EventBus 模式会产生非常脏的代码,如果需要更频繁地在组件之间进行通信,调试就会变得非常困难。
要处理多个组件之间的共享状态,强烈建议使用vuex
。它使您的应用程序状态易于管理和维护。我相信每个真实世界的 Vue 应用程序都需要状态管理,这可以通过 vuex
(或其他类似工具)轻松实现,我建议您这样做。