【问题标题】:Angular2/Typescript returns 404 not found when trying to import css-animatorAngular2/Typescript 在尝试导入 css-animator 时返回 404
【发布时间】:2016-06-23 13:52:55
【问题描述】:

我刚开始玩 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(…)

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

谢谢。

【问题讨论】:

标签: typescript angular angular2-directives polyfills


【解决方案1】:

您将animation_builder.js 直接包含在&lt;head&gt; 部分中:

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

你会得到一个 ReferenceError,因为 css-animator 包中包含的单个文件被编译为 CommonJS,浏览器本身不理解。

尝试删除该行,如果您在某处导入文件,SystemJS 应该会自动加载文件。 SystemJS 也理解 CommonJS。

或者,您可以在 &lt;head&gt; 部分中包含软件包提供的 SystemJS 包。如果你从 css-animator 导入一个模块,SystemJS 已经注册了这个模块,可以立即加载它。

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

<!-- 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>

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

【讨论】:

  • animation_builder.js:434 Uncaught ReferenceError: exports is not defined 已解决但我仍然有错误zone.js:101 GET http://localhost:3000/node_modules/css-animator/ 404 (Not Found)
  • 我认为你必须告诉 SystemJS css-animator 的主要入口点是什么。尝试将'css-animator': { main: 'index.js', defaultExtension: 'js' }, 添加到packages 对象。如果您有时间,您可能需要查看 jspm 以进行前端依赖管理。它会为您生成 SystemJS 配置文件。
  • 谢谢它的工作。我必须在我的包中添加 'css-animator': { main: 'index.js', defaultExtension: 'js' }。
猜你喜欢
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 2019-04-15
  • 2018-01-27
  • 1970-01-01
  • 2016-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多