iview/src/mixins/link.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-06-21 10:36:53 +08:00
import { oneOf } from '../utils/assist';
2018-06-20 17:09:11 +08:00
export default {
2018-06-21 10:36:53 +08:00
props: {
to: {
type: [Object, String]
},
replace: {
type: Boolean,
default: false
},
target: {
type: String,
validator (value) {
return oneOf(value, ['_blank', '_self', '_parent', '_top']);
},
default: '_self'
}
},
2018-06-20 17:09:11 +08:00
computed: {
linkUrl () {
2018-06-20 17:16:38 +08:00
const type = typeof this.to;
return type === 'string' ? this.to : null;
2018-06-20 17:09:11 +08:00
}
},
methods: {
handleClick () {
const isRoute = this.$router;
2018-06-29 18:12:37 +08:00
const href = this.to || this.href;
2018-06-20 17:09:11 +08:00
if (isRoute) {
2018-06-29 18:10:14 +08:00
this.replace ? this.$router.replace(href) : this.$router.push(href);
2018-06-20 17:09:11 +08:00
} else {
2018-06-29 18:10:14 +08:00
window.location.href = href;
2018-06-20 17:09:11 +08:00
}
2018-06-21 10:22:11 +08:00
},
handleCheckClick (event) {
2018-06-29 18:10:14 +08:00
if (this.to || this.href) {
2018-06-21 10:22:11 +08:00
if (this.target === '_blank') {
return false;
} else {
event.preventDefault();
this.handleClick();
}
}
2018-06-20 17:09:11 +08:00
}
}
2018-06-29 18:10:14 +08:00
};