繁体   English   中英

如何在打字稿中导入angularjs子模块

[英]how to import angularjs submodules in typescript

我正在尝试使用打字稿来构建angularjs(angular 1.)项目的结构。 我正在使用webpack将打字稿和ES6编译为javascript。

在我的项目中,我将webpack配置为仅使用“ import”语法编译“ app.ts”文件和其中包含的所有其他文件:

    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.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)


}

我有一种更准确的方法。 我对吗?

谢谢

我在这里找到答案

您可以保留声明逻辑原样

angular.module('myApp.view1', ['ngRoute'])

.config(['$routeProvider', ($routeProvider: angular.route.IRouteProvider) =>    {
  $routeProvider.when('/view1', {
    templateUrl: 'view1/view1.html',
    controller: 'View1Ctrl'
  });
}])

.controller('View1Ctrl', [() => {

}]); 

并简单地导入它:

import './view1/view1';

句法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM