0.0.1 • Published 6 years ago
vue-auto-route v0.0.1
vue-auto-route
Usage
- import autoRoute from 'auto-route';
- const routes = autoRoute(require.context('@/view', true, /index.vue$/));
- put the routes into vue-router
import autoRoute from 'vue-auto-route';
const routes = autoRoute(require.context('@/view', true, /index\.vue$/));
new Router({
routes: [
...routes,
],
});
API
autoRoute(context, options)
- context: must generate by require.context
- options: options.ignore path RegExp which you don't want to put into router options.fileReg file RegExp which you want to replace empty string
- If a sub-component that you want to be mounted father-component, you should write them in vue instance:
export default {
name: 'ASubComponentName',
isBeMountedByTheParentComponent: true,
data() {
return {
...
};
},
}
- In the father-component, you can write a 'redirect' property that can redirect to sub-component:
export default {
redirect: 'path/to/sub/component'
name: 'AFatherComponentName',
data() {
return {
...
};
},
}
Tips
- route'name is token from vue instance's name, please make sure that every vue component has a name;
- Route which nests other child routes should be plat with child routes, like this:
├── config.js
├── fatherRoute.vue
├── childRoute1
│ └── index.vue
│ └── config.js
├── childRoute2
│ └── index.vue
│ └── config.js
0.0.1
6 years ago