美文网首页
编程大白话之-vue页面之间方法的互调

编程大白话之-vue页面之间方法的互调

作者: Han涛_ | 来源:发表于2020-08-06 11:13 被阅读0次

    整理一下vue页面间常用的调用方法

    一、父组件调用子组件的方法

    1.利用ref

    // 子组件拥有方法的页面
     <template>
      <div>
        child
      </div>
    </template>
     
    <script>
      export default {
        name: "child",
        props: "someprops",
        methods: {
          parentHandleclick(e) {
            console.log(e)
          }
        }
      }
    </script>
    
    // 父组件调用方法的页面
    <template>
      <div>
        <button @click="clickParent">点击</button>
        <child ref="mychild"></child>
      </div>
    </template>
     
    <script>
      import Child from './child';
      export default {
        name: "parent",
        components: {
          child: Child
        },
        methods: {
          clickParent() {
            this.$refs.mychild.parentHandleclick("嘿嘿嘿");
          }
        }
      }
    </script>
    

    2.通过组件的emit、on方法;

    // 子组件拥有方法的页面
    <template>
        <div>我是子组件</div>
    </template>
    <script>
    export default {
        mounted() {
            this.$nextTick(function() {
                this.$on('childmethods', function() {
                    console.log('我是子组件方法');
                });
            });
         },
    };
    </script>
    
    // 父组件调用方法的页面
    <template>
        <div>
            <Button @click="handleClick">点击调用子组件方法</Button>
            <Child ref="child"/>
        </div>
    </template>    
    
    <script>
    import Child from './child';
    
    export default {
        methods: {
            handleClick() {
                  this.$refs.child.sing();
            },
        },
    }
    </script>
    

    二、子组件调用父组件的方法

    1.直接在子组件中通过this.$parent.event来调用父组件的方法

    // 父组件拥有方法的页面
    <template>
      <div>
        <child></child>
      </div>
    </template>
    <script>
      import child from '~/components/dam/child';
      export default {
        components: {
          child
        },
        methods: {
          fatherMethod() {
            console.log('测试');
          }
        }
      };
    </script>
    
    // 子组件调用方法的页面
    <template>
      <div>
        <button @click="childMethod()">点击</button>
      </div>
    </template>
    <script>
      export default {
        methods: {
          childMethod() {
            this.$parent.fatherMethod();
          }
        }
      };
    </script>
    

    2.在子组件中使用$emit向父组件触发一个事件,父组件监听这个事件

    // 父组件拥有方法的页面
    <template>
      <div>
        <child @fatherMethod="fatherMethod"></child>
      </div>
    </template>
    <script>
      import child from '~/components/dam/child';
      export default {
        components: {
          child
        },
        methods: {
          fatherMethod() {
            console.log('测试');
          }
        }
      };
    </script>
    
    // 子组件调用方法的页面
    <template>
      <div>
        <button @click="childMethod()">点击</button>
      </div>
    </template>
    <script>
      export default {
        methods: {
          childMethod() {
            this.$emit('fatherMethod');
          }
        }
      };
    </script>
    

    3.父组件把方法传入子组件中,在子组件里直接调用这个方法

    // 父组件拥有方法的页面
    
    <template>
      <div>
        <child :fatherMethod="fatherMethod"></child>
      </div>
    </template>
    <script>
      import child from '~/components/dam/child';
      export default {
        components: {
          child
        },
        methods: {
          fatherMethod() {
            console.log('测试');
          }
        }
      };
    </script>
    
    // 子组件调用方法的页面
    <template>
      <div>
        <button @click="childMethod()">点击</button>
      </div>
    </template>
    <script>
      export default {
        props: {
          fatherMethod: {
            type: Function,
            default: null
          }
        },
        methods: {
          childMethod() {
            if (this.fatherMethod) {
              this.fatherMethod();
            }
          }
        }
      };
    </script>
    

    三、兄弟组件相互调用方法

    // 父组件起到中间作用
    <template>
        <child1 @save="save"></child1>
        <child2 ref="child2Container"></child2>
    </template>
    <script>
        import child1 from './components/child1';
        import child2 from './components/child2';
        export default {
            name: 'App',
            components: {
                child1,
                child2
            },
            methods: {
                save(){
                    this.$refs.child2Container.aaa();
                }
            }
        }
    </script>
    
    // 子组件1
    <template>
        <div>我是子组件1</div>
        <div @click="save"></div>
    </template>
    <script>
        export default {
            name: 'App',
            methods: {
                save(){
                    this.$emit('save');
                }
            }
        }
    </script>
    
    // 子组件2
    <template>
        <div>我是子组件2</div>
        <div @click="aaa"></div>
    </template>
    <script>
        export default {
            name: 'App',
            methods: {
                aaa(){
                    console.log('我是子组件2里面的方法");
                }
            }
        }
    
    </script>
    
    
    
    

    四、两个没有关系的vue页面,想调用另一个页面的方法时,需要使用一个中间件,兄弟组件之间也可以使用这种方法。

    // 设置中间件vue文件
    import Vue from 'vue'
    export default new Vue
    
    // 调用事件的页面
    import Utiles form '@/utils/updataList'
    Utiles.$emit('updata')
    
    // 有方法要执行的页面
    import Utiles form '@/utils/updataList'
    mounted () {
      var than = this
     Utiles.$on('updata', function () {
      than.getList()
    })
    }
    

    以上方法,效率有所不同,根据实际需求选择合适的方法。

    相关文章

      网友评论

          本文标题:编程大白话之-vue页面之间方法的互调

          本文链接:https://www.haomeiwen.com/subject/wauorktx.html