//下面的方式就是自定义组件或方法的方式
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
import coustom from './coustom'
export default {
components: {
coustom
}
}
"data" v-if ="show" >
换个办法以自定义alert 为例
就这么一句就调用出来 this.$alert('哈哈哈');
alert.vue 如下
[javascript] view plain copy
"dialog-fade" >
if=
"show" class =
"modal fade dialog-modal" id=
"alert" role=
"dialog" data-backdrop=
"false" aria-hidden=
"true" >
class=
"modal-dialog" role=
"document" >
class=
"modal-content" >
class="modal-header row" >
class="modal-title col-md-4" >提示
"button" class ="close" aria-label="Close" @click="close" >
"true" >×
class=
"modal-body" >
class="col-xs-offset-1" >{ {message}}
class="modal-footer" >
"button" class ="btn btn-primary" @click="close" >确定
对然后将Alert 挂载到vue全局 index.js
[javascript] view plain copy
function install(Vue) {
Object.defineProperty(Vue.prototype, '$alert' , {
get() {
let div = document.createElement('div' )
document.body.appendChild(div);
return (message) => {
const Constructor = Vue.extend(Alert)
let Instance = new Constructor({
data() {
return {
message: message,
show: true
}
}
}).$mount(div);
};
}
});
}
export default install
最后vue.use 一下
[javascript] view plain copy
import alert from 'index'
Vue.use(alert)
就能直接调用了
当然前面有个坑 transition 的 vue 的过渡 alert的div不是一开始就加载到文档上的,通过后面的
[javascript] view plain copy
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
"dialog-fade" >
if=
"show" class =
"modal fade" id=
"confirm" tabindex=
"-1" role=
"dialog"
data-backdrop="false" aria-hidden="true" >
class=
"modal-dialog" role=
"document" >
class=
"modal-content" >
class="modal-header row" >
class="modal-title col-md-4" >提示
"button" class ="close" @click="close" >
"true" >×
class=
"modal-body" >
class="col-xs-offset-1" >{ {message}}
class="modal-footer" >
"button" class ="btn btn-info" @click="close" >取消
"button" class ="btn btn-primary" @click="ConfirmSure" >确定
[javascript] view plain copy
import Confirm from './Confirm.vue'
function install(Vue) {
Object.defineProperty(Vue.prototype, '$confirm' , {
get() {
let div = document.createElement('div' )
document.body.appendChild(div);
return (message, confirmSure) => {
const Constructor = Vue.extend(Confirm)
const Instance = new Constructor({
data() {
return {
message: message,
show: true
}
},
methods: {
confirmSure: confirmSure //确定方法
}
}).$mount(div);
};
}
});
}
export default install
同样use 一下
import alert from 'index' Vue.use(alert)
[javascript] view plain copy
this .$confirm('你是猴子请来的唐僧么' , () => console.log('yes,哈哈哈哈哈' ))
传了两个arguments,累了吧,轻松点,
片分三级,嗯········参数也得 至少能传 三个。。。。
嗯,往哪里看呐···!
这里传递的params 才传递到第二个,才实现第二个功能,要么要实现第三个功能呢,dialog对话框内容,根据环境应用环境传递进去显示
如此中间的form 表单是动态传递进入的
[javascript] view plain copy
class=
"midpass" >
class=
"form-group form-group-inline flex" :
class =
"errors.has('ans') ? 'has-error has-danger' : '' " >
class="form-control-label" >1+1=?
class=
"form-input-longer" >
"password" class ="form-control form-control-title" name="ans" v-model="input.value"
v-validate="'required|min:1'" placeholder="请输入答案" >
class="help-block" >请输入答案
[javascript] view plain copy
export default {
name: 'oneaddone' ,
data() {
return {
input: {
value: null
}
}
}
}
用到了前端验证 vue veevalidate 这样传递进去 要调教数据时,触发验证,就是父组件调用子组件的方法
this.$children 即可
dialog.vue
[javascript] view plain copy
"dialog-fade" >
if=
"show" class =
"modal fade" id=
"popform" tabindex=
"-1"
role="dialog" data-backdrop="false" aria-hidden="true" >
class="hide" id="formpop-btn" data-toggle="modal" data-target="#popform" >
class=
"modal-dialog" role=
"document" >
class=
"modal-content" >
class="modal-header row" >
class="modal-title col-md-6 col-xs-4" >{ {message}}
"button" class ="close col-md-1" aria-label="Close" @click="close" >
"true" >×
>
class="modal-body" >
"modalBody" ref="forms" >
class=
"modal-footer" >
class="center-block" style="width: 230px;" >
"button" class ="btn btn-secondary" @click="close" >取消
"submit" class ="btn btn-primary" >保存
再来一遍
[javascript] view plain copy
import dialog from './dialog.vue'
function install(Vue) {
Object.defineProperty(Vue.prototype, '$dialog' , {
get() {
let div = document.createElement('div' );
document.body.appendChild(div);
return (message, modalBody) => {
const Constructor = Vue.extend(dialog)
const Instance = new Constructor({
data() {
return {
message: message,
show: true ,
modalBody: modalBody
}
}
}).$mount(div)
return Instance.setSubmit //放回 一个方法用于 传递 自定义的数据和 submit 时方法
};
}
});
}
export default install
Vue.use 同上
[javascript] view plain copy
this .$dialog('请计算下面的数学题' , resolve =>
require(['./oneaddone.vue' ], resolve))('input' , (data) => {
this .$alert('哈哈哈,正确' )
console.log(data)
return data
}
)
PS:这里需要注意的是 this.$dialog()(); 是这样的 因为里面返回的是一个方法,同时$mount 不能直接挂载在body 或者html下 必须在body的 子元素中 所以,createElement('div')
1+1 = 2 答案正确········
欢迎加群交流:897724635 更多视频教程资源分享给大家