如何从另一个组件访问 App.vue? [英] How to access App.vue from another component?

查看:38
本文介绍了如何从另一个组件访问 App.vue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我用 VueJs 2 编写的应用程序中,我在 Vue.app 中输入了以下代码:

出口默认{名称:'应用',数据 () {返回 {title: 'Gestione fornitori',idfornitore: ''}},方法: {loadFunction(路由){this.$router.push(路由)}}}

我希望从另一个组件访问属性 idfornitore,我使用过:

 挂载 () {this.$parent.idfornitore = ''},

或者:

 挂载 () {var Vue = require('vue')Vue.app.idfornitore = ''},

但是没有用.从另一个组件访问属性的正确方法是什么?

提前致谢.

解决方案

  • 使用 props 将数据从父级传递给子级.

  • 发出事件以从子级到父级进行通信

Parent.vue

 <模板><div><h2>父级:{{idfornitore}}</h2><child :idfornitore="idfornitore" @changevalue="idfornitore = $event"></child>//idfornitore - 从父级发送给子级的数据.//changevalue - 从子级发出并由父级接收的事件

<脚本>从'./compname.vue'导入子项;导出默认{组件:{孩子":孩子},数据(){返回 {idfornitore : "34"}}}

Child.vue