使用vue.use(##)构建全局方法,用this.$##使用,同时给自定义的方法 传递component调用其方法 //eventHub

 

//下面的方式就是自定义组件或方法的方式

Vue.use(Object.defineProperty(Vue.prototype, '$eventHub', {  

 

    get() {  
        return new Vue({
data () {
   return {
     // 定义数据
       val: ''
   }
},
created () {
   // 绑定监听
   this.$on('eventHub', (val)=>{
       this.val = val
   })
}
});
    }  

 

}))

发送数据到eventHub

this.$eventHub.$emit('eventHub', { 
    data:"data"

   });

 

需要使用数据的地方接收eventHub

computed:{
          val () {
                return eventHub.val
          }
}

从这里开始是转载::::::::::::::::::::::::::::::::::

项目中会遇到一个组件/方法, 在多个地方被调用,比较方便的的方式之一,this.$custom(agruments) 这样就比较方便

,不然用组件引入的办法调用就就比较麻烦,每可能都需要这样调用

 

[javascript] view plain copy

  1. import coustom from './coustom'  
  2. export default {  
  3.     components: {  
  4.         coustom  
  5.     }  
  6. }  
  7. <coustom :data="data" v-if="show"></coustom>  


换个办法以自定义alert 为例

 

 

 

就这么一句就调用出来
this.$alert('哈哈哈');

alert.vue 如下

[javascript] view plain copy

  1. <template>  
  2.     <transition name="dialog-fade">  
  3.         <div v-if="show" class="modal fade dialog-modal" id="alert"  role="dialog" data-backdrop="false" aria-hidden="true">  
  4.             <div class="modal-dialog" role="document">  
  5.                 <div class="modal-content">  
  6.                     <div class="modal-header row">  
  7.                         <h5 class="modal-title col-md-4">提示</h5>  
  8.                         <button type="button" class="close" aria-label="Close" @click="close">  
  9.                             <span aria-hidden="true">×</span>  
  10.                         </button>  
  11.                     </div>  
  12.                     <div class="modal-body">  
  13.                         <div class="col-xs-offset-1">{{message}}</div>  
  14.                     </div>  
  15.                     <div class="modal-footer">  
  16.                         <button type="button" class="btn btn-primary" @click="close">确定</button>  
  17.                     </div>  
  18.                 </div>  
  19.             </div>  
  20.         </div>  
  21.     </transition>  
  22. </template>  
  23. <script>  
  24. export default {  
  25.     name: 'Alert',  
  26.     methods: {  
  27.         close: function() {  
  28.             this.show = false  
  29.         }  
  30.     }  
  31. }  
  32. </script>  

 

对然后将Alert 挂载到vue全局  index.js

 

 

 

[javascript] view plain copy

  1. function install(Vue) {  
  2.       
  3.     Object.defineProperty(Vue.prototype, '$alert', {  
  4.         get() {  
  5.             let div = document.createElement('div')  
  6.             document.body.appendChild(div);  
  7.             return (message) => {  
  8.                 const Constructor = Vue.extend(Alert)  
  9.                 let Instance = new Constructor({  
  10.                     data() {  
  11.                         return {  
  12.                             message: message,  
  13.                             show: true  
  14.                         }  
  15.                     }  
  16.                 }).$mount(div);  
  17.             };  
  18.         }  
  19.     });  
  20. }  
  21.   
  22. export default install  

 

 

最后vue.use 一下

 

[javascript] view plain copy

  1. import alert from 'index'  
  2.   
  3. Vue.use(alert)  


就能直接调用了

 

当然前面有个坑 transition 的 vue 的过渡 alert的div不是一开始就加载到文档上的,通过后面的 

[javascript] view plain copy

  1. document.body.appendChild(div);  

 

动态写入,就造成 alert 显示时看不到transition效果,抛开vue来说也会遇到这样的情况 可以settimeout 下 给append的元素 addClass

同理在vue 中也可以,当然还有更好的办法暂时没想到。。。。

 

alert 只是纯的 传递一个param 但是需要 传递一个function 时,比如confirem

 

this.$confirm('请确定你是傻逼', () => console.log('yes')})

还是相同的味道,相同的道理

Confirm.vue

 

[javascript] view plain copy

  1. <template>  
  2.     <transition name="dialog-fade">  
  3.         <div v-if="show" class="modal fade" id="confirm" tabindex="-1" role="dialog"   
  4.             data-backdrop="false"  aria-hidden="true">  
  5.             <div class="modal-dialog" role="document">  
  6.                 <div class="modal-content">  
  7.                     <div class="modal-header row">  
  8.                         <h5 class="modal-title col-md-4">提示</h5>  
  9.                         <button type="button" class="close" @click="close">  
  10.                             <span aria-hidden="true">×</span>  
  11.                         </button>  
  12.                     </div>  
  13.                     <div class="modal-body">  
  14.                         <div class="col-xs-offset-1">{{message}}</div>  
  15.                     </div>  
  16.                     <div class="modal-footer">  
  17.                         <button type="button" class="btn btn-info" @click="close">取消</button>  
  18.                         <button type="button" class="btn btn-primary" @click="ConfirmSure">确定</button>  
  19.                     </div>  
  20.                 </div>  
  21.             </div>  
  22.         </div>  
  23.     </transition>  
  24. </template>  
  25. <script>  
  26. export default {  
  27.     name: 'Confirm',  
  28.     methods: {  
  29.         close: function() {  
  30.             this.show = false  
  31.         },  
  32.         ConfirmSure() {  
  33.             this.confirmSure()//确定关闭 由install 传入  
  34.             this.close()  
  35.         }  
  36.     }  
  37. }  
  38. </script>  

 

[javascript] view plain copy

  1. import Confirm from './Confirm.vue'  
  2.   
  3. function install(Vue) {  
  4.   
  5.     Object.defineProperty(Vue.prototype, '$confirm', {  
  6.         get() {  
  7.             let div = document.createElement('div')  
  8.             document.body.appendChild(div);  
  9.             return (message, confirmSure) => {  
  10.                 const Constructor = Vue.extend(Confirm)  
  11.                 const Instance = new Constructor({  
  12.                     data() {  
  13.                         return {  
  14.                             message: message,  
  15.                             show: true  
  16.                         }  
  17.                     },  
  18.                     methods: {  
  19.                         confirmSure: confirmSure    //确定方法  
  20.                     }  
  21.                 }).$mount(div);  
  22.             };  
  23.         }  
  24.     });  
  25. }  
  26.   
  27. export default install  

 

 

同样use 一下

import alert from 'index' Vue.use(alert)

 

 

[javascript] view plain copy

  1. this.$confirm('你是猴子请来的唐僧么', () => console.log('yes,哈哈哈哈哈'))  

 

 

 

 

 

传了两个arguments,累了吧,轻松点,

片分三级,嗯········参数也得 至少能传 三个。。。。

嗯,往哪里看呐···!

这里传递的params 才传递到第二个,才实现第二个功能,要么要实现第三个功能呢,dialog对话框内容,根据环境应用环境传递进去显示 

如此中间的form 表单是动态传递进入的

 

[javascript] view plain copy

  1. <div class="midpass">  
  2.        <div class="form-group form-group-inline flex" :class="errors.has('ans') ? 'has-error has-danger' : '' ">  
  3.            <label class="form-control-label">1+1=?</label>  
  4.            <div class="form-input-longer">  
  5.                <input type="password" class="form-control form-control-title" name="ans" v-model="input.value"  
  6.                    v-validate="'required|min:1'"  placeholder="请输入答案">  
  7.                <div class="help-block">请输入答案</div>  
  8.            </div>  
  9.        </div>  
  10.    </div>  

[javascript] view plain copy

  1. export default {  
  2.     name: 'oneaddone',  
  3.     data() {  
  4.         return {  
  5.             input: {  
  6.                  
  7.                 value: null  
  8.             }  
  9.         }  
  10.     }  
  11. }  

 

 

用到了前端验证 vue veevalidate 这样传递进去 要调教数据时,触发验证,就是父组件调用子组件的方法

this.$children 即可

 

dialog.vue

 

[javascript] view plain copy

  1. <template>  
  2.     <transition name="dialog-fade">  
  3.         <div v-if="show" class="modal fade" id="popform" tabindex="-1"   
  4.             role="dialog" data-backdrop="false" aria-hidden="true">  
  5.             <div class="hide" id="formpop-btn" data-toggle="modal" data-target="#popform"></div>  
  6.             <div class="modal-dialog" role="document">  
  7.                 <div class="modal-content">  
  8.                     <div class="modal-header row">  
  9.                         <h4 class="modal-title col-md-6 col-xs-4">{{message}}</h4>  
  10.                         <button type="button" class="close col-md-1" aria-label="Close"  @click="close">  
  11.                             <span aria-hidden="true">×</span>  
  12.                         </button>  
  13.                     </div>  
  14.                     <form @submit.prevent="submit">  
  15.                         <div class="modal-body">  
  16.                             <keep-alive>  
  17.                                 <component :is="modalBody" ref="forms"></component>  
  18.                             </keep-alive>  
  19.                         </div>  
  20.                         <div class="modal-footer">  
  21.                             <div class="center-block" style="width: 230px;">  
  22.                                 <button type="button" class="btn btn-secondary" @click="close">取消</button>  
  23.                                 <button type="submit" class="btn btn-primary">保存</button>  
  24.                             </div>  
  25.                         </div>  
  26.                     </form>  
  27.                 </div>  
  28.             </div>  
  29.         </div>  
  30.     </transition>  
  31. </template>  
  32.   
  33. <script>  
  34. export default {  
  35.     name: 'dialog',  
  36.     data() {  
  37.         return {  
  38.             Submit: () => {}  
  39.         }  
  40.     },  
  41.     methods: {  
  42.         close() {  
  43.             this.show = false  
  44.         },  
  45.         setSubmit(dataKey, Submit) {  
  46.             this.submit = () => {   //给submit btn 设置提交方法  
  47.                 this.$children.map(function (child) {  
  48.                     let data = child.$data[dataKey] //data 的key 调用时传递的 data key 可以根据情景定义  
  49.                     child.$validator.validateAll().then((result) => {  
  50.                         if (result) {  
  51.                             Submit(data).then(res => {  
  52.                                 if (res) this.close()  
  53.                             })  
  54.                         }  
  55.                     });  
  56.                 })  
  57.             }  
  58.         },  
  59.     }  
  60. }  
  61. </script>  

 

