webpack - how to load the .vue files of a vue project with node.js -


i want analize current router config of project vue2. because wanna use cli generate vue component. before have load current route register info. when require router.js under router directory. node throws syntaxerror: unexpected token import. try many ways fix didn't work. please tell me right way load router config. thanks!

      //to load router config     const routerpath = path.join(process.cwd(), 'src', 'router', 'index.js');      if (existssync(routerpath)) {         routes = require(routerpath)     }      //error     import vue "vue";     ^^^^^^      syntaxerror: unexpected token import         @ object. (/users/mosx/projects/mjb-cli/lib/check-components.js:28:33)         @ module._compile (module.js:570:32)         @ object.module._extensions..js (module.js:579:10)         @ module.load (module.js:487:32)         @ trymoduleload (module.js:446:12)         @ function.module._load (module.js:438:3)         @ module.require (module.js:497:17)         @ require (internal/module.js:20:19)         @ object. (/users/mosx/projects/mjb-cli/bin/mjb-component:12:25)         @ module._compile (module.js:570:32)      // path/to/router/index.js     import router "vue-router";     import hello "../components/hello.vue";      vue.use(router);      export default new router({         routes: [             {                 path: '/',                 name: 'hello',                 component: hello,                 children: [                     {                         path: 'child',                         name: 'child',                         component: hello                     }                 ]             }          ]     })  

though modularity of code posted in question questionable, believe answer solve problem facing.
here goes..

to build below code, made use of vue boilerplate

vue init webpack-simple vue-cli 

then, installed vue-router package

npm install --save vue-router 

main.js file

import vue 'vue'; import {routes} './routes'; import vuerouter 'vue-router'; import app './app.vue';  vue.use(vuerouter); const router = new vuerouter({   routes : routes,   mode : 'history' })  new vue({   el: '#app',   router : router,   render: h => h(app) }) 

route.js file

import cricket './components/cricket.vue'; import football './components/football.vue'; import basketball './components/basketball.vue'; import sports './components/sports.vue'; export const routes = [     {         path: '/cricket',         component: cricket     },     {         path: '/football',         component: football     },     {         path: '/basketball',         component: basketball     },     {         path: '',         component: sports     } ]; 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -