【问题标题】:how to import angularjs submodules in typescript如何在打字稿中导入angularjs子模块
【发布时间】: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


    【解决方案1】:

    我找到了here的答案

    您可以保持声明逻辑不变

    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';
    

    语法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 2017-10-29
      • 2017-02-21
      • 1970-01-01
      • 2022-01-14
      • 2018-01-10
      • 1970-01-01
      相关资源
      最近更新 更多