1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <template>
- <div class="student">
- <h2>学生姓名:{{ name }}</h2>
- <h2>学生性别:{{ sex }}</h2>
- <h2>当前求和为:{{ number }}</h2>
- <button @click="add">点我number++</button>
- <button @click="sendStudentName">把学校名给App</button>
- <button @click="unbind">解绑atguigu事件</button>
- <button @click="death">销毁当前Student组件的实例对象(vc)</button>
- </div>
- </template>
- <script>
- export default {
- name:'Student',
- data() {
- return {
- name:'张三',
- sex:'男',
- number:0
- }
- },
- methods:{
- add(){
- console.log('add回调被调用了');
- this.number++
- },
- sendStudentName(){
- //触发Student组件实例对象上的atguigu事件
- // this.$emit('atguigu',this.name)
- // 当传递多个参数时,逐个传递,在父组件逐个接收(一般不这样使用)
- // this.$emit('atguigu',this.name,666,888,999)
- // 常用方式一:将要传递的内容包装成一个对象传递
- // 常用方式二:传递的时候正常传递,在接收时处理
- this.$emit('atguigu',this.name,666,888,999)
- // this.$emit('demo')
- },
- // 解绑自定义事件
- unbind(){
- this.$off('atguigu') //只适用于解绑一个自定义事件
- // this.$off(['atguigu','demo']) //解绑多个自定义事件
- // this.$off() //解绑所有的自定义事件
- },
- death(){
- this.$destroy() //销毁了当前Student组件的实例,销毁后,所有Student组件实例的自定义事件全都不奏效。
- }
- }
- }
- </script>
- <style scoped>
- .student{
- background-color: pink;
- padding: 5px;
- margin-top: 30px;
- }
- </style>
|