Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

javascript - Webpack 2 and Angular 1: exporting and import modules

Hope to get some clarification on why the following doesn't work as expected, hopefully, it's something easy I may have overlooked. Without Webpack the current implementation works as expected.

Ideally, would like to keep current implementation, I feel registering the component/controller/etc should be done in its own file and just point to the relative module. But if this isn't best practice I'd also like to see another suggestion.

File root.module is where I define the root module and then in the root.component file I tack on the component to that module.

Current implementation that doesn't import the module:

//root.component.js
'use strict';

var root = {
  template: require('./root.html')
};

module.exports = angular
  .module('root')
  .component('root', root);
'use strict';

//root.module.js
module.exports = angular
    .module('root', [
        require('./common').name,
        require('./components').name
    ]);

If I do the following works and loads module as expected:

//root.component.js
'use strict';

var root = {
  template: require('./root.html')
};
module.exports = root;

//root.module.js
'use strict';

module.exports = angular
  .module('root', [
    require('./common').name,
    require('./components').name
  ])
  .component('root', require('./root.component'));

Current directory tree:

├── ./src
│?? ├── ./src/app
│?? │?? ├── ./src/app/app.less
│?? │?? ├── ./src/app/app.spec.js
│?? │?? ├── ./src/app/common
│?? │?? │?? ├── ./src/app/common/app.component.js
│?? │?? │?? ├── ./src/app/common/app.controller.js
│?? │?? │?? ├── ./src/app/common/app.html
│?? │?? │?? ├── ./src/app/common/footer
│?? │?? │?? │?? ├── ./src/app/common/footer/app-footer.component.js
│?? │?? │?? │?? ├── ./src/app/common/footer/app-footer.controller.js
│?? │?? │?? │?? ├── ./src/app/common/footer/app-footer.html
│?? │?? │?? │?? └── ./src/app/common/footer/index.js
│?? │?? │?? ├── ./src/app/common/header
│?? │?? │?? │?? ├── ./src/app/common/header/app-nav.component.js
│?? │?? │?? │?? ├── ./src/app/common/header/app-nav.controller.js
│?? │?? │?? │?? ├── ./src/app/common/header/app-nav.html
│?? │?? │?? │?? └── ./src/app/common/header/index.js
│?? │?? │?? ├── ./src/app/common/index.js
│?? │?? │?? └── ./src/app/common/sideBar
│?? │?? │??     ├── ./src/app/common/sideBar/app-sidebar.component.js
│?? │?? │??     ├── ./src/app/common/sideBar/app-sidebar.controller.js
│?? │?? │??     ├── ./src/app/common/sideBar/app-sidebar.html
│?? │?? │??     └── ./src/app/common/sideBar/index.js
│?? │?? ├── ./src/app/components
│?? │?? │?? ├── ./src/app/components/auth
│?? │?? │?? │?? ├── ./src/app/components/auth/auth-form
│?? │?? │?? │?? │?? ├── ./src/app/components/auth/auth-form/auth-form.component.js
│?? │?? │?? │?? │?? ├── ./src/app/components/auth/auth-form/auth-form.controller.js
│?? │?? │?? │?? │?? ├── ./src/app/components/auth/auth-form/auth-form.html
│?? │?? │?? │?? │?? └── ./src/app/components/auth/auth-form/index.js
│?? │?? │?? │?? ├── ./src/app/components/auth/auth.service.js
│?? │?? │?? │?? ├── ./src/app/components/auth/auth.user.js
│?? │?? │?? │?? ├── ./src/app/components/auth/index.js
│?? │?? │?? │?? ├── ./src/app/components/auth/login
│?? │?? │?? │?? │?? ├── ./src/app/components/auth/login/index.js
│?? │?? │?? │?? │?? ├── ./src/app/components/auth/login/login.component.js
│?? │?? │?? │?? │?? ├── ./src/app/components/auth/login/login.controller.js
│?? │?? │?? │?? │?? └── ./src/app/components/auth/login/login.html
│?? │?? │?? │?? └── ./src/app/components/auth/register
│?? │?? │?? │??     ├── ./src/app/components/auth/register/index.js
│?? │?? │?? │??     ├── ./src/app/components/auth/register/register.component.js
│?? │?? │?? │??     ├── ./src/app/components/auth/register/register.controller.js
│?? │?? │?? │??     └── ./src/app/components/auth/register/register.html
│?? │?? │?? └── ./src/app/components/index.js
│?? │?? ├── ./src/app/root.component.js
│?? │?? ├── ./src/app/root.html
│?? │?? └── ./src/app/root.module.js
│?? └── ./src/index.ejs
└── ./webpack.config.js
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A file should be imported (or more precisely, required, because the application relies on CommonJS modules) in order for it to be executed.

In the first snippet root.module.js doesn't contain require('./root.component'), so root.component.js is never executed.

It should be

//root.module.js
module.exports = anglar
  .module('root', [
    require('./common').name,
    require('./components').name
  ])
  .component('root', require('./root.component'));

require('./root.component');

Notice that root.component.js should be required after root module was defined, doing these in opposite order will result in $injector:modulerr error.

The proven way to eliminate race conditions and utilize modularity is to have one Angular module per one file. In this case it doesn't matter in which order the files are required. It is conventional to export and import module's name property from files that contain Angular modules:

//root.component.js
module.exports = angular.module('root.rootComponent', [])
  .component('root', {
    template: require('./root.html')
  })
  .name;

//root.module.js
var rootComponentModule = require('./root.component');
var commonModule = require('./common');
var componentsModule = require('./components');

module.exports = angular
    .module('root', [
        rootComponentModule,
        commonModule,
        componentsModule
    ])
    .name;

This recipe allows to maintain well-arranged deep hierarchy of highly modular units. This is beneficial for code reusing and testing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...