vue-route-props

Automatically bind vue-router query to vm, APIs are mostly same as the Vue props.
723 1
Install
npm install vue-route-props
Latest Version:2.0.0
Vue:2
License:MIT
Last Updated:Jun 16, 2024
Links: GitHub  ·  npm
Maintainer: iendeavor

vue-route-props

Automatically bind vue-router query to vm, APIs are mostly same as the Vue props.

npm version CI codecov

Install

npm install vue-route-props
yarn add vue-route-props

Why

In order to make route stateful(e.g, let user to copy one route, and paste in another tab), in this way you need to pass query instead of params. vue-route-props is implemented it which is much of the same in vue props.

Usage

Edit vue-route-props

import Vue from "vue";
import VueRouter from "vue-router";
import * as VueRouteProps from "vue-route-props";

Vue.use(VueRouter);
Vue.use(VueRouteProps);

new Vue({
  routeProps: {
    optional: {
      type: String,
      default: "an optional routeProp with default value",
    },
    required: {
      required: true,
      type: String,
    },
    multiple: {
      type: [String, Array, Object],
    },
    validator: {
      validator(value) {
        return value === "with custom validator";
      },
    },
  },
});

API

vm

Prop Types

In order to keep values' type, you need to ALWAYS use JSON.stringify to insert query:

this.$router.push({
  query: {
    willBeString: 0, // wrong, there will be an error occurs
    willBeNumber: JSON.stringify(0), // expected, the willBeNumber is bind with 0 now
  },
});

Options

Inspect mode

Since we bind routeProps to vm's root instead of data/computed, and so on, You cannot use the vue-dev-tools to inspect what value it is.

In order to inspect routeProps, you can enable inspect mode. we will log all routeProps when it is updated.

Vue.use(VueRouteProps, {
  inspect: true,
});
new Vue({
  routeProps: {
    prop: {
      type: String,
      default: "a default value",
    },
  },
});

/*
console:

[VueRouteProps info]: https://github.com/iendeavor/vue-route-props/blob/HEAD/{
  prop: "a default value"
}
*/

Debug mode

In general, we log errors while environment is not in production mode. you can override it with debug mode.

Vue.use(VueRouteProps, {
  debug: true,
});

Related Packages

vue-router-parse-props

Parse params and query in vue-router to component props.

2,357 2
@chronicstone/vue-route-query

Type-safe URL query parameter synchronization for Vue 3 with Zod validation

3,077
@znck/prop-types

Fluent prop validation for Vue

20,852
vue-prop-validation-helper

A Vue validator lib that provide utilities to help you validate your properties

7,014 8
@queryweave/vue-router

Vue Router adapter contracts for QueryWeave.

1,045 7

Version History

Version Released Vue License
2.0.0 ^2.6.12 MIT
1.1.0 ^2.6.11 MIT
1.0.1 ^2.6.11 MIT
1.0.0 ^2.6.11 MIT
0.1.2 ^2.6.11 MIT
Pre-releases (15)
Version Released Vue License
0.1.0-beta ^2.6.11 MIT
0.0.6-beta ^2.6.11 MIT
0.0.6-alpha.10 ^2.6.11 MIT
0.0.6-alpha.9 ^2.6.11 MIT
0.0.6-alpha.8 ^2.6.11 MIT