Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJs - bind input to url param

I would like to bind an input field to a route param in vue.js.

<input v-model="$route.params.query"/>

Is it possible?

like image 428
nagy.zsolt.hun Avatar asked Oct 22 '17 19:10

nagy.zsolt.hun


2 Answers

The most straight-forward way I found was the following:

<input v-model="foo" />

--

data() {
    return {
        foo: this.$route.query.foo
    };
},
watch: {
    foo(newVal) {
        this.$router.push({ query: { ...this.$route.query, foo: newVal } });
    },
    '$route.query.foo': function(val) {
        this.foo = val;
    }
}

Edit 2019-08-16: added watch for $route to react to back navigation.

like image 147
Kirill Rakhman Avatar answered Oct 20 '22 01:10

Kirill Rakhman


A bit of time has passed, but since the docs say "it is often a better idea to use a computed property rather than an imperative watch callback" I thought I would add this pattern which I tested successfully and also looks more elegant IMHO.

The idea is to use a computed property linked to the query param with explicit get and set methods and bind the input with v-model to that property.

So in your template:

<input v-model="query_param"/>

Then in your computed properties:

computed: {
  query_param: {
      get() {
        return this.$route.query.your_query_param
      },
      set(value) {
        /*  Here I use replace so that you're not actually 
        pushing a new page to the history stack */

        this.$router.replace({  
          query: {
            ...this.$route.query,
            your_query_param: value
          }
        })
      }
    }
}

This of course would work for a single query parameter. If you have multiple inputs you want to bind to different parameters just put additional computed properties in the same way.

like image 39
Sushi2all Avatar answered Oct 20 '22 00:10

Sushi2all