Student.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <div class="student">
  3. <h2>学生姓名:{{ name }}</h2>
  4. <h2>学生性别:{{ sex }}</h2>
  5. <h2>当前求和为:{{ number }}</h2>
  6. <button @click="add">点我number++</button>
  7. <button @click="sendStudentName">把学校名给App</button>
  8. <button @click="unbind">解绑atguigu事件</button>
  9. <button @click="death">销毁当前Student组件的实例对象(vc)</button>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. name:'Student',
  15. data() {
  16. return {
  17. name:'张三',
  18. sex:'男',
  19. number:0
  20. }
  21. },
  22. methods:{
  23. add(){
  24. console.log('add回调被调用了');
  25. this.number++
  26. },
  27. sendStudentName(){
  28. //触发Student组件实例对象上的atguigu事件
  29. // this.$emit('atguigu',this.name)
  30. // 当传递多个参数时,逐个传递,在父组件逐个接收(一般不这样使用)
  31. // this.$emit('atguigu',this.name,666,888,999)
  32. // 常用方式一:将要传递的内容包装成一个对象传递
  33. // 常用方式二:传递的时候正常传递,在接收时处理
  34. this.$emit('atguigu',this.name,666,888,999)
  35. // this.$emit('demo')
  36. },
  37. // 解绑自定义事件
  38. unbind(){
  39. this.$off('atguigu') //只适用于解绑一个自定义事件
  40. // this.$off(['atguigu','demo']) //解绑多个自定义事件
  41. // this.$off() //解绑所有的自定义事件
  42. },
  43. death(){
  44. this.$destroy() //销毁了当前Student组件的实例,销毁后,所有Student组件实例的自定义事件全都不奏效。
  45. }
  46. }
  47. }
  48. </script>
  49. <style scoped>
  50. .student{
  51. background-color: pink;
  52. padding: 5px;
  53. margin-top: 30px;
  54. }
  55. </style>