Vue.js is a popular framework for creating front end web apps.
In this article, we’ll look at some tips for writing better Vue.js apps.
Force Download with GET Request using Axios
We can make the response always download by passing our response data into the Blob
constructor.
For instance, we can write:
axios
.get(`download-pdf`, {
responseType: 'arraybuffer'
})
.then(response => {
const blob = new Blob(
[response.data],
{ type: 'application/pdf' }
),
const url = window.URL.createObjectURL(blob);
window.open(url) ;
})
We make a GET request to the download-pdf
endpoint to download our PDF.
We make sure that we specify that the responseType
is 'arraybuffer'
to indicate that it’s a binary file.
Then in the then
callback, we get the response
parameter, which has the data we want in the data
property.
We pass that into the Blob
constructor in an array.
And we specify the MIME type of the response data.
Then we create the URL that lets us download the file with createObjectURL
.
Finally, we call window.open
with the url
to download the file.
We can also specify the file name of the downloaded file by making a small change in the then
callback.
To do that, we write:
axios
.get(`download-pdf`, {
responseType: 'arraybuffer'
})
.then(response => {
const blob = new Blob(
[response.data],
{ type: 'application/pdf' }
),
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "file.pdf";
link.click();
})
Instead of creating an object URL directly, we create an invisible link first and set the file name as the download
property of it.
Then we call click
to download it.
Fire an Event When v-model Changes
We can add a change event listener to our input to listen for input value changes.
For instance, we can write:
<input
type="radio"
name="option"
value=""
v-model="status"
v-on:change="onChange"
>
onChange
is the name of the change event handler.
We can also replace v-on:
with @
for short:
<input
type="radio"
name="option"
value=""
v-model="status"
@change="onChange"
>
Also, we can add a watcher for our model property instead of attach a change event listener.
For instance, we can write:
new Vue({
el: "#app",
data: {
status: ''
},
watch: {
status(val, oldVal) {
console.log(val, oldVal)
}
}
});
With the watch
property, we added a watcher for the status
variable as indicated by the name.
val
has the current value and oldVal
has the old value.
Redirect to Requested Page After Login Using Vue Router
We can add a redirect path to the query parameter when redirecting to the login page.
For instance, we can write:
onClick() {
if (!isAuthenticated) {
this.$router.push({ name: 'login', query: { redirect: '/profile' } });
}
}
We check for authentication credentials.
If they aren’t present, then we call this.$router.push
to redirect to the route for the login page.
The query
property has the path to redirect to when login is successful.
We have a query string with the redirect
key and the /profile
value.
Then in our login form component, we can write:
submitForm() {
login(this.credentials)
.then(() => this.$router.push(this.$route.query.redirect || '/'))
.catch(error => {
//...
})
}
in the methods
property.
We call login
which returns a promise.
So we can call then
with a callback to redirect to the path in the query string.
We get that path with the this.$route.query.redirect
property.
Then we call this.$router.push
to do the redirect.
In case it’s not defined, we redirect to the /
route.
Run Vue.js Dev Server with HTTPS
We can run Vue’s dev server with HTTPS by changing the code in build/dev-server.js
to read the certificate and the private key files.
Then we can use them to call https.createServer
to create the HTTPS server.
For instance, we can write:
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'))
};
const server = https.createServer(options, app).listen(port);
We read the files and put them into the options
object.
Then we pass the object into the https.createServer
method.
app
is the Express server’s app object.
Conclusion
We can serve a Vue project with a dev HTTPS server.
Also, we can download files with Axios.
And we can add watchers to watch for variable changes.