2.使用时,注意及时销毁事件监听,比如,页面 onLoad 里边 uni.$on 注 册 监 听 ,onUnload里边uni.$off 移除,或者一次性的事件,直接使用 uni.$once 监听
A页面传值,使用uni.$emit
B页面接收值,使用uni.$on或uni.$once监听
- <script>
- export default {
- data() {
- return {};
- },
- onLoad() {
- uni.$emit('dianji', {
- name:'老王'
- });
- }
- };
- </script>
当访问A页面的时候,会自动触发“dianji”事件,此时B页面就会监听到,而去触发你的逻辑
- script>
- export default {
- data() {
- return {
- name:''
- };
- },
- onLoad() {
- // 监听事件
- uni.$on('dianji',function(data){
- this.name=data.name
- console.log('A页面传的值为:' + data.name);
- });
- },
- onUnload() {
- // 移除监听事件
- uni.$off('dianji');
- }
- };
- </script>
文章点评