2

I have the following code in my vue file. The issue I am facing here is that page reloads when I click on here. I have added event.preventDefault() but couldn't resolve the issue. Please help me find where I am going wrong.

<template>
  <p>Please click <a @click="stopProcess">here</a></p>
</template>

<script>  
  export default {
    methods: {
      stopProcess(){
        event.preventDefault()
        var page = "../../../pages/page.pdf"
        window.open(page);
      }
    }
  };
</script>
6
  • why are you using window.open? Commented Jul 28, 2020 at 22:03
  • @LeonardoBezerra because I want to open the page in the new tab on click Commented Jul 28, 2020 at 22:07
  • 1
    window.open will only work with absolute links, using https. "yourdomain.com/pages/page.pdf" Commented Jul 28, 2020 at 22:09
  • Yes like @LeonardoBezerra it works only with http protocol and you should also add 'blank' as second parameter to open that in new tab Commented Jul 28, 2020 at 22:11
  • What is your actual intention here? Downloading the pdf? Commented Jul 28, 2020 at 22:19

5 Answers 5

3

Try to add prevent modifier to prevent the default behavior as follows :

  <p>Please click <a @click.prevent="stopProcess">here</a></p>

or like you did but you missed the event as method parameter :

    stopProcess(event){
        event.preventDefault()
Sign up to request clarification or add additional context in comments.

6 Comments

this maybe true but it's not the reason for the reload
@LeonardoBezerra why ?
@user12763413 did you try out <a href="../../../pages/page.pdf" target="_blank">here</a>
its reloads with pdf file or with old content?
the pdf file opens in next page but the page from where I clicked is reloaded
|
2

Based on your code I would try this, but if it was me, I would use the router.

<template>
  <p>Please click <a @click="stopProcess">here</a></p>
</template>

<script>  
  export default {
    methods: {
      stopProcess(){
        var page = window.location.origin + "/pages/page.pdf"
        window.open(page);
      }
    }
  };
</script>

4 Comments

this works with my app, It won't work if you have router installed
Yes i have routers installed.
How do u recommend using it with router?
1

If you add href="javascript:void(0)" inside your anchor tag definition, then the page will not reload.

This is because the void operator returns undefined, a result the browser doesn't act on. See here for more info on the void operator.

E.g: <a href="javascript:void(0)" @click="stopProcess">here</a>

Comments

1

So first of you need to know @click does not act like the native click event so event.preventDefault() may not work as expected to make this work, you need to make your native click event with using @click.native. Then you need to pass the event itself to your handler method (The best way to handling the events in vue.js is to use built-in event handlers, so you may get rid of event.preventDefault() and directly use @click.prevent instead).

But all of these alone won't solve your issue because by default if the requested route is in the same domain the current page will follow it after window.open() happens so you need to pass the second value to it, to make sure the new path is not the same as the current one (the preferred value for it is _blank). You also always may need to indicate a href for your <a>.

<template>
  <p>Please click <a href="#" @click.native="stopProcess">here</a></p>
</template>

<script>
  export default {
    methods: {
      stopProcess(event) {
        event.preventDefault()
        var page = "../../../pages/page.pdf"
        window.open(page, '_blank');
      }
    }
  };
</script>

You can also make this much easier without any of this complicated stuff by setting the <a> target to _blank.

<template>
  <p>Please click <a href="../../../pages/page.pdf" target="_blank">here</a></p>
</template>

If you are using vue-router this could be something like this instead of above ones:

<template>
  <p>Please click  <router-link to="../../../pages/page.pdf" target="_blank">Home</router-link></p>
</template>

Comments

0

@click.prevent is the correct method, but the page will still be reloaded.

Since the stopProcess() will throw an exception because the event is not defined, the prevent modifier will not work.

Please remove the event.preventDefault() and add the prevent modifier and you will see it's working.

1 Comment

"A" tag doesn't cause the page to reload, so prevent will ONLY prevent the page from scrolling to the top of the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.