javascript - Vuejs TypeError:无法读取未定义的属性“占位符”
问题描述
我有一个包含 2 个子组件的索引模板,post-status
并且status-preview
我正在将几个道具传递给我的post-status
模板routes
user
和locale
<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>
这只是一个数据数组、表单路由、要翻译的语言环境以及包含一些用户信息的用户对象
不,当我呈现页面时,占位符工作正常:
https://i.imgur.com/GfGTPeP.png
但我收到 2 个警告:
https://i.imgur.com/xvb1nLB.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>
所有数据均已设置:
https://i.imgur.com/ztpEQCr.png https://i.imgur.com/pSStYoK.png
当它似乎工作正常时,是什么导致了这个警告?
解决方案
看一下Vue 实例生命周期图:
该mounted
阶段发生在第一次 DOM 渲染之后。在设置locale
. 所以这就是发生的事情:
- 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
mounted
您可能应该使用 , 而不是使用,created
以确保在第一次渲染之前设置这些道具。
推荐阅读
- gradle - 子依赖模块中的 Gradle 库依赖关系
- java - 如果文件已经下载,如何防止从 url 下载文件
- protractor - 无法运行 package.json 文件中的 postInstall 以更新量角器节点模块中的 Web 驱动程序管理器
- ecmascript-6 - ES6 扩展运算符输出
- android - "," 在 android 软键盘中显示为 ".-"
- java - 输入无效时禁用 JFace-InputDialog 的 OK 按钮
- html - 椭圆形镂空形状元素
- c - 如何使用 Eclipse/gcc/ac6/sw4stm32 在 STM32 上使用自定义回调重新定位 printf?
- asp.net - ASP.net MVC 表单发布安全
- visual-studio - 有什么方法可以保存在 Visual Basic 6.0 中制作的程序的复选框状态