vue 子组件 调用、触发父组件中的方法

深碍√TFBOYSˉ_ 2022-04-02 10:56 328阅读 0赞

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

我发现了两种写法。

方法一:

子组件:

  1. <template>
  2. <button @click="submit">提交</button>
  3. </template>
  4. <script>
  5. export default {
  6. methods: {
  7. submit: function () {
  8. // 子组件中触发父组件方法ee并传值cc12345
  9. this.$emit('ee', 'cc12345')
  10. }
  11. }
  12. }
  13. </script>

父组件:

  1. <template>
  2. <editor id="editor" class="editor" @ee="cc"></editor>
  3. </template>
  4. <script>
  5. export default {
  6. methods: {
  7. cc: function (str) {
  8. alert(str)
  9. }
  10. }
  11. }
  12. </script>

方法二:

子组件:

  1. <template>
  2. <button @click="submit">提交</button>
  3. </template>
  4. <script>
  5. export default {
  6. props: {
  7. onsubmit: {
  8. type: Function,
  9. default: null
  10. }
  11. },
  12. methods: {
  13. submit: function () {
  14. if (this.onsubmit) {
  15. this.onsubmit(‘cc12345’)
  16. }
  17. }
  18. }
  19. }
  20. </script>

父组件:

  1. <template>
  2. <editor id="editor" class="editor" :onsubmit="cc"></editor>
  3. </template>
  4. <script>
  5. export default {
  6. methods: {
  7. cc: function (str) {
  8. alert(str)
  9. }
  10. }
  11. }
  12. </script>

转自:https://www.cnblogs.com/caik13/p/6896890.html

发表评论

表情:
评论列表 (有 0 条评论,328人围观)

还没有评论,来说两句吧...

相关阅读