Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

373
Visualizações
Vuejs : Accessing Specific URL Segment

I have a route/URL :

Ex. http://localhost:8080/qr-codes

I want to access to the first segment

qr-codes

How do I do that ?

I used to be able to do sth like this in Laravel

Request::segment(1);

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

const getUrlPathSegments = () => (
  (new URL(window.location.href)).pathname.split('/').filter(Boolean)
)

for example, when you call the function on current stackoverflow page, you get:

getUrlPathSegments() === ['questions', '70427242', 'vuejs-accessing-specific-url-segment']

getUrlPathSegments()[0] === 'questions'
over 4 years ago · Santiago Trujillo Relatório

0

Using URL.pathname and String#split:

const str = 'http://localhost:8080/qr-codes';

const firstSegment = (new URL(str)).pathname.split('/')[1];

console.log(firstSegment);

Vue demo:

new Vue({
  el: "#app",
  computed: {
    path: function() {
      const firstSegment = (new URL(window.location.href)).pathname.split('/')[1];
      return `${firstSegment}/create`;
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">{{path}}</div>

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda