8

In my previous Angular app I was able to open my resume in another window like such:

<a class="link popup" href="javascript:void(0);" onclick="javascipt:window.open('./../../assets/Resume_Christopher_Kade.pdf');">Resume</a>

While rewriting my website with Vue, I noted that it did not work as intended, after changing it to:

<a class="link popup" href="javascript:void(0);" v-on:click="openPdf()">Resume</a>

With openPdf() in my component's methods:

openPdf () {
    javascipt:window.open('./../assets/Resume_Christopher_Kade.pdf');
}

When clicking the link, a new page opens to the following URL:

http://localhost:8080/assets/Resume_Christopher_Kade.pdf

while showing an empty route on my screen instead of displaying the pdf in the browser's pdf viewer.

This issue might be related to the way I handle routes in Vue:

export default new Router({
  mode: 'history',
  routes: [    
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/work',
      name: 'Work',
      component: Work
    },
    {
      path: '/posts',
      name: 'Posts',
      component: Posts
    },
    { path: '*', component: Home }
  ]
})

Why isn't it as straightforward as in Angular? Am I missing something?

Christopher
  • 1,712
  • 2
  • 21
  • 50

2 Answers2

5

Did you figure it out?

My solution is for projects created with Vue CLI 3 running locally (I haven't built my project yet).

My issue was similar to yours. I just wanted <a> link that opened up my pdf file on a new tab but my url concatenated a single hash to my path and redirected me to my home page. And it was a surprisingly easy fix:

<a href="./resume.pdf" target="_blank">resume</a>

Just a dot, forward slash, and my file name. And my file is located under root > public folder.

Relative Path Imports

When you reference a static asset using relative path (must start with .) inside JavaScript, CSS or *.vue files, the asset will be included into webpack's dependency graph...

https://cli.vuejs.org/guide/html-and-static-assets.html#relative-path-imports

Leesa
  • 740
  • 2
  • 11
  • 15
  • It didn't work because my file (a link actually) didn't have `.pdf` extension. Fixed. – DevonDahon Mar 27 '19 at 08:35
  • 3
    Not working. Still getting the same problem as the question says. It always redirects to localhost://assets/filename.pdf – Soorya Oct 14 '19 at 10:09
-1

Assuming that you have static folder generated by default by Vue CLI you can simply put the PDF there and do it as follows <a href="./../static/file.pdf" target="_blank">.

Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94