简体   繁体   English

如何将数据从组件文件传递到vue js中的查看文件

[英]How to pass data from component file to view file in vue js

I'm trying to pass data from vue component to vue view file.我正在尝试将数据从 vue 组件传递到 vue 视图文件。 I tried creating props but it didn't work.我尝试创建道具,但没有奏效。 I have one component file on path src/components/list.vue and another file on path src/views/select.vue我在路径src/components/list.vue上有一个组件文件,在路径src/views/select.vue上有另一个文件

The flow goes like this: User lands on select.vue page, here on click of input box a pop-up appears which have have list of elements in <ul> <li> tag from list.vue (component file).流程是这样的:用户登陆select.vue页面,点击输入框会出现一个弹出窗口,其中包含来自list.vue (组件文件)的<ul> <li>标签中的元素列表。

What I want to achieve:我想要达到的目标:

Whenever user select any option from list.vue file, modal pop-up should close and selected element should be displayed in input box of select.vue file.每当用户从list.vue文件中选择任何选项时,模式弹出窗口应关闭,并且所选元素应显示在select.vue文件的输入框中。

Below is code:下面是代码:

src/views/select.vue src/views/select.vue

    <label class="primary-label mb-2">First Question</label>
        <div class="form-block">
          <label class="secondary-label mb-1">Question</label>
          <b-form-input placeholder="Select Question" v-model="questions.first" class="form-control l-input" @click="onOptionChanged"></b-form-input>
        </div>
        <div class="form-block">
          <label class="secondary-label mb-1">Answer</label>
          <b-form-input v-model="answers.first" placeholder="Enter answer" class="form-control l-input"></b-form-input>
        </div>

   <script>
     export default {
   data() {
  return {
    questions: {
      first: null,          
    },
    answers: {
      first: null,          
    },
    options: [
      { value: null, text: 'Select Question', disabled:true },
      { value: 1, text: 'In what city were you born?' },
      { value: 2, text: 'What is the name of your favorite pet?' },
      { value: 3, text: 'What is your mother\'s maiden name?' },
      { value: 4, text: 'What high school did you attend?' },
      { value: 5, text: 'What is the name of your first school?' },
      { value: 6, text: 'What was the make of your first car?' },
      { value: 7, text: 'What was your favorite food as a child?' },
      { value: 8, text: 'Where did you meet your spouse?' },
    ],               
  }
},
methods: {      
  onOptionChanged() {        
    var modal_ref = 'myModalRef';
    this.$refs[modal_ref].show();   
  },       
},
components: {
  SecurityQuestionsList,
},   
 }

src/components/list.vue src/components/list.vue

<template>
  <main>    
<div class="container">
  <div class="row">
    <div class="col-md-6">
      <div class="search-block">
        <span class="s-icon fa fa-search"></span>           
      </div>         
      <ul class="l-group" v-if="filteredQuestions.length > 0">
        <li class="d-flex align-items-center" :key="item.id" v-for="item in filteredQuestions" @click="onOptionSelect(item.question)"
        :class="selected === item.id ? 'my-selected-item-class':null">
          {{item.question}}
        </li>
      </ul>
    </div>
  </div>
   </div>  
   </main>
  </template>
  <script>
   export default {
data() {
  return {
    search: '',
    selected: null,
    questions: [
      { id: 1, question: 'In what city were you born?' },
      { id: 2, question: 'What is the name of your favorite pet?' },
      { id: 3, question: 'What is your mother\'s maiden name?' },
      { id: 4, question: 'What high school did you attend?' },
      { id: 5, question: 'What is the name of your first school?' },
      { id: 6, question: 'What was the make of your first car?' },
      { id: 7, question: 'What was your favorite food as a child?' },
      { id: 8, question: 'Where did you meet your spouse?' },
    ],
  }
},
computed: {
  filteredQuestions() {
    return this.questions.filter(item => {
      return item.question.toLowerCase().includes(this.search.toLowerCase());
    });
  }
},        
methods: {
  onOptionSelect(selectedId) {
      this.selected = selectedId;            
      console.log(this.selected);
      this.$emit('questions-selected', this.selected);
  }, 
}
} 
</script>

I am getting selected value in console but not sure how to catch it in search.vue file.我在控制台中获得了选定的值,但不确定如何在 search.vue 文件中捕获它。

Thanks!谢谢!

You need to connect your components and pass props, try like following snippet:您需要连接您的组件并传递道具,请尝试以下代码段:

 Vue.component('list', { template: ` <main> <div class="container"> <div class="row"> <div class="col-md-6"> <div class="search-block"> <span class="s-icon fa fa-search"></span> </div> <ul class="l-group" v-if="filteredQuestions.length > 0"> <li class="d-flex align-items-center" :key="item.id" v-for="item in filteredQuestions" @click="onOptionSelect(item.question)" :class="selected === item.id ? 'my-selected-item-class':null"> {{item.question}} </li> </ul> </div> </div> </div> </main> `, props: ['search'], data() { return { selected: null, questions: [ { id: 1, question: 'In what city were you born?' }, { id: 2, question: 'What is the name of your favorite pet?' }, { id: 3, question: 'What is your mother\'s maiden name?' }, { id: 4, question: 'What high school did you attend?' }, { id: 5, question: 'What is the name of your first school?' }, { id: 6, question: 'What was the make of your first car?' }, { id: 7, question: 'What was your favorite food as a child?' }, { id: 8, question: 'Where did you meet your spouse?' }, ], } }, computed: { filteredQuestions() { if (this.search) { return this.questions.filter(item => { return item.question.toLowerCase().includes(this.search.toLowerCase()); }); } return this.questions } }, methods: { onOptionSelect(selectedId) { this.selected = selectedId; this.$emit('questions-selected', selectedId); }, } }) new Vue({ el: "#demo", name: 'select', data() { return { questions: { first: null, }, answers: { first: null, }, selected: false } }, methods: { onOptionChanged() { this.selected = true }, onSelected(val) { this.questions.first = val this.selected = false }, }, })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap/dist/css/bootstrap.min.css" /> <link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.css" /> <script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.js"></script> <script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue-icons.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" /> <div id="demo"> <div> <label class="primary-label mb-2">First Question</label> <div class="form-block"> <label class="secondary-label mb-1">Question</label> <b-form-input placeholder="Select Question" v-model="questions.first" class="form-control l-input" @click="onOptionChanged()"></b-form-input> </div> <div class="form-block"> <label class="secondary-label mb-1">Answer</label> <b-form-input v-model="answers.first" placeholder="Enter answer" class="form-control l-input"></b-form-input> <list :search="questions.first" @questions-selected="onSelected" v-if="selected"></list> </div> </div> </div>

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

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