Issues with data bind in vue.js and events

百般思念 提交于 2020-12-15 04:58:26

问题


I am working on a basic notepad app, for now the functionality is simple, create a note, when done click on the note from a list of previous created notes to view its details. I am not able to click on the note and see the details, instead I see the details of the component ShowNote.vue on the bottom of the notepad template, and in order to see the details I have to make the v-if="noteIsOpen to false". I am also not able to see the data from the data bind in ShowNote.vue file. Also when you click on the plus button the details from the note populate the page the button generates when clicked. I will paste screen shots of my code below. Please help me figure this out. I have tried to fix the props, and when I did I was able to see the note details finally.

App.vue
<template>
  <div class="body">
    <div class="notepad-container h-75 w-75">
      <header class="header d-flex justify-content-center align-items-center">
        <h4>Light Notepad v1</h4>
      </header>

      <section class="notepad-content" v-if="editorIsOpen === false">
        <note-list
          v-for="note in notes"
          :key="note.id"
          :note="note"
        ></note-list>
        <add-note-button @open-editor="openNewEditor"></add-note-button>
      </section>

      <section class="notepad-editor" v-if="editorIsOpen === true">
        <save-button></save-button>
      </section>

      <section class="notepad-content" v-if="noteIsOpen === true">
        <show-note 
        :note="notes"
        @open-note="readNote"
        />
      </section>

      <section class="notepad-content" v-if="noteIsOpen === false">
        <show-note 
        :note="notes"
        @open-note="openNote"
        />
      </section>
      
    </div>
  </div>
</template>
<script>
import AddNoteButton from "./components/AddNoteButton.vue";
import NoteList from "./components/NoteList.vue";
import SaveButton from "./components/SaveButton.vue";
import ShowNote from "./components/ShowNote.vue";

export default {
  components: {
    NoteList,
    AddNoteButton,
    SaveButton,
    ShowNote,
  },
  data() {
    return {
      editorIsOpen: false,
      noteIsOpen: false,
      notes: [
        {
          id: 1,
          title: "1st Note",
          body: "This is a note",
          date: "10/17/20",
        },
        {
          id: 2,
          title: "2nd Note",
          body: "This is a note",
          date: "11/17/20",
        },
      ],
    };
  },
  methods: {
    openNewEditor() {
      this.editorIsOpen = !this.editorIsOpen;
    },
    readNote() {
      this.noteIsOpen = !this.noteIsOpen;
    },
  },
};
</script>

AddNoteButton.vue

<template>
  <div class="add-note-container" @click="openEditor">
    <b-icon-plus-circle></b-icon-plus-circle>
  </div>
</template>

<script>
import {BIconPlusCircle} from 'bootstrap-vue';
export default {
  emits: ['open-editor'],
  components: {
    BIconPlusCircle
  },
  methods: {
    openEditor() {
      console.log('hello');
      this.$emit('open-editor');
    }
  }
}
</script>

NoteList.vue

<template>
<div>
    <b-list-group>
      <b-list-group-item button @click="openNote()" 
        >{{ note.title }} - {{ note.date }}</b-list-group-item
      >
    </b-list-group>
    </div>
</template>
<script>
export default {
  emits: ['open-note'],
  props: {
    note: {
      required: true,
    },
  },
  methods: {
    openNote() {
      this.$emit('open-note');
      console.log("clicked from NoteList");

    },
  },
};
</script>

ShowNote.vue

<template>
  <div>
   note details: 
    Note ID: {{ note.id }}, Date: {{ note.date }},
    Title: {{ note.title }}, Body: {{ note.body }}
  </div>
</template>

<script>
export default {
  name: 'showNote',
  props: {
    note: {
      required: true,
    }
  },
  
};
</script>

回答1:


In your NoteList.vue you are emitting the event "open-note". But this event is never catched in your parent component named App.vue. You have to bind this event in order to get notified when ever you clicked on a note entry. Something like @open-note="openNote".




回答2:


I figured out how to get the details to show when clicking on the note, for now I created a button in the notepad-content section:

<button class="readNoteButton" @click="readNote">view note one</button> 

and changed the section with the show note component to:

  <section v-if="readingNote === true" class="">
    <show-note
      @open-note="openNote"
      v-for="note in notes"
      :key="note.id"
      :note="note"
    ></show-note>
  </section>

issue I have now is figuring out how to get the details to show separately pertaining to each individual button



来源:https://stackoverflow.com/questions/65229871/issues-with-data-bind-in-vue-js-and-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!