简体   繁体   English

如何获取输入值并将其发送到另一个组件

[英]How get a input value and send him to another component

I have an modal component, and inside this i have two input how i can get the value inserted in this input and send him to another component? 我有一个模态组件,在此组件中有两个输入,如何获取插入到此输入中的值并将其发送到另一个组件?

  <template> <modal v-if="Modal"> div(class="modal-title has-text-centered" slot="header") div(slot="content") div(class="control") div(class="columns") div(class="column") div(class="field") div(class="control") input(class="input is-info is-large" type="text" placeholder="Analysis name") div(class="field") div(class="control") input(class="input is-info is-large" type="text" placeholder="Item name") div(slot="footer") button(class="button is-info is-large modal-button" @click="closeModal" style="margin-right:5em;") Cancel <router-link :to="{name: 'analyse'}" class="button is-info is-large modal-button">Create</router-link> </modal> </template> <script> import JwtDecode from 'jwt-decode' import Modal from '../layout/modal/Basemodal.vue' import Section from '../../views/Section.vue' export default { data: function() { return { user: '', isActive: false, isDrop: false, Modal: false, analyseName: '', analyseItem: '', application: '', } }, components: { 'modal': Modal, 'section-content': Section, }, methods: { // on-click logout the user and send him to the initial page logout: function(){ this.$store.dispatch('logout') .then(resp => { this.$router.push('/') }) }, // used to change the color of the selected button toggle: function(event) { event.ldaModal = !event.ldaModal }, createAnalyse: function() { this.$route.push('/analyse') } }, mounted() { // render user token to get all user information this.user = JwtDecode(localStorage.getItem('token')) } } </script> 

and here i have another component, in this component i need to get the value of modal and render it 在这里,我还有另一个组件,在这个组件中,我需要获取modal的值并将其渲染

 <template lang="pug"> p {{ input1-value }} p {{ input2-value }} </template> 

please, what's the best approach to do this? 拜托,什么是最好的方法呢?

I think that you should use the EventBus to solve your question. 我认为您应该使用EventBus解决您的问题。

in main.js 在main.js中

const bus = new Vue()
Vue.prototype.$bus = bus 

you can register an event listener 您可以注册一个事件监听器

this.$bus.on( 'test', ( data ) => {
 console.log( 'test', data );
} );

and emit it 并发射它

this.$bus.emit( 'test', { code: 1 } );

you also can look at https://github.com/yyued/hub.js 您还可以查看https://github.com/yyued/hub.js

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM