ditdot-dev / vue-flow-form

Create conversational conditional-logic forms with Vue.js.
https://www.ditdot.hr/en/vue-flow-form
MIT License
779 stars 174 forks source link

Global Variables (or previous answers) via QuestionModel? #249

Closed liamb13 closed 2 years ago

liamb13 commented 2 years ago

This may already be possible and I'm just having a mind blank.

Using the Questionnaire example, how could I keep 'first_name' to reuse it in taglines and titles of multiple other questions? I know it can be done within components but how about in a questions data array?

I tried with the following:

export default {
  data() {
    return {
      name_label: "",
      questions: [
        new QuestionModel({
          id: "first_name",
          title: `What is your name?`,
          ...
        }),
        new QuestionModel({
          id: "email",
          title: `Nice to meet you ${this.name_label}! What is your email?`,
          ...
        }),
      ],
    }
  },
  methods: {
    onQuestionAnswered(question) {
      if (question.id === "first_name" && question.answer !== null) {
        this.name_label = question.answer;
      }
    },
  },
};

Instead ended up using, which only covers 1 question and isn't multiple language friendly:

    onQuestionAnswered(question) {
      console.log(question);
      if (question.id === "first_name" && question.answer !== null) {
        this.questions[1].tagline = `Nice to meet you ${question.answer}. What is your email?`;
      }
    },
spinn commented 2 years ago

Hi @liamb13,

unfortunately this is only possible with questions as components as this is the only way to get full Vue reactivity.