typescript - 尝试导入 css-animator 时,Angular2/Typescript 返回 404 not found

标签 typescript angular angular2-directives polyfills

我刚刚开始玩 angular2 并尝试用 css-animator 做一些动画

package.json

"dependencies": {
    "@angular/common": "2.0.0-rc.3",
    "@angular/compiler": "2.0.0-rc.3",
    "@angular/core": "2.0.0-rc.3",
    "@angular/forms": "0.1.1",
    "@angular/http": "2.0.0-rc.3",
    "@angular/platform-browser": "2.0.0-rc.3",
    "@angular/platform-browser-dynamic": "2.0.0-rc.3",
    "@angular/router": "3.0.0-alpha.7",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "@angular/upgrade": "2.0.0-rc.3",
    "angular2-in-memory-web-api": "0.0.12",
    "bootstrap": "^3.3.6",
    "chokidar": "^1.6.0",
    "core-js": "^2.4.0",
    "css-animator": "^1.2.4",
    "http-proxy": "^1.14.0",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "systemjs": "0.19.27",
    "zone.js": "^0.6.12"
 }

index.html

<html>
  <head>
   <title>Angular 2 QuickStart</title>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <link rel="stylesheet" href="app/css/bootstrap.css">
   <link rel="stylesheet" href="app/css/style.css">
   <!-- 1. Load libraries -->
 <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
   <script src="node_modules/systemjs/dist/system.src.js"></script>
   <script src="node_modules/css-animator/builder/animation_builder.js">      </script>
   <!-- 2. Configure SystemJS -->
   <script src="systemjs.config.js"></script>
   <script>
     System.import('app').catch(function(err){ console.error(err); });
   </script>
         </head>
     <!-- 3. Display the application -->
        <body class="bg-image">
          <my-app>Loading...</my-app>
          </body>
      </html>

systemjs.config.js

var map = {
    'app':                        'app', // 'dist',

    '@angular':                   'node_modules/@angular',
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
    'rxjs':                       'node_modules/rxjs',
    'css-animator':               'node_modules/css-animator'
  };
   var packages = {
    'app':                        { main: 'main.js',  defaultExtension: 'js' },
    'rxjs':                       { defaultExtension: 'js' },
    'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js'   },
     };
     System.config(config);

custom.animate.directive.ts

import {Directive, ElementRef, Renderer} from '@angular/core';
 import {AnimationService, AnimationBuilder} from 'css-animator';

 @Directive({
    selector: '[login-method]',
    host: {
      '(click)':'onClick()'
   }
})

export class LoginOptionsDirective{
   private animator : AnimationBuilder;

   constructor(animationService: AnimationService, private el: ElementRef, private renderer: Renderer){
      this.animator = animationService.builder();
  }

  onclick(){
      this.renderer.setElementStyle(this.el, 'color','black');
      console.log(this.el.nativeElement.getAttribute("login-method"));
   }
 }

我得到 animation_builder.js:434 Uncaught ReferenceError: Exports is not Definedlocalhost/:18 Error: Error: XHR error (404 Not Found) loading http://localhost:浏览器控制台上的 3000/node_modules/css-animator(…)

有人可以帮我解决这个问题吗?

谢谢。

最佳答案

您包括animation_builder.js直接在 <head>部分:

<script src="node_modules/css-animator/builder/animation_builder.js"></script>

您会收到引用错误,因为 css-animator 中包含单个文件包被编译为 CommonJS,浏览器本身无法理解。

尝试删除该行,如果您将文件导入到某个地方,SystemJS 应该会自动加载这些文件。 SystemJS 也理解 CommonJS。

或者,您可以将包提供的 SystemJS 包包含在 <head> 中。部分。如果您随后从 css-animator 导入模块,SystemJS 已注册该模块并可以立即加载它。

您可以像这样包含缩小或未缩小的 bundle :

<!-- Unminified Source -->
<script src="node_modules/css-animator/bundles/css-animator.js"></script>

<!-- Minified Source -->
<script src="node_modules/css-animator/bundles/css-animator.min.js"></script>

免责声明:我是这个包的作者。

关于typescript - 尝试导入 css-animator 时,Angular2/Typescript 返回 404 not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37993651/

相关文章:

angular - 如何使用 docker-compose 运行 Angular?

用于 2 路属性绑定(bind)的 Angular2 装饰器

angular - 尽管主题接下来调用,但指令订阅不会触发

angular - 将 ngModel 绑定(bind)到选择控件的模型

angular - 如何将动态添加的自定义组件注册为表单组中的表单控件

typescript :理解联合和交叉类型

TypeScript 无法使用 keyof 泛型参数作为对象的键

javascript - 为什么我的 readState 逻辑中 document.body 为 null?

javascript - Discord.js 在普通消息上发送超链接,就像在用户消息中一样

typescript - 如何在 Angular 2-Typescript 中设置可选的类参数?