vue全局方法运用

<template>
<div>
    <el-dialog
      title="请选择人员"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-select v-model="personName" multiple placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
    <el-button @click="cancel">取 消</el-button>
    <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
    </el-dialog>
</div>
</template>

<script>
    export default {
        name: "index",
        data() {
            return {
              dialogVisible: false,
              options: [],
              personName: [],
              personOptions:{
                resolve: null,
                reject: null
              }
            }
        },
        methods: {
         showDialog(options){
           this.options=options;
           return new Promise((resolve, reject) => {
             this.dialogVisible = true
            this.personOptions.resolve=resolve;
             this.personOptions.reject=reject
           })
         },
          confirm(){
            this.personOptions.resolve(this.personName)
            this.doClose()
          },
          cancel(){
            this.dialogVisible=false
          },
          clear () {
            this.options = [];
            this.personName=[];
            this.personOptions = {
              resolve: null,
              reject: null
            }
          },
          doClose () {
            this.dialogVisible = false
            this.clear()
          }
        }
    }
</script>

<style scoped lang="scss">

</style>

组件selectPersonDialog

<template>
  <div id="app">
    <router-view/>
    <select-person-dialog ref="selectPerson"></select-person-dialog>
  </div>
</template>

<script>
import Vue from 'vue'
import  selectPersonDialog from '@/components/selectPersonDialog'

export default {
  name: 'App',
  components: {
    selectPersonDialog
  },
  mounted () {
    Vue.prototype.$selectPerson = this.$refs.selectPerson
  }
}
</script>

在app.vue上面 引入selectPersonDialog组件,设置ref,挂载和设置全局方法

<template>
 <el-button type="default" class="save-btn" plain size="small" @click="selectPerson">选人</el-button>
</template>

在单个页面触发这个方法

 selectPerson(){
        let options=[{
          value: '选项1',
          label: '黄金糕'
        }, {
          value: '选项2',
          label: '双皮奶'
        }, {
          value: '选项3',
          label: '蚵仔煎'
        }, {
          value: '选项4',
          label: '龙须面'
        }, {
          value: '选项5',
          label: '北京烤鸭'
        }]
        this.$selectPerson.showDialog(options).then(res=>{
          console.log(res);
        })
      },

调用selectPerson方法

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、了解Vue.js 1.1.1 Vue.js是什么? 简单小巧、渐进式、功能强大的技术栈 1.1.2 为什么学习...
    蔡华鹏阅读 3,377评论 0 3
  • 前言 记录平时学到的知识,标题写的大气一点,也算是给自己一点鼓励,希望在技术这条路可以远走越远,路越走越宽~ 文中...
    徐国军_plus阅读 1,676评论 3 28
  • 目录 - 3.1 vue中子组件调用父组件的方法 - 3.2 Vue父组件调用子组件的方法 - 3.3 涉及到组件...
    我跟你蒋阅读 726评论 0 10
  • 基本用法 一、vuejs简介 是一个构建用户界面的框架 是一个轻量级的MVVM(Model-View-ViewMo...
    深度剖析JavaScript阅读 18,264评论 0 8
  • 渲染函数和jsx 在vue中我们可以不用template来指定组件的模板,而是用render函数来创建虚拟dom结...
    6e5e50574d74阅读 728评论 0 0