Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass url parameters to Vuejs

Tags:

ajax

url

php

vue.js

I'm building an app with laravel and VueJs and I was wondering how to pass a url parameter like the user slug or the user id to vuejs in a proper way to be able to use that parameter for making ajax requests?

For example when someone clicks on a link leading to

domain.com/user/john-appleseed

I want to make an ajax request within my vuejs application with the slug 'john-appleseed' as a parameter.

What is the "right/proper" way to this?

Thanks in advance!

like image 672
Luuk Van Dongen Avatar asked Sep 14 '15 08:09

Luuk Van Dongen


People also ask

How to pass and access query parameters in Vue JS?

In this tutorial, we are going to learn how to pass and access query parameters in Vue.js. Assume a link with parameter: At the end of the URL, query parameters are added. In this URL ?curreny=bdt is the query parameter. Currency is the key and bdt is the value.

What are the parameters passed in the URL?

This will be something akin to - id, query, role are parameters passed in the URL. The receiving function can have access to the parameters and values, and have processing logic based on values. URL params are heavily used in some of the modern headless CMS applications (e.g. Strapi).

Can I use urlsearchparams without vue-router?

Absolute elegant answer. Thanks! Of-course without vue-router. URLSearchParams is a pure javascript approach it's not a vuejs helper. @mrded why is this approach better, given that Vue and apparently Vue Router is already used anyway? @Igor because it's a JS native way and it works everywhere. Vue Router is just an unnecessary abstraction.

How to pass query parameters from script tag in PHP?

At the end of the URL, query parameters are added. In this URL ?curreny=bdt is the query parameter. Currency is the key and bdt is the value. From script tag, we can pass query using this.$router.push () method like this:


3 Answers

vue-router would probably be of help here. It lets you define your routes like so:

router.map({
    '/user/:slug': {
        component: Profile,
        name: 'profile'
    },
})

Within your Profile component, the requested user's slug would then become available under this.$route.params.slug

Check the official documentation for details: http://router.vuejs.org/en/

like image 185
jsphpl Avatar answered Oct 25 '22 05:10

jsphpl


I suppose you are building a component. Let's say, you have a button inside that component and when is clicked, you wanna perform an action (AJAX request, or just redirect).

In this case you pass the URL as parameter to the component and you can use it inside.

HTML:

<my-component login-url="get_url('url_token')">

Your component JS:

export default {
    props: {
        loginUrl: {required: true},
    },
    ...
}

Then you can access the param in the component as this.loginUrl.

Please notice that param name difference in HTML part as dash-notatinon and in JS as camelCase.

like image 23
n1_ Avatar answered Oct 25 '22 05:10

n1_


If you are using vue-router and want to capture query parameters and pass them to props, you can do it this way.

Url being called : http://localhost:8080/#/?prop1=test1&prop2=test2

MyComponent.vue

<template>
  <div>
    {{prop1}} {{prop2}}
  </div>
</template>
<script>
  export default {
    name: 'MyComponent',
    props: {
      prop1: {
        type: String,
        required: true
      },
      prop2: {
        type: String,
        default:'prop2'
      }
    }
</script>
<style></style>

router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import MyComponent from '@/components/MyComponent'

Vue.use (Router) 
Vue.component ('my-component', MyComponent)

export default new Router ({
  routes: [
    {
      path: '/',
      name: 'MyComponent',
      component: MyComponent,
      props: (route) => ({
        prop1: route.query.prop1,
        prop2: route.query.prop2
      })
    }
  ]
})
like image 30
Marc Bouvier Avatar answered Oct 25 '22 06:10

Marc Bouvier