Can vue-router open a link in a new tab?

后端 未结 11 1679
礼貌的吻别
礼貌的吻别 2020-12-04 16:58

I have a summary page and a detail subpage. All of the routes are implemented with vue-router (v 0.7.x) using programmatic navigation like this:



        
相关标签:
11条回答
  • 2020-12-04 17:40

    I think the best way is to simply use:

    window.open("yourURL", '_blank');
    

    0 讨论(0)
  • 2020-12-04 17:43

    This worked for me-

    let routeData = this.$router.resolve(
    {
      path: '/resources/c-m-communities', 
      query: {'dataParameter': 'parameterValue'}
    });
    window.open(routeData.href, '_blank');
    

    I modified @Rafael_Andrs_Cspedes_Basterio answer

    0 讨论(0)
  • 2020-12-04 17:43

    The simplest way of doing this using an anchor tag would be this:

    <a :href="$router.resolve({name: 'posts.show', params: {post: post.id}}).href" target="_blank">
        Open Post in new tab
    </a>
    
    0 讨论(0)
  • 2020-12-04 17:46

    It seems like this is now possible in newer versions (Vue Router 3.0.1):

    <router-link :to="{ name: 'fooRoute'}" target="_blank">
      Link Text
    </router-link>
    
    0 讨论(0)
  • 2020-12-04 17:47

    In case that you define your route like the one asked in the question (path: '/link/to/page'):

    import Vue from 'vue'
    import Router from 'vue-router'
    import MyComponent from '@/components/MyComponent.vue';
    
    Vue.use(Router)
    
    export default new Router({
      routes: [
        {
          path: '/link/to/page',
          component: MyComponent
        }
      ]
    })
    

    You can resolve the URL in your summary page and open your sub page as below:

    <script>
    export default {
      methods: {
        popup() {
          let route = this.$router.resolve({path: '/link/to/page'});
          // let route = this.$router.resolve('/link/to/page'); // This also works.
          window.open(route.href, '_blank');
        }
      }
    };
    </script>
    

    Of course if you've given your route a name, you can resolve the URL by name:

    routes: [
      {
        path: '/link/to/page',
        component: MyComponent,
        name: 'subPage'
      }
    ]
    
    ...
    
    let route = this.$router.resolve({name: 'subPage'});
    

    References:

    • vue-router router.resolve(location, current?, append?)
    • vue-router router-link
    0 讨论(0)
  • 2020-12-04 17:52

    Just write this code in your routing file :

    {
      name: 'Google',
      path: '/google',
      beforeEnter() {                    
                    window.open("http://www.google.com", 
                    '_blank');
                }
    }
    
    0 讨论(0)
提交回复
热议问题