【问题标题】:Emits in Vue won't emitVue中的发射不会发射
【发布时间】:2021-04-15 13:48:11
【问题描述】:

我想做什么:

我单击 cmets.vue 中的按钮,该按钮假定添加在文本区域中键入的注释。这个动作将被发送到 event.vue 来监听这个动作。当它听到这个动作时,它将激活一个将评论推送到列表中的方法。然后我想显示这个 cmets 列表。我不知道为什么它不起作用。

评论.vue:

<template>
  <div>
    <form>
      <label>Comments</label>
      <textarea v-model="comment"/>
      <input class="button" @click.prevent="addComment" type="submit" value="Add comment">
    </form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      name: 'Arnulf',
      comment: ''
      /* timeAndDate: Date */
    }
  },
  methods: {
    addComment () {
      const comment = {
        name: this.name,
        comment: this.comment
        /* timeAndDate: this.timeAndDate */

      }
      console.log('hello')
      this.$emit('submitted', comment)// THIS WONT EMIT FOR SOME REASON

      this.comment = ''
    }
  }
}
</script>

Event.vue:

<template>
  <div>
    <CommentList :comments="listOfComments"></CommentList>
    <Comments @submitted="addComment"></Comments>
  </div>
</template>

<script>
import CommentList from '@/components/CommentList'
import Comments from '@/components/Comments'
export default {
  data () {
    return {
      listOfComments: []
    }
  },
  name: 'Event',
  components: {
    CommentList,
    Comments
  },
  methods: {
    addComment (comment) {
      console.log('bruh')
      this.listOfComments.push(comment)
    }
  }
}
</script>

commentlist.vue:

<template>
  <div>
    <h3>Kommentarer:</h3>
    <ul>
      <li v-for="(comment, index) in comments" :key="index">
        {{comment.name}}:
        <br/>
        "{{comment.comment}}"
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'CommentList',
  props: {
    comments: {
      type: Array
    }
  }
}
</script>

编辑:添加了其余代码,因此可以跟踪所有逻辑。

【问题讨论】:

  • listOfComments 是被动的吗? Aka 你在data 中定义了吗?
  • 是的,它是在数据中定义的。 console.log('bruh') 永远不会命中。
  • @Kenso33,我已经在 SO 代码 sn-ps(发布为下面的答案)以及 Vue SFC Playground 中尝试了您的代码。它在任何地方都运行良好。如果能提供某种问题重现的代码sn-ps就更好了。
  • 您使用的是vue2vue3

标签: javascript vue.js


【解决方案1】:

您的代码运行良好。检查下面的代码sn-p。

Vue.config.productionTip = false;
Vue.config.devtools = false;

Vue.component('root', {
  data () {
    return {
      listOfComments: []
    }
  },
  name: 'Event',
  methods: {
    addComment (comment) {
      console.log('bruh')
      this.listOfComments.push(comment)
    }
  },
  template: `
  <div>
    <comments-list :comments="listOfComments"></comments-list>
    <comments @submitted="addComment"></comments>
  </div>
  `
});

Vue.component('comments',{
data () {
    return {
      name: 'Arnulf',
      comment: ''
      /* timeAndDate: Date */
    }
  },
  methods: {
    addComment () {
      const comment = {
        name: this.name,
        comment: this.comment
        /* timeAndDate: this.timeAndDate */

      }
      console.log('hello')
      this.$emit('submitted', comment)// THIS WONT EMIT FOR SOME REASON

      this.comment = ''
    }
  },
  template : `
    <div>
    <form>
      <label>Comments</label>
      <textarea v-model="comment"/>
      <input class="button" @click.prevent="addComment" type="submit" value="Add comment">
    </form>
  </div>
  `
});

Vue.component('comments-list',{
  props: {
    comments: {
      type: Array
    }
  },
  template : `
  <div>
    <h3>Kommentarer:</h3>
    <ul>
      <li v-for="(comment, index) in comments" :key="index">
        {{comment.name}}:
        <br/>
        "{{comment.comment}}"
      </li>
    </ul>
  </div>
  `
});

new Vue({ el:"#root" });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="root">
  <root></root>
</div>

【讨论】:

  • 谢谢!看来我的 event.vue 组件是问题所在。我非常纠结为什么我的代码不能正常工作,我不认为只是 html 不能正常工作。它现在已修复且功能齐全!谢谢!
猜你喜欢
  • 1970-01-01
  • 2021-06-27
  • 2022-11-22
  • 2021-02-03
  • 2021-04-26
  • 1970-01-01
  • 2020-08-30
  • 2020-03-09
  • 1970-01-01
相关资源
最近更新 更多