【发布时间】:2017-04-11 04:21:14
【问题描述】:
我正在尝试使用 typescript 构建 angularjs (angular 1.) 项目的结构。 我正在使用 webpack 将 typescript 和 ES6 编译为 javascript。
在我的项目中,我将 webpack 配置为仅编译“app.ts”文件和其中包含的所有其他文件,并使用“import”语法:
import { config } from './config';///<--HERE
module angularSeed {
'use strict';
// Declare app level module which depends on views, and components
angular.module('myApp', [
'ngRoute',
'myApp.view1',
'myApp.view2',
'myApp.version'
]).config(config)//<-- using imported config.ts
}
我希望将我的项目拆分为 Angular 子模块并包含在主模块中,如下所示:
angular.module('mainApp',['myApp.version','myApp.view1','myApp.view2'])
问题是:如何导出子模块?
到目前为止我发现的唯一方法是将模块定义文件定义为类:
class view1 {
constructor(){
angular.module('myApp.view1', ['ngRoute'])
.config(['$routeProvider', ($routeProvider: angular.route.IRouteProvider) => {
$routeProvider.when('/view1', {
templateUrl: 'view1/view1.html',
controller: 'View1Ctrl'
});
}])
.controller('View1Ctrl', [() => {
}]);
}
}
export default view1;
在主文件中 - 使用“new”来触发构造函数:
import { config } from './config';
import view2 from './view2/view2';
import view1 from './view1/view1';
import version from './components/version/version';
module angularSeed {
'use strict';
new view2();///<-- HERE
new view1();
new version();
// Declare app level module which depends on views, and components
angular.module('myApp', [
'ngRoute',
'myApp.view1',
'myApp.view2',
'myApp.version'
]).config(config)
}
我感觉有更准确的方法。 我说的对吗?
谢谢
【问题讨论】:
标签: angularjs typescript import