当前代码更改所有行和特定行的字体:
Vue.component('blog-post', {
props: ['post'],
template: `
<div class="blog-post row">
<h3 class="cell"> {{ post.title }}</h3>
<button @click="$emit('enlarge-text')">Enlarge text</button>
<div v-html="post.content" class="cell"></div>
</div>
`,
});
new Vue({
el : '#blog-post-demo',
data : {
posts : [
{id: 1, title : 'My Journey to Africa', content : 'I am the post'},
{id: 2, title : 'My Journey to America', content : 'I am the post'},
{id: 3, title : 'My Journey to Antartica', content : 'I am the post'},
{id: 4, title : 'My Journey to Asia', content : 'I am the post'},
],
postFontSize : 1,
}
});
.row {
background-color: cyan;
}
.cell {
background-color: antiquewhite;
}
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js"></script>
<div id="blog-post-demo">
<blog-post v-for="post in posts" :post="post" :key="post.id" :style="{fontSize : postFontSize + 'em'}" @enlarge-text="postFontSize += 0.1"></blog-post>
</div>
我该如何操作特定的一行,一行,更新一行的字体大小和所有行的字体?
尝试了以下操作,但无效:
Vue.component('blog-post', {
props: ['post'],
template: `
<div class="blog-post row" :style="{fontSize : postFontSize + 'em'}" @enlarge-text="postFontSize += 0.1">
<h3 class="cell">{{ post.title }}</h3>
<button @click="$emit('enlarge-text')">Enlarge text</button>
<div v-html="post.content" class="cell"></div>
</div>
`,
});
new Vue({
el : '#blog-post-demo',
data : {
posts : [
{id: 1, title : 'My Journey to Africa', content : 'I am the post'},
{id: 2, title : 'My Journey to America', content : 'I am the post'},
{id: 3, title : 'My Journey to Antartica', content : 'I am the post'},
{id: 4, title : 'My Journey to Asia', content : 'I am the post'},
],
postFontSize : 1,
}
})
.row {
background-color: cyan;
}
.cell {
background-color: antiquewhite;
}
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js"></script>
<div id="blog-post-demo">
<blog-post v-for="post in posts" :post="post" :key="post.id"></blog-post>
</div>
答案 0 :(得分:1)
如果您只想更新1行,则postFontSize
应该是blog-post
组件的本地数据
<!DOCTYPE html>
<html>
<head>
<title>My first Vue app</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
<style>
.row {
background-color: cyan;
}
.cell {
background-color: antiquewhite;
}
</style>
</head>
<body>
<div id="blog-post-demo">
<blog-post v-for="post in posts" :post="post" :key="post.id"></blog-post>
</div>
<script>
Vue.component('blog-post', {
props: ['post'],
template: `
<div class="blog-post row" :style="{fontSize : postFontSize + 'em'}">
<h3 class="cell"> {{ post.title }}</h3>
<button @click="postFontSize += 0.1">Enlarge text</button>
<div v-html="post.content" class="cell"></div>
</div>`,
data() {
return {
postFontSize : 1
}
}
})
new Vue({
el : '#blog-post-demo',
data : {
posts : [
{id: 1, title : 'My Journey to Africa', content : 'I am the post'},
{id: 2, title : 'My Journey to America', content : 'I am the post'},
{id: 3, title : 'My Journey to Antartica', content : 'I am the post'},
{id: 4, title : 'My Journey to Asia', content : 'I am the post'},
]
}
})
</script>
</body>
</html>
答案 1 :(得分:1)
您的第一个代码段不起作用的原因是因为您将postFontSize
变量放置在其父组件中。这就是为什么所有子组件都共享相同的变量的原因,这意味着变量中的一项更改将影响每个子组件。
要解决此问题,只需将与blog-post
组件相关的所有变量移到blog-post
组件中(而不是在其父组件中)。
这包括postFontSize
,@click
方法和:style
声明:
Vue.component('blog-post', {
template: `
<div class="blog-post row" :style="{ fontSize: postFontSize + 'em' }">
<h3 class="cell">{{ post.title }}</h3>
<button @click="enlargeText">Enlarge text</button>
<div v-html="post.content" class="cell"></div>
</div>
`,
props: ['post'],
data: function(){
return {
postFontSize: 1
}
},
methods: {
enlargeText: function(){
this.postFontSize += 0.1;
}
}
});
new Vue({
el : '#blog-post-demo',
data : {
posts : [
{id: 1, title : 'My Journey to Africa', content : 'I am the post'},
{id: 2, title : 'My Journey to America', content : 'I am the post'},
{id: 3, title : 'My Journey to Antartica', content : 'I am the post'},
{id: 4, title : 'My Journey to Asia', content : 'I am the post'},
]
}
});
.row {
background-color: cyan;
}
.cell {
background-color: antiquewhite;
}
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js"></script>
<div id="blog-post-demo">
<blog-post v-for="post in posts" :post="post" :key="post.id"></blog-post>
</div>