Javascript 网页包2和1:导出和导入模块
希望得到一些澄清,为什么下面的工作没有如预期的那样,希望,这是一些容易我忽略了。如果没有Webpack,当前的实现将按预期工作 理想情况下,如果希望保持当前的实现,我觉得应该在自己的文件中注册组件/控制器/etc,并且只指向相关模块。但如果这不是最佳实践,我也希望看到另一个建议 文件root.module是我定义根模块的地方,然后在root.component文件中我将组件固定到该模块上 当前未导入模块的实现:Javascript 网页包2和1:导出和导入模块,javascript,angularjs,webpack,angular-ui-router,Javascript,Angularjs,Webpack,Angular Ui Router,希望得到一些澄清,为什么下面的工作没有如预期的那样,希望,这是一些容易我忽略了。如果没有Webpack,当前的实现将按预期工作 理想情况下,如果希望保持当前的实现,我觉得应该在自己的文件中注册组件/控制器/etc,并且只指向相关模块。但如果这不是最佳实践,我也希望看到另一个建议 文件root.module是我定义根模块的地方,然后在root.component文件中我将组件固定到该模块上 当前未导入模块的实现: //root.component.js "严格使用",; 变量根={ 模板:需要(“
//root.component.js
"严格使用",;
变量根={
模板:需要(“./root.html”)
};
module.exports=角度
.module('根')
.组件(“根”,根);
"严格使用",;
//root.module.js
module.exports=角度
.module('根'[
需要('./common')。名称,
需要('./组件')。名称
]);
如果我按预期完成以下工作并加载模块:
//root.component.js
"严格使用",;
变量根={
模板:需要(“./root.html”)
};
module.exports=root;
//root.module.js
"严格使用",;
module.exports=角度
.module('根'[
需要('./common')。名称,
需要('./组件')。名称
])
.component('root',require('./root.component'));
当前目录树:
├── ./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表单
│ │ │ │ │ ├── ./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
为了执行文件,应该导入文件(或者更准确地说,需要
d,因为应用程序依赖于CommonJS模块)
在第一个代码段中,root.module.js
不包含require('./root.component')
,因此永远不会执行root.component.js
应该是
//root.module.js
module.exports = anglar
.module('root', [
require('./common').name,
require('./components').name
])
.component('root', require('./root.component'));
require('./root.component');
请注意,在定义了root
模块之后,应该需要root.component.js
,按相反顺序执行这些操作将导致$injector:modulerr
错误
消除争用条件和利用模块化的行之有效的方法是每个文件有一个角度模块。在这种情况下,需要文件的顺序无关紧要。传统的做法是从包含数据的文件中导出和导入模块的name
属性
//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;
// src/components/foo/foo.component.js
import './foo.scss';
export class FooComponent {
static NAME = 'foo';
static OPTIONS = {
controller: FooComponent,
template : require('./foo.template.html'),
bindings : {},
};
constructor(FooService) {
'ngInject';
this._FooService = FooService;
}
$onInit() { /* ... */ }
$onDestroy() { /* ... */ }
/* ... */
}
// src/components/foo/foo.service.js
export class FooService {
/* ... */
}
// src/components/foo/index.js
import { FooComponent } from './foo.component';
import { FooService } from './foo.service';
export const FOO_COMPONENT = angular.module('components.foo', [])
.service('FooService', FooService)
.component(FooComponent.NAME, FooComponent.OPTIONS)
.name;
// src/components/index.js
export { FOO_COMPONENT } from './foo';
export { BAR_COMPONENT } from './bar';
/* ... */
// src/app/users/index.js
import { CORE } from 'shared/core';
import {
FOO_COMPONENT,
BAR_COMPONENT,
} from 'components';
import { USERS_LIST_COMPONENT } from './users-list';
import { USER_PROFILE_COMPONENT } from './user-profile';
/* ... */
export const USERS_MODULE = angular
.module('app.users', [
CORE,
FOO_COMPONENT,
BAR_COMPONENT,
USERS_LIST_COMPONENT,
USER_PROFILE_COMPONENT,
])
.name
// src/app/index.js
import { USERS_MODULE } from './users';
import { PRODUCTS_MODULE } from './users';
import { AppComponent } from './app.component';
export const APP_MODULE = angular
.module('app', [
USERS_MODULE,
PRODUCTS_MODULE,
])
.component(AppComponent.NAME, AppComponent.OPTIONS)
.name;