How to break lines using template string (ES6)?

1

I have a request for the backend and its URL is quite large.

this
  .$http
  .get('backoffice/usuarios/${this.id}/grupo/${this.grupo}/filial/${this.filial}/multinivel/${this.multinivel}')
  .then(({ data }) => {
    this.items = data
  })
})

Since I'm using Template Strings from the ES6 Javascript, I'd like to know as I perform a line break without it placing the spaces (% 20) in the request.

I know I can break into multiple constants using const , but I wonder if there is a way to perform line wrap easily with ES6.

I have tried with '/ n', enter normal, and it did not work.

The request is made in a Vuejs2 project.

    
asked by anonymous 23.05.2018 / 17:25

1 answer

0

If the idea is just to make reading easier you can do:

const url = '
  backoffice/
  usuarios/
  ${this.id}/
  grupo/
  ${this.grupo}/
  filial/
  ${this.filial}/
  multinivel/
  ${this.multinivel}
';

this
  .$http
  .get(url)
  .then(({ data }) => {
    this.items = data
  })
})

If the .get() does not clear the whitespace and line breaks it joins .replace(/[\n\s]+/g, '') .

const url = '
  backoffice/
  usuarios/
  ${this.id}/
  grupo/
  ${this.grupo}/
  filial/
  ${this.filial}/
  multinivel/
  ${this.multinivel}
'.replace(/[\n\s]+/g, '');

this
  .$http
  .get(url)
  .then(({ data }) => {
    this.items = data
  })
})

You could also do this with an array (it would be my choice in this case):

const url = [
  "backoffice",
  "usuarios",
  this.id,
  "grupo",
  this.grupo,
  "filial",
  this.filial,
  "multinivel",
  this.multinivel
].join('/');

this
  .$http
  .get(url)
  .then(({
    data
  }) => {
    this.items = data
  })
})
    
23.05.2018 / 19:46