├── .babelrc
├── .gitignore
├── .npmignore
├── README.md
├── example
├── components
│ ├── home.vue
│ ├── layout.vue
│ ├── user-card.vue
│ ├── user.vue
│ └── users.vue
├── index.html
├── index.js
└── routes.js
├── mix-manifest.json
├── package-lock.json
├── package.json
├── src
└── resolve.js
├── webpack.mix.js
├── yarn-error.log
└── yarn.lock
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["babel-preset-es2015", "babel-preset-minify"],
3 | "plugins": ["transform-object-rest-spread"]
4 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | public
3 | dist
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | example/
2 | public/
3 | .npmignore
4 | mix-manifest.json
5 | webpack.mix.js
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Vue-resolve
2 |
3 | A VueJS (2.x) / Vue-router plugin that resolves dependencies for routes before entering.
4 |
5 | ### What is this?
6 |
7 | A plugin that reads a `meta.resolve` property on your component's route and resolve it (as a promise) before serving the route.
8 |
9 | **Code Sandbox example**
10 |
11 | https://codesandbox.io/s/wk2k671jyl
12 |
13 | ## Install
14 |
15 | Install from npm:
16 | ```js
17 | npm install vue-resolve --save
18 | ```
19 |
20 | or from yarn:
21 | ```js
22 | yarn add vue-resolve
23 | ```
24 | ## Example
25 |
26 | ```js
27 | ...
28 | import VueResolve from 'vue-resolve';
29 |
30 | const router = new VueRouter({
31 | routes: [{
32 | path: '/',
33 | component: mycomp,
34 | meta: {
35 | resolve: {
36 | mydata() {
37 | return axios.get('/api/data');
38 | }
39 | }
40 | }
41 | }]
42 | });
43 |
44 | Vue.use(VueResolve, {
45 | router,
46 | dataProperty: 'data',
47 | });
48 |
49 | new Vue({
50 | ...
51 | router,
52 | });
53 | ```
54 |
55 | **Important:**
56 |
57 | For reactivity please make sure to define the *data* properties your route is going to populate in your component, so Vue can keep track of changes after the routes resolves. (e.g. on the `mycomp` component, *must* exist a `mydata` data)
58 |
59 | For a more complete example, please check the [example folder](/example).
60 |
61 | ### Options
62 |
63 | **resolved()**
64 |
65 | You can add a `resolved()` option in your component that will be executed when dependencies for that route/component are resolved.
66 |
67 | **this.$resolve()**
68 |
69 | You can use `this.$resolve()` to re-resolve dependencies, in case you want to run the same dependencies again without the need of duplicating it.
70 |
--------------------------------------------------------------------------------
/example/components/home.vue:
--------------------------------------------------------------------------------
1 |
2 |
8 | Is a plugin for Vue.js + Vue-router that lets you define a promise that must be
9 | resolved before rendering the route.
10 |
13 | For example you can set async requests (xhr) from an api, so the view renders
14 | after the data fetched is fetched.
15 |
20 | In your routes definition, you need to add a
26 | Each property defined inside the
32 | * Important: to avoid reactivity issues, you must
33 | define the data that will store the resolved value on the route's component.
34 |
39 | 1. Define the empty data property on your model, like this:
40 |
53 | 2. Define your route with the
76 | If you need to re-run the resolved dependencies,
77 | you can use the
87 | If you want to run something when the dependencies for a route is resolved,
88 | you can add a
94 | * Important due limitations on the way that Vue-router pass the instanced components
95 | to the guards, the {{ data.first_name }} {{ data.last_name }}Welcome to Vue-Resolve
4 |
5 | What is this?
6 |
7 | How to use
18 |
19 | resolve
propery
21 | inside the meta
properties, with all the dependencies you
22 | want to resolve for that route.
23 | resolve
key, must
27 | be a function and return a promise. Also, the property name must match the data entry on
28 | the component mapped to that route.
29 | Example
37 |
38 |
43 | const mycomponent = {
44 | data() {
45 | return {
46 | mydata: null
47 | };
48 | }
49 | };
50 |
51 |
52 | resolve
key as dependency:
54 |
56 | ...
57 | {
58 | path: '/',
59 | component: mycomponent,
60 | meta: {
61 | resolve: {
62 | mydata() {
63 | return axios.get('http://my.api.example/endpoint');
64 | }
65 | }
66 | }
67 | }
68 | ...
69 |
70 |
71 | Options
72 |
73 | Re-resolving dependencies
74 |
75 | $resolved()
, all the dependencies for the current route
78 | will be executed again and re-populated to the data.
79 |
80 | If data was modified since the route was resolved, this data will be lost as
81 | the route will be re-resolved.
82 | Executing a callback when the route is resolved
85 |
86 | resolve()
option (at the same level as the data
89 | or methods
options) on the component that is attached to the route,
90 | and that method is going to be executed once the routes dependencies are resolve.
91 | resolved
option is called at the same time as the resolved
.
96 |
18 |
19 |
Viewing User with ID: {{ user.id }}
22 |
23 | All Users
26 |
27 |