订阅事件学习

new_branch1
roger 2 years ago
parent 46b4084acc
commit 40ce48023a
  1. 128
      20_脚手架/vue_code/12.src_todolist全局事件总线/App.vue
  2. BIN
      20_脚手架/vue_code/12.src_todolist全局事件总线/assets/logo.png
  3. 0
      20_脚手架/vue_code/12.src_todolist全局事件总线/components/TodoBottom.vue
  4. 0
      20_脚手架/vue_code/12.src_todolist全局事件总线/components/TodoHeader.vue
  5. 0
      20_脚手架/vue_code/12.src_todolist全局事件总线/components/TodoItem.vue
  6. 0
      20_脚手架/vue_code/12.src_todolist全局事件总线/components/TodoList.vue
  7. 19
      20_脚手架/vue_code/12.src_todolist全局事件总线/main.js
  8. 120
      20_脚手架/vue_code/src/App.vue
  9. 47
      20_脚手架/vue_code/src/components/School.vue
  10. 35
      20_脚手架/vue_code/src/components/Student.vue

@ -0,0 +1,128 @@
<template>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<TodoHeader @addTodo="addTodo"/>
<TodoList :todos="todos"/>
<TodoBottom :todos="todos" @checkAllTodo="checkAllTodo" @clearAllTodo="clearAllTodo"/>
</div>
</div>
</div>
</template>
<script>
import TodoHeader from "./components/TodoHeader.vue";
import TodoList from "./components/TodoList.vue";
import TodoBottom from "./components/TodoBottom.vue";
export default {
name: "App",
components: {
TodoHeader,
TodoList,
TodoBottom
},
data() {
return {
//
todos: JSON.parse(localStorage.getItem('todos')) || []
}
},
// 访App
// App
methods: {
// todoTodoHeader
addTodo(todoObj) {
this.todos.unshift(todoObj)
},
// todo穿TodoListTodoItem
checkTodo(id) {
this.todos.forEach((todo) => {
if (todo.id === id) todo.done = !todo.done
})
},
// todo穿TodoListTodoItem
deleteTodo(id) {
this.todos = this.todos.filter(todo => todo.id !== id)
},
// todoTodoBottom
checkAllTodo(done) {
this.todos.forEach((todo) => todo.done = done)
},
// todoTodoBottom
clearAllTodo() {
this.todos = this.todos.filter((todo) => {
return !todo.done
})
}
},
watch: {
todos: {
deep: true,
handler(value) {
localStorage.setItem('todos', JSON.stringify(value))
}
}
},
// 线
mounted() {
this.$bus.$on('checkTodo', this.checkTodo)
this.$bus.$on('deleteTodo', this.deleteTodo)
},
// 线
beforeDestroy() {
this.$bus.$off('checkTodo')
this.$bus.$off('deleteTodo')
}
}
</script>
<style>
body {
background: #fff;
}
.btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
</style>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

@ -0,0 +1,19 @@
// 引入Vue
import Vue from "vue";
// 引入App
import App from "./App";
// 设置Vue
Vue.config.productionTip = false
// 实例化Vue
new Vue({
components: {
App
},
render: h => h(App),
beforeCreate() {
Vue.prototype.$bus = this // 安装全局事件总线
}
}).$mount('#app')

@ -1,128 +1,32 @@
<template> <template>
<div id="root"> <div class="app">
<div class="todo-container"> <h1>{{ msg }}</h1>
<div class="todo-wrap"> <School/>
<TodoHeader @addTodo="addTodo"/> <Student/>
<TodoList :todos="todos"/>
<TodoBottom :todos="todos" @checkAllTodo="checkAllTodo" @clearAllTodo="clearAllTodo"/>
</div>
</div>
</div> </div>
</template> </template>
<script> <script>
import TodoHeader from "./components/TodoHeader.vue"; import School from "./components/School.vue";
import TodoList from "./components/TodoList.vue"; import Student from "./components/Student.vue";
import TodoBottom from "./components/TodoBottom.vue";
export default { export default {
name: "App", name: "App",
components: { components: {
TodoHeader, School,
TodoList, Student
TodoBottom
}, },
data() { data() {
return { return {
// msg: "Hello:",
todos: JSON.parse(localStorage.getItem('todos')) || []
}
},
// 访App
// App
methods: {
// todoTodoHeader
addTodo(todoObj) {
this.todos.unshift(todoObj)
},
// todo穿TodoListTodoItem
checkTodo(id) {
this.todos.forEach((todo) => {
if (todo.id === id) todo.done = !todo.done
})
},
// todo穿TodoListTodoItem
deleteTodo(id) {
this.todos = this.todos.filter(todo => todo.id !== id)
},
// todoTodoBottom
checkAllTodo(done) {
this.todos.forEach((todo) => todo.done = done)
},
// todoTodoBottom
clearAllTodo() {
this.todos = this.todos.filter((todo) => {
return !todo.done
})
}
},
watch: {
todos: {
deep: true,
handler(value) {
localStorage.setItem('todos', JSON.stringify(value))
}
} }
},
// 线
mounted() {
this.$bus.$on('checkTodo', this.checkTodo)
this.$bus.$on('deleteTodo', this.deleteTodo)
},
// 线
beforeDestroy() {
this.$bus.$off('checkTodo')
this.$bus.$off('deleteTodo')
} }
} }
</script> </script>
<style> <style>
body { .app {
background: #fff; background-color: gray;
} padding: 15px
.btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
} }
</style> </style>

@ -0,0 +1,47 @@
<template>
<div class="school">
<h2>学校名称{{ name }}</h2>
<h2>学校地址{{ address }}</h2>
</div>
</template>
<script>
import pubsub from 'pubsub-js'
export default {
name: "School",
data() {
return {
name: "修仙学院",
address: "长白山",
}
},
methods: {
subMethod(msgName, data) {
console.log('School发布了hello消息,hello消息的回调被执行了', data)
}
},
mounted() {
// this.$bus.$on('hello', (data)=>{
// console.log('School', data)
// })
// 使
// this.pubId = pubsub.subscribe('hello', (msgName, data) => {
// console.log('Schoolhellohello', data)
// })
this.pubId = pubsub.subscribe('hello', this.subMethod)
},
beforeDestroy() {
// this.$bus.off('hello')
pubsub.unsubscribe(this.pubId)
}
}
</script>
<style scoped>
.school {
background-color: skyblue;
padding: 15px
}
</style>

@ -0,0 +1,35 @@
<template>
<div class="student">
<h2>学生名称{{ name }}</h2>
<h2>学生年龄{{ age }}</h2>
<button @click="sendStudentName">发送数据给School组件</button>
</div>
</template>
<script>
import pubsub from 'pubsub-js'
export default {
name: "Student",
data() {
return {
name: "小强",
age: 18
}
},
methods:{
sendStudentName() {
// this.$bus.$emit('hello', this.name)
pubsub.publish('hello', 666)
}
}
}
</script>
<style lang="css" scoped>
.student {
background-color: pink;
padding: 15px;
margin-top: 20px;
}
</style>
Loading…
Cancel
Save