因此,我有一个Vuejs书籍组件,它获得了一个“书”道具,它是一个带有标题,颜色,章节等的对象。下面是代码。该模板工作正常,并能够创建章节。我想要做的是在挂载上设置导航颜色。VueJS道具意外行为
Vue.component('book', {
props: ['book'],
template: `
<section class="book">
<h1 :id="book.title"><i class="fa fa-book"></i> {{ book.title }} {{ book.description }}</h1>
<chapter v-for="chapter in book.chapter" :chapter="chapter"></chapter>
</section>
`,
mounted: function() {
console.log(this);
console.log(this.book);
document.getElementById("nav").style.backgroundColor = '#' + this.book.color;
}
});
在安装功能,console.log(this)
表明组件正确地与观察者的道具,所有的属性,但是打印时this.book
只观测显示出来,这就是为什么this.book.color
不起作用。这种不一致的原因是什么?为什么组件中的图书对象不是this.book
?
你如何查看数据控制台?通常数据在您查看时进行评估,而不是在您登录时进行评估。另外,为什么不从一开始就简单地在'CSS'类中添加颜色,或者像这样:https://vuejs.org/v2/guide/class-and-style.html#Object-Syntax-1 – Cristy
backgroundColor是对于只是常规html元素的导航栏。 – arlyon
尝试在'this。$ nextTick()'中改变颜色。 – Cristy