父组件 App.vue
<template>
<div id="app">
<!-- 父组件的@btnClick与子组件$emit的btnClick名字相同 -->
<Emit @btnClick='deleteP'/>
<p ref='word'>我是被删的那个</p>
</div>
</template>
<script>
import Emit from './components/Emit.vue';
export default {
name: 'App',
components: {
Emit
},
data(){
return{
}
},
methods:{
//通过args接收子组件传来的参数
deleteP(args){
console.log('我接收到子组件传递的事件了')
console.log(args);
//refs拿到节点的唯一word值
this.$refs.word.remove();
}
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
子组件 components/Emit.vue
<template>
<div class="hello">
<button @click="say">删除父组件的p标签</button>
</div>
</template>
<script>
export default {
name: 'Emit',
methods: {
say(){
console.log("明天不上班");
// 绑定父组件 自定义@btnClick事件
this.$emit('btnClick',{name:'理子', sex:'男'})
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
网友评论