完成自定义组件的学习,接下来需要在todolist项目中实践

new_branch1
roger 2 years ago
parent a7eee22755
commit 590facbd52
  1. 64
      20_脚手架/vue_code/09.自定义事件/App.vue
  2. BIN
      20_脚手架/vue_code/09.自定义事件/assets/logo.png
  3. 35
      20_脚手架/vue_code/09.自定义事件/components/School.vue
  4. 52
      20_脚手架/vue_code/09.自定义事件/components/Student.vue
  5. 16
      20_脚手架/vue_code/09.自定义事件/main.js
  6. 20
      20_脚手架/vue_code/src/App.vue
  7. 18
      20_脚手架/vue_code/src/components/Student.vue

@ -0,0 +1,64 @@
<template>
<div class="app">
<h1>{{ msg }}{{stuName}}</h1>
<!-- 通过父组件给子组件传递函数间接获取子组件数据 -->
<School :getSchoolName="getSchoolName"/>
<!-- 通过父组件给子组件绑定一个自定义事件实现给父传递数据使用v-on或@方式 -->
<!-- <Student v-on:studentName="getStudentName"/>-->
<Student @studentName="getStudentName" @demo="method1" @click.native="show"/>
<!-- <Student @studentName.once="getStudentName"/>-->
<!-- 通过父组件给子组件绑定一个自定义事件实现给父传递数据,使用ref方式 -->
<!-- <Student ref="student"/>-->
</div>
</template>
<script>
import School from "./components/School.vue";
import Student from "./components/Student.vue";
export default {
name: "App",
components: {
School,
Student
},
data() {
return {
msg: "Hello:",
stuName: ""
}
},
methods: {
getSchoolName(schoolName) {
console.log("获取学校名称", schoolName)
},
getStudentName(name, ...params) {
console.log("getStudentName", name, params)
this.stuName = name
},
method1() {
console.log("demo自定义事件被调用!")
},
show() {
alert("组件点击事件")
}
},
mounted() {
//
// this.$refs.student.$on("studentName", this.getStudentName)
//
//
// this.$refs.student.$once("studentName", this.getStudentName)
// setInterval(() => {
// this.$refs.student.$on("studentName", this.getStudentName)
// }, 3000)
}
}
</script>
<style>
.app {
background-color: gray;
padding: 15px
}
</style>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

@ -0,0 +1,35 @@
<template>
<div class="school">
<h2>学校名称{{ name }}</h2>
<h2>学校地址{{ address }}</h2>
<!-- 通过父组件给子组件传递函数间接获取子组件数据 -->
<button @click="sendSchoolName">把学校名称传给App</button>
</div>
</template>
<script>
export default {
name: "School",
props: ["getSchoolName"],
data() {
return {
name: "修仙学院",
address: "长白山",
}
},
methods: {
sendSchoolName() {
this.getSchoolName(this.name)
}
}
}
</script>
<style scoped>
.school {
background-color: skyblue;
padding: 15px
}
</style>

@ -0,0 +1,52 @@
<template>
<div class="student">
<h2>学生名称{{ name }}</h2>
<h2>学生年龄{{ age }}</h2>
<h2>当前序号{{number}}</h2>
<button @click="sendStudentName">把学生名称传给App</button>
<button @click="unbind">解绑studentName事件</button>
<button @click="death">销毁当前Student组件的实例</button>
<button @click="num">number+1</button>
</div>
</template>
<script>
export default {
name: "Student",
data() {
return {
name: "小强",
age: 18,
number: 0
}
},
methods: {
sendStudentName() {
// StudentstudentName
this.$emit("studentName", this.name, 666, 777, 888)
this.$emit("demo")
},
unbind() {
// this.$off("studentName") //
this.$off(["studentName", "demo"]) //
this.$off() //
},
death() {
this.$destroy() // Student
},
num() {
console.log("num被调用")
this.number++
}
}
}
</script>
<style lang="css" scoped>
.student {
background-color: pink;
padding: 15px;
margin-top: 20px;
}
</style>

@ -0,0 +1,16 @@
// 引入Vue
import Vue from "vue";
// 引入App
import App from "./App";
// 设置Vue
Vue.config.productionTip = false
// 实例化Vue
new Vue({
components: {
App
},
render: h => h(App)
}).$mount('#app')

@ -1,13 +1,14 @@
<template>
<div class="app">
<h1>{{ msg }}</h1>
<h1>{{ msg }}{{stuName}}</h1>
<!-- 通过父组件给子组件传递函数间接获取子组件数据 -->
<School :getSchoolName="getSchoolName"/>
<!-- 通过父组件给子组件绑定一个自定义事件实现给父传递数据使用v-on或@方式 -->
<!-- <Student v-on:studentName="getStudentName"/>-->
<!-- <Student @studentName="getStudentName"/>-->
<Student @studentName="getStudentName" @demo="method1" @click.native="show"/>
<!-- <Student @studentName.once="getStudentName"/>-->
<!-- 通过父组件给子组件绑定一个自定义事件实现给父传递数据,使用ref方式 -->
<Student ref="student"/>
<!-- <Student ref="student"/>-->
</div>
</template>
@ -23,7 +24,8 @@ export default {
},
data() {
return {
msg: "Hello:"
msg: "Hello:",
stuName: ""
}
},
methods: {
@ -32,12 +34,18 @@ export default {
},
getStudentName(name, ...params) {
console.log("getStudentName", name, params)
this.stuName = name
},
method1() {
console.log("demo自定义事件被调用!")
},
show() {
alert("组件点击事件")
}
},
mounted() {
//
this.$refs.student.$on("studentName", this.getStudentName)
// this.$refs.student.$on("studentName", this.getStudentName)
//
//
// this.$refs.student.$once("studentName", this.getStudentName)

@ -2,7 +2,11 @@
<div class="student">
<h2>学生名称{{ name }}</h2>
<h2>学生年龄{{ age }}</h2>
<h2>当前序号{{number}}</h2>
<button @click="sendStudentName">把学生名称传给App</button>
<button @click="unbind">解绑studentName事件</button>
<button @click="death">销毁当前Student组件的实例</button>
<button @click="num">number+1</button>
</div>
</template>
@ -13,12 +17,26 @@ export default {
return {
name: "小强",
age: 18,
number: 0
}
},
methods: {
sendStudentName() {
// StudentstudentName
this.$emit("studentName", this.name, 666, 777, 888)
this.$emit("demo")
},
unbind() {
// this.$off("studentName") //
this.$off(["studentName", "demo"]) //
this.$off() //
},
death() {
this.$destroy() // Student
},
num() {
console.log("num被调用")
this.number++
}
}
}

Loading…
Cancel
Save