为什么我不能导入*这些Javascript文件?

时间:2017-12-06 03:27:02

标签: ecmascript-6 vue.js vuejs2 vue-router

我正在尝试在perimeters文件夹中导入这些文件

  1. basePerimeter.js
  2. byePerimeter.js
  3. secretPerimeter.js
  4. 我的导入代码:

    import * as perimeters from '../perimeters'
    

    basePerimeter.js

    import { Perimeter } from 'vue-kindergarten';
    
    export default class BasePerimeter extends Perimeter {
      isAdmin() {
        return this.child && this.child.role === 'admin';
      }
    }
    

    byePerimeter.js

    import basePerimeter from './basePerimeter';
    
    export default new basePerimeter({
      purpose: 'bye',
      govern: {    
        'can route': () => true,
        'can viewParagraph': function () {
          return this.isAdmin();
        },
      },
    });
    

    secretPerimeter.js

    import basePerimeter from './basePerimeter';
    
    export default new basePerimeter({
      purpose: 'secret',
      govern: {
        'can route': function () {
          return this.isAdmin();
        },
      },
    });
    

    但如果我单独导入它,它就可以。

    像这样:

    import basePerimeter from '../perimeters/basePerimeter'
    

    由于此代码,我需要通过*导入:

    router.beforeEach((to, from, next) => {
      const perimeter = perimeters[`${to.name}Perimeter`];
      if (perimeter) {
        const sandbox = createSandbox(child(store), {
          perimeters: [
            perimeter,
          ],
        });
        if (!sandbox.isAllowed('route')) {
          return next('/');
        }
      }
      return next();
    });
    

    为什么会抛出此错误:

    ERROR in ./src/router/index.js
    Module not found: Error: Can't resolve '../perimeters' in 'E:\my\vue\instance\src\router'
     @ ./src/router/index.js 13:0-44
     @ ./src/main.js
     @ multi ./build/dev-client ./src/main.js
    

1 个答案:

答案 0 :(得分:0)

我不知道这里发生了什么,但添加了index.js文件并导入我需要的文件解决了我的问题。

index.js

import byePerimeter from './byePerimeter'
import secretPerimeter from './secretPerimeter'

export {
    byePerimeter,
    secretPerimeter
}