再来一遍

 

 

[javascript] view plain copy

  1. import dialog from './dialog.vue'  
  2.   
  3. function install(Vue) {  
  4.     Object.defineProperty(Vue.prototype, '$dialog', {  
  5.         get() {  
  6.             let div = document.createElement('div');  
  7.             document.body.appendChild(div);  
  8.             return (message, modalBody) => {  
  9.                 const Constructor = Vue.extend(dialog)  
  10.                 const Instance = new Constructor({  
  11.                     data() {  
  12.                         return {  
  13.                             message: message,  
  14.                             show: true,  
  15.                             modalBody: modalBody  
  16.                         }  
  17.                     }  
  18.                 }).$mount(div)  
  19.                 return Instance.setSubmit  //放回 一个方法用于 传递 自定义的数据和 submit 时方法  
  20.             };  
  21.               
  22.         }  
  23.     });  
  24. }  
  25.   
  26. export default install  

Vue.use 同上

[javascript] view plain copy

  1. this.$dialog('请计算下面的数学题', resolve =>   
  2.                 require(['./oneaddone.vue'], resolve))('input', (data) => {  
  3.         this.$alert('哈哈哈,正确')  
  4.                     console.log(data)  
  5.                     return data  
  6.                 }  
  7.             )  


PS:这里需要注意的是 this.$dialog()(); 是这样的 因为里面返回的是一个方法,同时$mount 不能直接挂载在body 或者html下 必须在body的 子元素中  所以,createElement('div')

 

1+1 = 2 答案正确········

欢迎加群交流:897724635  更多视频教程资源分享给大家

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: `Vue.use` 和 `Vue.component` 都是 Vue.js 框架中用来注册全局组件的方法,但它们的使用方式和作用略有不同。 `Vue.use` 方法用来安装 Vue.js 插件,这些插件可以是 Vue.js 的内置插件或第三方插件。这个方法会在全局 Vue 实例上安装一个插件,并且可以传递一些参数给这个插件。例如,我们可以使用 `Vue.use` 来安装 Vuex 或 Vue Router 等插件。 示例: ``` import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const store = new Vuex.Store({ // state, mutations, actions, getters... }); new Vue({ el: '#app', store, render: h => h(App) }); ``` `Vue.component` 方法用来注册全局组件,这些组件可以在应用的任何地方使用。注册组件后,就可以在组件模板中使用这个组件了。 示例: ``` import Vue from 'vue'; import MyComponent from './MyComponent.vue'; Vue.component('my-component', MyComponent); new Vue({ el: '#app', render: h => h(App) }); ``` 在上面的示例中,我们使用 `Vue.component` 方法来注册一个名为 `my-component` 的全局组件,并指定组件的实现是 `MyComponent.vue` 文件中导出的组件。现在,在应用的任何地方都可以使用 `<my-component>` 标签来使用这个组件。 ### 回答2: vue.usevue.componentVue.js中两个不同的API。 vue.use是用来安装Vue.js插件的方法。它会调用传入插件对象的install方法,并且可以在全局范围内使用插件的方法、指令或组件。使用vue.use可以全局注册一个插件,例如使用第三方插件如vue-router、vuex等。 vue.component是用来注册组件的方法。它可以注册一个全局组件或局部组件。全局组件意味着可以在整个应用中使用这个组件,而局部组件只能在该组件的父组件内部使用。通过vue.component可以传入组件的名称和选项,例如template、data、methods等,来定义一个组件。注册完组件后,可以在template中使用这个组件。 总结来说,vue.use是用来安装插件的方法,而vue.component是用来注册组件的方法使用vue.use可以全局注册插件,使用vue.component可以全局注册组件或局部注册组件。 ### 回答3: vue.use是用来安装Vue插件的方法,它接收一个插件作为参数,然后将该插件安装到Vue中。通过调用插件的install方法,可以在插件内部进行一些初始化操作,例如注册全局组件、添加自定义指令、扩展Vue的原型等。使用vue.use方法安装插件后,该插件的功能就可以在整个应用中使用了。 而vue.component是用来注册全局组件的方法,它接收两个参数,组件的名称和组件的选项对象。该方法会将组件注册到Vue全局组件列表中,使得在任何组件中都可以使用该组件。注册组件后,我们可以在模板中通过标签的方式来使用该组件,并在组件选项对象中定义组件的属性、方法、生命周期钩子等。 综上所述,vue.use方法是用来安装插件的,而vue.component方法用来注册全局组件。使用vue.use方法安装插件时,可以实现一些全局的功能扩展;而使用vue.component方法注册全局组件时,可以在各个组件中使用该组件。两者的区别在于使用的场景和功能,vue.use用于插件的安装,vue.component用于组件的注册。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值