Vue

by fanwenjie

HTML

<div id="app">
    <a :href="href"
        @click.exact="handleClickLink($event, false)"
        @click.ctrl="handleClickLink($event, true)"
        @click.meta="handleClickLink($event, true)">
        多次绑定
    </a>

    <a :href="href" @click="handleClick">
        一次绑定
    </a>
    
    <a :href="href">这是一个普通链接</a>

</div>

CSS

a{
  display: block;
}

Vue

new Vue({
  el: "#app",
  computed:{
  	href(){
    	return 'https://www.baidu.com';
    }
  },
  methods: {
  	// 绑定多次,通过修饰符判断
  	handleClickLink(e, openOnNewWindow){
      if(openOnNewWindow){
      	window.open(this.href);
      } else{
        window.location=this.href;
      }
      e.preventDefault();
    },
    // 只绑定一次,通过ctrlKey判断
    handleClick(e) {
    	const openOnNewWindow = e.ctrlKey || e.metaKey;
      if(openOnNewWindow){
      	window.open(this.href);
      } else{
        window.location=this.href;
      }
      e.preventDefault();
    }
  }
})