【问题标题】:how to uncheck answered question in child vue from parent如何从父级取消选中子Vue中已回答的问题
【发布时间】:2019-05-18 11:26:53
【问题描述】:

我的应用程序类似于测验。每个问题可能有单选按钮答案、整数、文本等。 考虑选择题的情况。我有一个单选按钮选项的 vue。在父 Vue 中,每个问题都有一个重置按钮。如果我点击重置,它应该会删除该特定问题的选定答案。

如果重置按钮在父 vue 中,而要重置的答案在子 vue 中,我该如何实现?

家长:

<template>
  <div class="inputContent">
    <p class="lead" v-if="title">
       {{title}}
    <span v-if="valueConstraints.requiredValue" class="text-danger">* . 
    </span>
   </p>
  <b-alert variant="danger" show v-else>
      This item does not have a title defined
  </b-alert>

  <!-- If type is radio -->
  <div v-if="inputType==='radio'">
    <Radio :constraints="valueConstraints" :init="init" 
           :index="index" v-on:valueChanged="sendData" />
  </div>

  <!-- If type is text -->
  <div v-else-if="inputType==='text'">
    <TextInput :constraints="valueConstraints" :init="init" v- 
        on:valueChanged="sendData"/>
  </div>

  <div class="row float-right">
    <b-button class="" variant="default" type=reset @click="reset">
    Reset1
    </b-button>
    <b-button class="" variant="default" v- 
       if="!valueConstraints.requiredValue" @click="skip">
    Skip
    </b-button>
  </div>

</div>
</template>

<style></style>

<script>
import { bus } from '../main';
import Radio from './Inputs/Radio';
import TextInput from './Inputs/TextInput';

export default {
   name: 'InputSelector',
   props: ['inputType', 'title', 'index', 'valueConstraints', 
        'init'],
   components: {
       Radio,
       TextInput,
   },
  data() {
      return {
      };
  },
  methods: {
      skip() {
         this.$emit('skip');
      },
      // this emits an event on the bus with optional 'data' param
      reset() {
         bus.$emit('resetChild', this.index);
         this.$emit('dontKnow');
      },
      sendData(val) {
         this.$emit('valueChanged', val);
         this.$emit('next');
      },
  },
};
</script>

子 vue:

<template>
  <div class="radioInput container ml-3 pl-3">
    <div v-if="constraints.multipleChoice">
      <b-alert show variant="warning">
        Multiple Choice radio buttons are not implemented yet!
      </b-alert>
    </div>
    <div v-else>
      <b-form-group label="">
        <b-form-radio-group v-model="selected"
                            :options="options"
                            v-bind:name="'q' + index"
                            stacked
                            class="text-left"
                            @change="sendData"
        >
        </b-form-radio-group>
      </b-form-group>
    </div>
  </div>
</template>

<style scoped>
</style>

<script>
import _ from 'lodash';
import { bus } from '../../main';

export default {
  name: 'radioInput',
  props: ['constraints', 'init', 'index'],
  data() {
    return {
      selected: null,
    };
  },
  computed: {
    options() {
      return _.map(this.constraints['itemListElement'][0]['@list'], (v) => {
        const activeValueChoices = _.filter(v['name'], ac => ac['@language'] === "en");
        return {
          text: activeValueChoices[0]['@value'],
          value: v['value'][0]['@value'],
        };
      });
    },
  },
  watch: {
    init: {
      handler() {
        if (this.init) {
          this.selected = this.init.value;
        } else {
          this.selected = false;
        }
      },
      deep: true,
    },
  },
  mounted() {
    if (this.init) {
      this.selected = this.init.value;
    }
    bus.$on('resetChild', this.resetChildMethod);
  },
  methods: {
    sendData(val) {
      this.$emit('valueChanged', val);
    },
    resetChildMethod(selectedIndex) {
      this.selected = false;
    },
  },
};
</script>

【问题讨论】:

    标签: javascript vue.js vue-component


    【解决方案1】:

    一种方法是使用事件总线

    在你的主 js 中添加:

    //set up bus for communication
    export const bus = new Vue();
    

    在你的父 vue 中:

    import {bus} from 'pathto/main.js';
    
    // in your 'reset()' method add:
    // this emits an event on the bus with optional 'data' param
    bus.$emit('resetChild', data);
    

    在你的孩子 vue 中

    import {bus} from 'path/to/main';
    
    // listen for the event on the bus and run your method
    mounted(){
        bus.$on('resetChild', this.resetChildMethod());
    },
    methods: {
      resetChildMethod(){
        //put your reset logic here
      }
    }
    

    【讨论】:

    • 它给出了这个错误:“resetChild”的事件处理程序出错:“TypeError:无法读取未定义的属性'apply'”。这是什么意思?
    • 您需要将代码添加到您的问题中以供我们查看
    • 那个错误不知何故不再存在。我已经编辑了我的问题。你能帮我重置选定的值吗?如果我这样做 this.selected=false 它会重置所有问题在 ui 中选择的值。如何仅取消选中当前问题?
    • 一种方法是为每个按钮分配一个唯一的prop 索引。然后在发出时,也传递索引,然后根据孩子的 this.index 属性检查发出的索引
    猜你喜欢
    • 2022-11-11
    • 1970-01-01
    • 2013-02-21
    • 2020-12-11
    • 2021-09-26
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    相关资源
    最近更新 更多