当前位置:   article > 正文

vue中$refs, $emit, $on, $once, $off的使用详解_this.$once

this.$once

$once监听一个自定义事件。

$off移除自定义事件监听器。

只能监听自定义事件

推荐的清除定时器

直接在需要定时器的方法或者生命周期函数中声明并销毁,代码如下

  1. export default{
  2. methods:{
  3. fun1(){
  4. const timer = setInterval(()=>{
  5. //需要做的事情
  6. console.log(11111);
  7. },1000);
  8. this.$once('hook:beforeDestroy',()=>{
  9. clearInterval(timer);
  10. timer = null;
  11. })
  12. }
  13. }
  14. }

---------------------------------------------------------------------

1.$refs的使用场景

父组件调用子组件的方法,可以传递数据。

父组件:

  1. <div id="app">
  2. <child-a ref="child"></child-a>
  3. <button @click="getMyEvent">点击父组件</button>
  4. <div>
  5. <script>
  6. import ChildA from './child.vue'
  7. export default{
  8. components:{
  9. ChildA
  10. },
  11. data(){
  12. return {
  13. msg:'我是父组件的数据'
  14. }
  15. },
  16. methods:{
  17. getMyEvent(){
  18. //调用子组件的方法,child是上边ref起的名字,emitEvent是子组件的方法。
  19. this.$refs.child.emitEvent(this.msg)
  20. }
  21. }
  22. }
  23. </script>

子组件:

  1. <template>
  2. <button>点击我</button>
  3. </template>
  4. <script>
  5. export default{
  6. methods:{
  7. emitEvent(msg){
  8. console.log('接收的数据------'+msg)
  9. }
  10. }
  11. }
  12. </script>

2.$emit的使用

子组件调用父组件的方法并传递数据。

子组件:

  1. <template>
  2. <button @click="emitEvent">点击我</button>
  3. </template>
  4. <script>
  5. export default{
  6. data(){
  7. return{
  8. msg:'我是子组件的数据'
  9. }
  10. },
  11. methods:{
  12. emitEvent(){
  13. //通过按钮的点击事件触发方法,然后用$emit触发一个my-event的自定义方法,传递this.msg数据。
  14. this.$emit('my-event',this.msg)
  15. }
  16. }
  17. }
  18. </script>

父组件:

  1. <template>
  2. <div id="app">
  3. <child-a @my-event="getMyEvent"></child-a>
  4. //父组件通过监测my-event事件执行一个方法,然后取到子组件中传递过来的值。
  5. </div>
  6. </template>
  7. <script>
  8. import childA from './child.vue';
  9. export default {
  10. components:{
  11. childA
  12. },
  13. methods:{
  14. getMyEvent(msg){
  15. console.log('接收数据---'+msg);
  16. //接收数据,我是子组件的数据
  17. }
  18. }
  19. }
  20. </script>

3.$on的使用场景

兄弟组件之间相互传递数据。
首先创建一个Vue的空白实例(兄弟组件的桥梁)
子组件A:发送放使用$emit自定义事件把数据带过去。

  1. <template>
  2. <div>
  3. <span>A组件-{{msg}}</span>
  4. <input type="button" value="把A组件数据传递给B" @click="send">
  5. </div>
  6. </template>
  7. <script>
  8. export default{
  9. data(){
  10. return{
  11. msg:{
  12. a:'111',
  13. b:'222'
  14. }
  15. }
  16. },
  17. methods:{
  18. send(){
  19. this.$emit('aevent',this.msg)
  20. }
  21. }
  22. }
  23. </script>

子组件B:接收方通过$on监听自定义事件的callback接收数据

  1. <template>
  2. <div>
  3. <span>B组件,A传的数据为--{{msg}}</span>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. data(){
  9. return{
  10. msg:''
  11. }
  12. },
  13. mounted(){
  14. this.$on('aevent',(val)=>{//监听事件aevent,回调函数要使用箭头函数。
  15. console.log(val);//打印结果;我是a组件的数据。
  16. })
  17. }
  18. }
  19. </script>

父组件:

  1. <template>
  2. <div>
  3. <childa></childa>
  4. <br/>
  5. <childb></childb>
  6. </div>
  7. </template>
  8. <script>
  9. import childa from './childa.vue';
  10. import childb from './childb.vue';
  11. export default{
  12. componets:{
  13. childa,
  14. childb
  15. },
  16. data(){
  17. return{
  18. msg:''
  19. }
  20. }
  21. }
  22. </script>

4.$once的使用场景

参数:

  1. {string} event
  2. {Function} callback

用法:

  监听一个自定义事件,但是只触发一次。一旦触发之后,监听器就会被移除。

$once是一个函数,可以为Vue组件实例绑定一个自定义事件,但该事件只能被触发一次,触发之后随即被移除。

$once有两个参数,第一个参数为字符串类型,用来指定绑定的事件名称,第二个参数设置事件的

  1. <template>
  2. <div>
  3. <button @click="$emit('jpf')">按钮</button>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. mounted() {
  9. this.$once('jpf', () => {
  10. console.log('once');
  11. });
  12. }
  13. }
  14. </script>

$once可以多次为同一个事件绑定多个回调,触发时,回调函数按照绑定顺序依次执行。

  1. <template>
  2. <div>
  3. <button @click="$emit('jpf')">按钮</button>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. mounted() {
  9. this.$once('jpf', () => {
  10. console.log('1');
  11. });
  12. this.$once('jpf', () => {
  13. console.log('2');
  14. });
  15. }
  16. }
  17. </script>

  1. // 修饰符
  2. <a-button @click.once="test()" type="primary">保存</a-button>

$once, $off上面 里说的监听器 指的是 $on的监听器


5.$off的使用场景

参数:

  1. {string | Array<string>} event (只在 2.2.2+ 支持数组)
  2. {Function} [callback]

用法:
移除自定义事件监听器。

  1. 如果没有提供参数,则移除所有的事件监听器;
  2. 如果只提供了事件,则移除该事件所有的监听器;
  3. 如果同时提供了事件与回调,则只移除这个回调的监听器。

  1. // 选中返回数据
  2. handleChange(value) {
  3. this.$emit('change', value)
  4. this.$emit('Ok', value)
  5. this.$off('Ok', ()=> {
  6. console.log(1111111111111);
  7. })
  8. this.$off('change)
  9. // 不能有回调函数,第一个无效,因为 只移除这个回调的监听器
  10. },


来源:https://www.jianshu.com/p/33ac17271b43

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号