javascript - 子方法未定义 vue.js

标签 javascript vue.js vuejs2

我在使用 refs 从父级调用子方法时遇到问题

enter image description here

这里是我的父组件:

<template>
    <div>
        <div class="home-video-screen tw--mb-2" @click="onToggleModal">
            <slot/>
        </div>
        <tw-modal ref="modal">
            <iframe width="100%" height="400" :src="videoSrc" frameborder="0" allowfullscreen=""></iframe>
        </tw-modal>
    </div>
</template>

<script>
    import TwModal from './TwModal'

    export default {
        props: {
            videoId: { type: String, required: true },
            placeholder: { type: String, required: true },
            alt: { type: String }
        },
        components: { TwModal },
        computed: {
            videoSrc() {
                const url = `https://www.youtube.com/embed/${this.videoId}`
                return `${url}?modestbranding=1&rel=0&controls=0&showinfo=0&html5=1&autoplay=1`
            }
        },
        methods: {
            onToggleModal() {
                this.$refs.modal.onToggle();
            }
        },
    }
</script>

<style lang="scss" scoped>
    .home-video-screen { cursor: pointer; }
</style>

这里是子组件:

<template>
    <fade-transition>
        <div v-if="modal" @click.self="onToggle" class="tw-fixed tw-z-50 tw-pin tw-overflow-auto tw-bg-smoke-darker tw-flex">
            <div class="tw-fixed tw-shadow-inner tw-max-w-md md:tw-relative tw-pin-b tw-pin-x tw-align-top tw-m-auto tw-justify-end md:tw-justify-center tw-p-1 tw-bg-white tw-w-full md:tw-h-auto md:tw-shadow tw-flex tw-flex-col">
                <div class="tw-flex tw-flex-col">
                    <span @click="onToggle" class="tw-text-right">
                        <svg class="tw-h-6 tw-w-6 tw-text-grey hover:tw-text-grey-darkest" role="button" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><title>Close</title><path d="M14.348 14.849a1.2 1.2 0 0 1-1.697 0L10 11.819l-2.651 3.029a1.2 1.2 0 1 1-1.697-1.697l2.758-3.15-2.759-3.152a1.2 1.2 0 1 1 1.697-1.697L10 8.183l2.651-3.031a1.2 1.2 0 1 1 1.697 1.697l-2.758 3.152 2.758 3.15a1.2 1.2 0 0 1 0 1.698z"/></svg>
                    </span>
                    <slot/>
                </div>
            </div>
        </div>
    </fade-transition>
</template>

<script>
    import {FadeTransition} from 'vue2-transitions'

    export default {
        props: {
            scrollable: {
                type: Boolean,
                default: false,
            }
        },
        data() {
            return {
                modal: false,
            }
        },
        components: { FadeTransition },
        methods: {
            onToggle () {
                this.modal = !this.modal;

                if (!this.scrollable && this.modal) {
                    document.body.classList.add('v--modal-block-scroll')
                } else {
                    document.body.classList.remove('v--modal-block-scroll')
                }
            }
        }
    }
</script>

<style>
    .v--modal-block-scroll {
        overflow: hidden;
        position: fixed;
        width: 100vw;
    }
</style>

我错过了什么? 🤔

最佳答案

如果您想从父级调用子方法,您可以通过this.$children[index].methodName() 访问子方法。 .此外,您的父模板可能不需要 <slot>标签,但应该直接引用子组件。这是一个简单的例子:

父组件

<template>
  <div>
    <h1 @click="clicked()">parent</h1>
    <child></child>
  </div>
</template>

<script>
import Child from './child.vue'
export default {
  components: {
    child: Child
  },
  methods: {
    clicked() {
      this.$children[0].childToggle()
    }
  }
}
</script>

子组件

<template>
  <div>
    <h2>child</h2>
  </div>
</template>

<script>
export default {
  methods: {
    childToggle() {
      console.log('child toggle')
    }
  }
}
</script>

关于javascript - 子方法未定义 vue.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49223846/

相关文章:

vue.js - 在 Vue Select 中设置选定值的样式

javascript - 如何将动态 Prop 绑定(bind)到 VueJS 2 中的动态组件

javascript - 为什么我不能将 save() 保存到这个 Parse.Object?

javascript - 一次显示一张图像?

javascript - 如何使用 Vue.js 组件从 Firestore 中删除图像

javascript - 如何配置 Vue 路由器来响应查询字符串?

javascript - 更新 Vue 出现错误 window.Vue.use is not a function

javascript - 部署云函数时出现 Nuxt.js 错误

javascript - Python Beautifulsoup 抓取包含 Javascript 的页面

javascript - 'function1(data, function2)' 是如何工作的?