代码之家  ›  专栏  ›  技术社区  ›  code-8

Vue.js:属性或方法未在实例上定义,但在渲染过程中被引用

  •  0
  • code-8  · 技术社区  · 4 年前

    我有

    <template>
        <div class="dashboard">
            <Navbar />
            <MainPanel :title="Dashboard" :icon="dasboard" />
        </div>
    </template>
    
    <script>
    import MainPanel from '../../components/MainPanel'
    import Navbar from '../../components/Navbar'
    
    export default {
        name: 'Dashboard',
        components: {
            Navbar,
            MainPanel
        }
    }
    </script>
    

    正如您所看到的,我正在尝试重用我的MainPanel组件。

    主面板

    <template>
        <v-container fluid class="my-5">
            <v-row>
                <v-flex col-xs-12>
                    <v-card elevation="2" class="pb-15">
                        <v-flex xs12 class="text-center">
                            <v-card-title>
                                <v-btn dark text color="black">
                                    <v-icon right class="mr-2">{{ icon }}</v-icon>
                                    <span>{{ title }}</span>
                                </v-btn>
                            </v-card-title>
                        </v-flex>
                    </v-card>
                </v-flex>
            </v-row>
        </v-container>
    </template>
    <script>
    export default {
        name: 'MainPanel',
        props: {
            icon: String,
            title: String
        },
        data() {
            return {
                icon: '',
                title: ''
            }
        }
    }
    </script>
    <style lang=""></style>
    

    在控制台中,我一直收到这个错误

    [Vue warn]:属性或方法“dasboard”未在实例上定义,但在渲染过程中被引用。通过初始化该属性,确保该属性是被动的,无论是在数据选项中,还是对于基于类的组件。

    enter image description here

    有人能给我一些提示吗?

    2 回复  |  直到 4 年前
        1
  •  4
  •   Raffobaffo    4 年前

    这似乎是一个非常简单的错误:

    :title="Dashboard"
    

    在Vue中使用列( : )要在组件上预置道具或属性,将使用道具或数据中定义的属性。 而如果你使用 title="dashboard" 你实际上会传递一个字符串,这就是你想要的

        2
  •  1
  •   Boussadjra Brahim    4 年前

    使用绑定标志 : 当在props中使用原始值时,意味着值应作为属性出现在脚本选项中:

     <MainPanel title="Dashboard" icon="dasboard" />
    
        3
  •  1
  •   tony19 thanksd    3 年前

    相符合的 Vue2 official guide , props 可以简单地从属性中获取值或通过 v-bind 以实现响应。

    因此,在这种情况下,您可能需要使用 title 相反 :title .

    <MainPanel title="Dashboard" icon="dasboard" />
    

    如果你想让MainPanel的标题可以相应地更改,那么你可以在中插入一个变量 data 然后经过 v-bind :

    <MainPanel :title="varaibleForTitle" :icon="varaibleForIcon" />
    

    顺便说一下,您可以通过 v-bind -尽管我不明白为什么会有人想这么做。

    <MainPanel :title="'MainPanel'" :icon="'dasboard'" />
    

    干杯