您的位置:首页 > Web前端 > Vue.js

3. Vue 监听子组件(事件抛出值)

2020-07-13 05:55 37 查看

Vue 监听子组件(事件抛出值)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue 监听子组件</title>
</head>
<body>
<div id="app">
<div :style="{ fontSize: font_size+'px'}">
<blog-post
v-for="(post, index) in posts"
:key='index'
:post="post"
@set_font_size="setFontSize"
></blog-post>
</div>
</div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
Vue.component('blog-post', {
props: ['post'],
template: `
<div class="blog-post">
<h3>{{ post.title }}</h3>
<div v-html="post.content"></div>
<button @click="$emit('set_font_size', 12)">set font size 12px</button>
<button @click="$emit('set_font_size', 18)">set font size 18px</button>
<button @click="$emit('set_font_size', 24)">set font size 24px</button>
</div>
`
})
app = new Vue({
el: '#app',
data: {
font_size: 16,
posts: [
{ id: 1, title: 'My journey with Vue', content:'1..' },
{ id: 2, title: 'Blogging with Vue', content:'2..' },
{ id: 3, title: 'Why Vue is so fun', content:'3..' }
]
},
methods: {
setFontSize(size){
this.font_size = size
}
},
})
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: