javascript - Vuejs 类型错误 : Cannot read property 'placeholder' of undefined"

标签 javascript vue.js vuejs2

我有一个索引模板,它有 2 个子组件:post-statusstatus-preview

我将一些 Prop 传递给我的 post-status 模板 routes userlocale

<post-status
    :user="user"
    :routes="routes"
    :locale="locale">
</post-status>

post-status.vue

<template>
    <div>
        <div class="bg-blue-lightest p-4">
            <form method="post" @submit.prevent="postStatus">
                <div class="flex">
                    <img :src="user.avatar" class="rounded-full w-8 h-8 items-end mr-3">
                    <textarea v-model="form.post" name="post" rows="5" class="w-full items-center rounded border-2 border-blue-lighter" :placeholder="locale.status.placeholder"></textarea>
                </div>
                <div class="ml-8 flex mt-4">
                    <div class="w-1/2 flex justify-start px-3">
                        <div class="mr-3">
                            <i class="text-blue-light fa fa-picture-o fa-2x"></i>
                        </div>
                        <div class="mr-3">
                            <i class="text-blue-light fa fa-bar-chart fa-2x"></i>
                        </div>
                        <div>
                            <i class="text-blue-light fa fa-map-pin fa-2x"></i>
                        </div>
                    </div>
                    <div class="w-1/2 flex justify-end">
                        <button class="bg-teal hover:bg-teal-dark text-white font-medium py-2 px-4 rounded-full">Tweet</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</template>

<script>
    export default {
        props: ['user', 'routes', 'locale'],
        data() {
            return {
                form: {
                    'post': '',
                },
            }
        },
        methods: {
            postStatus: function() {
                axios
                    .post(routes.store, this.form)
                    .then(response => {
                        console.log(response)
                    })
            },
        }
    }
</script>

<style scoped>

</style>

这只是一个数据数组、表单路由、要翻译的区域设置以及带有一些用户信息的用户对象

不,当我渲染页面时,占位符工作正常:

/image/oNZKE.png

但我收到 2 个警告:

/image/Lhqdp.png

我的完整索引页:

<template>
    <div>
        <post-status
            :user="user"
            :routes="routes"
            :locale="locale">
        </post-status>

        <status-preview v-for="status in statuses"
            :key="status.id"
            :name="status.owner.name"
            :username="status.owner.username"
            :created="status.created_at"
            :post="status.post">
        </status-preview>
    </div>
</template>


<script>

    import StatusPreview from "./status-preview.vue";
    import PostStatus from "./post-status.vue";

    export default {
        components: {
            'status-preview': StatusPreview,
            'post-status': PostStatus
        },
        data() {
            return {
                routes: [],
                statuses: [],
                locale: [],
                user: [],
                completed : false,
                progress : false,
                page: 1
            }
        },
        methods: {
            getStatuses: function() {
                let url = this.routes.index + '?page=' + this.page
                axios
                    .get(url, {
                        page: this.page
                    })
                    .then(response => {
                        if (response.data.length) {
                            this.statuses = this.statuses.concat(response.data);
                            this.progress = false;
                        } else {
                            this.progress = false;
                            this.completed = true;
                        }
                        this.page += this.page;
                    });
            },
            infiniteScroll: function() {
                if (!this.completed &&  !this.progress) {
                    this.getStatuses()
                }

            },
        },
        mounted() {
            this.routes = routes
            this.locale = locale
            this.user = user
            this.getStatuses()
            window.addEventListener('scroll', this.infiniteScroll);
        }
    }
</script>

所有数据已设置:

/image/yPiw2.png /image/cTEra.png

当它看起来工作正常时,是什么导致了这个警告?

最佳答案

看看Vue Instance Lifecycle Diagram :

Vue Instance Lifecycle Diagram

mounted 阶段发生在第一次 DOM 渲染之后。在设置您的区域设置之前您需要等待它。所以这就是发生的事情:

- Your template is evaluated
- `this.locale` is still equal to your initial `[]`
- You get an error in your console saying `locale.status` is undefined
- The `mounted` event handler is executed, which populates your `locale` prop
- The component re-renders, since its props have changed
- `locale` is now set, your placeholder works

您可能应该使用created,而不是使用mounted,以确保在第一次渲染之前设置这些 Prop 。

关于javascript - Vuejs 类型错误 : Cannot read property 'placeholder' of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50892043/

相关文章:

javascript - 使用 d3 - 单击按钮时如何从数组中选择特定数据以突出显示?

webpack - 如何让 Flow 与 Vue 2 (webpack) 一起正常工作?

javascript - Element FE/Element UI TimePicker - 12 小时格式

django-forms - 来自 Django 的 Vue 动态 html 表单

javascript - 如何在vue js中单击div时隐藏特定div

javascript - 将用户输入从文本框附加到新元素

javascript - 在 Javascript 中创建一个按钮

vue.js - Ref 不适用于 Vue3 中的自定义组件

javascript - 分页和过滤分别运行/Vue

javascript - 添加事件监听vuejs