xxxxStorage.setItem('key', 'value');
xxxxStorage.getItem('person');
xxxxStorage.removeItem('key');
xxxxStorage.clear()
< Demo @zmy = "test"/ > 或
<Demo ref="demo"/>
......
mounted(){
this.$refs.xxx.$on('zmy', this.test)
}
(3) 若想让自定义事件只能触发一次,可以使用once
修饰符,或$once
方法
4. 触发自定义事件:this. $ emit('zmy', 数据)
5. 解绑自定义事件:this.$ off('zmy')
6. 组件上也可以绑定原生DOM事件,需要使用native
修饰符
7. 注意:通过 this.$ refs.xxx.$ on('zmy', 回调)
绑定自定义事件时,回调要么配置在methods中,要么用箭头函数
,否则this指向会出问题!
任意组件间通信
$on(eventName, listener)
: 绑定自定义事件监听$emit(eventName, data)
: 分发自定义事件$off(eventName)
: 解绑自定义事件监听$once(eventName, listener)
: 绑定事件监听, 但只能处理一次Vue.prototype.$bus = new Vue()
,所有的组件对象都能看到$bus 这个属性对象new Vue({
......
beforeCreate(){
Vue.prototype.$bus = this//安装全局事件总线,$bus就是当前应用的vm
},
......
})
回调留在A组件自身
。methods(){
demo(data){......}
}
......
mounted(){
this,$bus.$on('xxx', this.demo)
}
(2)提供数据:this.$ bus.$ emit('xxx', 数据)
$ off
去解绑当前组件所用到的
事件School.vue
<template>
<div class="school">
<h2>学校名称:{{ name }}</h2>
<h2>学校地址:{{ address }}</h2>
</div>
</template>
<script>
export default {
name: 'School',
props: ['getSchoolName'],
data(){
return {
name: '西安文理',
address: '西安',
}
},
mounted(){
// console.log('School', this);
this.$bus.$on('hello',(data) => {
console.log('我是School组件, 收到了数据', data);
})
},
beforeDestroy(){
this.$bus.$off('hello')
}
}
</script>
<style scoped>
.school {
background-color: skyblue;
padding: 5px;
}
</style>
Student.vue
<template>
<div class="student">
<h2>学生名称:{{ name }}</h2>
<h2>学生性别:{{ sex }}</h2>
<button @click="sendStudentName">把学生名给School组件</button>
</div>
</template>
<script>
export default {
name: 'Student',
data(){
return {
name: '张三',
sex: '男',
}
},
methods: {
sendStudentName(){
this.$bus.$emit('hello', this.name)
}
}
}
</script>
<style lang="less" scoped>
.student {
background-color: orange;
padding: 5px;
margin-top: 30px;
}
</style>
App.vue
<template>
<div class="app">
<h1>{{ msg }}</h1>
<School/>
<Student/>
</div>
</template>
<script>
import Student from './components/Student.vue'
import School from './components/School.vue'
export default {
name: 'App',
components: {School, Student},
data(){
return {
msg: '你好啊',
studentName:''
}
},
}
</script>
<style scoped>
.app{
background-color: gray;
padding: 5px;
}
</style>
main.js
//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//关闭Vue的生产提示
Vue.config.productionTip = false
/* const demo = Vue.extend({})
const d = new demo() */
//创建vm
new Vue({
el: '#app',
render: h => h(App),
beforeCreate(){
Vue.prototype.$bus = this //安装全局事件总线
}
})
任意组件通信
npm i pubsub-js
import pubsub from 'pubsub-js'
回调留在A组件自身
。methods(){
demo(data) {......}
}
......
mounted() {
this.pid = pubsub.subscribe('xxx', this.demo)//订阅消息
}
(4)提供数据:pubsub.publish('xxx', 数据)
(5)最好在beforeDestroy钩子中,用PubSub.unsubscribe(pid)去取消订阅
。
npm install -S pubsub-js
import PubSub from 'pubsub-js'
// 引入PubSub.subscribe(‘msgName’, functon(msgName, data){ })
PubSub.publish(‘msgName’, data)
: 发布消息, 触发订阅的回调函数调用PubSub.unsubscribe(token)
: 取消消息的订阅this.nextTick(回调函数)
<transition name="hello">
<h1 v-show="isShow">你好啊!</h1>
</transition>
< transition-group >
,且每个元素都要指定key
值Tset.vue
<template>
<div>
<button @click="isShow = !isShow">显示/隐藏</button>
<transition name="hello" appear>
<h1 v-show="isShow">你好啊</h1>
</transition>
</div>
</template>
<script>
export default {
name: 'Test',
data(){
return {
isShow: true
}
},
}
</script>
<style scoped>
h1{
background-color: orange;
}
.hello-enter-active{
animation: aaa 0.5s linear;
}
.hello-leave-active{
animation: aaa 0.5s linear reverse;
}
@keyframes aaa {
from{
transform: translateX(-100%);
}
to{
transform: translateX(0px);
}
}
</style>
Tset2.vue
<template>
<div>
<button @click="isShow = !isShow">显示/隐藏</button>
<transition-group name="hello" appear>
<h1 v-show="!isShow" key="1">你好啊</h1>
<h1 v-show="isShow" key="2">aaa</h1>
</transition-group>
</div>
</template>
<script>
export default {
name: 'Test',
data(){
return {
isShow: true
}
},
}
</script>
<style scoped>
h1{
background-color: orange;
}
/* 进入的起点 离开的终点*/
.hello-enter, .hello-leave-to{
transform: translateX(-100%);
}
.hello-enter-active, .hello-leave-active{
transition: 0.5s linear;
}
/* 进入的终点 离开的起点*/
.hello-enter-to, .hello-leave{
transform: translateX(0);
}
</style>
Tset3.vue
<template>
<div>
<button @click="isShow = !isShow">显示/隐藏</button>
<transition-group name="animate__animated animate__bounce" appear enter-active-class="animate__swing" leave-active-class="animate__backOutUp">
<h1 v-show="!isShow" key="1">你好啊</h1>
<h1 v-show="isShow" key="2">aaa</h1>
</transition-group>
</div>
</template>
<script>
import 'animate.css'
export default {
name: 'Test',
data(){
return {
isShow: true
}
},
}
</script>
<style scoped>
h1{
background-color: orange;
}
</style>
App.vue
<template>
<div>
<Test></Test>
<Test2></Test2>
<Test3></Test3>
</div>
</template>
<script>
import Test from './components/Test.vue'
import Test2 from './components/Test2.vue'
import Test3 from './components/Test3.vue'
export default {
name: 'App',
components: {Test, Test2, Test3},
}
</script>
main.js
//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//关闭Vue的生产提示
Vue.config.productionTip = false
//创建vm
new Vue({
el: '#app',
render: h => h(App),
beforeCreate(){
Vue.prototype.$bus = this
}
})