Categories
Angular Answers

How to redirect to an external URL from Angular route without using component?

Sometimes, we want to redirect to an external URL from Angular route without using component.

In this article, we’ll look at how to redirect to an external URL from Angular route without using component.

How to redirect to an external URL from Angular route without using component?

To redirect to an external URL from Angular route without using component, we can set window.location.href to a value in our code.

For instance, we write

import { Component, OnInit } from "@angular/core";

@Component({
  selector: "redirect",
  template: "redirecting...",
})
export class RedirectComponent implements OnInit {
  constructor() {}

  ngOnInit() {
    window.location.href = "http://www.example.com";
  }
}

to create the RedirectComponent that redirects to http://www.example.com with

window.location.href = "http://www.example.com"

when the component mounts.

Conclusion

To redirect to an external URL from Angular route without using component, we can set window.location.href to a value in our code.

Categories
JavaScript Answers

How to export JavaScript data to a CSV file without server interaction?

Sometimes, we want to export JavaScript data to a CSV file without server interaction.

In this article, we’ll look at how to export JavaScript data to a CSV file without server interaction.

How to export JavaScript data to a CSV file without server interaction?

To export JavaScript data to a CSV file without server interaction, we can set a link to a base64 string with the CSV content.

For instance, we write

const csvString = csvRows.join("\r\n");
a.href = "data:application/csv;charset=utf-8," + encodeURIComponent(csvString);

to create the csvString with the CSV data.

Then we put the data in a base64 string with

"data:application/csv;charset=utf-8," + encodeURIComponent(csvString)

And we assign the value to a.href where a is an a element.

Then when we click on the link, the data will be downloaded.

Conclusion

To export JavaScript data to a CSV file without server interaction, we can set a link to a base64 string with the CSV content.

Categories
React Answers

How to create constants file with React?

Sometimes, we want to create constants file with React.

In this article, we’ll look at how to create constants file with React.

How to create constants file with React?

To create constants file with React, we can create a JavaScript file with variables exported.

For instance, we write

fileWithConstants.js

export const ACTION_INVALID = "This action is invalid!";
export const CONSTANT_NUMBER_1 = "hello I am a constant";
export const CONSTANT_NUMBER_2 = "hello I am also a constant";

to export the ACTION_INVALID, CONSTANT_NUMBER_1 and CONSTANT_NUMBER_2 constants in a file.

Then we can use them by writing

import * as consts from "path/to/fileWithConstants";

const errorMsg = consts.ACTION_INVALID;

to import all the contents of fileWithConstants.js that are export and reference ACTION_INVALID with consts.ACTION_INVALID.

How to create constants file with React?

To create constants file with React, we can create a JavaScript file with variables exported.

Categories
CSS

How to make div fixed scrolling to that div with CSS?

Sometimes, we want to make div fixed scrolling to that div with CSS.

In this article, we’ll look at how to make div fixed scrolling to that div with CSS.

How to make div fixed scrolling to that div with CSS?

To make div fixed scrolling to that div with CSS, we can use the position: sticky CSS property.

For instance, we write

position: -webkit-sticky;
position: sticky;
top: 0;
z-index: 1;

to set position to sticky and z-index to 1 to make the element with the sticky position show on top after we scroll to it.

Conclusion

To make div fixed scrolling to that div with CSS, we can use the position: sticky CSS property.

Categories
Vue Answers

How to center content vertically on Vuetify?

Sometimes, we want to center content vertically on Vuetify.

In this article, we’ll look at how to center content vertically on Vuetify.

How to center content vertically on Vuetify?

To center content vertically on Vuetify, we can use the align and justify props.

For instance, we write

<template>
  <v-container fill-height fluid>
    <v-row align="center" justify="center">
      <v-col></v-col>
    </v-row>
  </v-container>
</template>

to set the align and justify props to set the align-items and justify-content CSS properties respectivelt on the v-row.

Since align is set to center, the content should be vertically centered.

Conclusion

To center content vertically on Vuetify, we can use the align and justify props.