Laravel Mix更新后,无法将父组件的对象作为prop在Vue中传递

问题描述 投票:0回答:1

我最近将项目中的laravel mix更新为最新版本,并立即被Vue错误淹没。我似乎特别为此挣扎。我有一个组件:

<template>
    <div>
        <ChildComponent :context="this"></ChildComponent>
    </div>
</template>
<script>
    import ChildComponent from './child-component';
    export default {
        components: { ChildComponent },
        ...
    }
</script>

这是我得到的错误:Error in render: "TypeError: Cannot read property 'context' of undefined"。似乎很奇怪,因为在vue-devtools中,父组件在ChildComponent的context属性中作为对象存在。我可能还应该添加上下文,在ChildComponent的props中定义为context: {}

javascript vue.js laravel-mix
1个回答
0
投票

this未在<template>中定义,并且尝试将整个Vue实例传递给孩子似乎是一种反模式。您应该显式定义数据和变量并将其传递给子组件,并且(可选)如果要从子组件接收一些数据,则应发出事件并绑定到父组件中的那些事件。

© www.soinside.com 2019 - 2024. All rights reserved.