如何在AngularJS 1.6组件控制器中导入类?

时间:2016-12-26 11:59:46

标签: javascript angularjs angularjs-1.6

如果我想在AngularJS 1.6 component控制器中导入一个类,我必须使用什么语法?

例如,我有以下组件。

应用/电话list.component.js

// Register `phoneList` component, along with its associated controller and template
angular.
  module('phonecatApp').
  component('phoneList', {
    template:
        '<ul>' +
          '<li ng-repeat="phone in $ctrl.phones">' +
            '<span>{{phone.name}}</span>' +
            '<p>{{phone.snippet}}</p>' +
          '</li>' +
        '</ul>',
    controller: function PhoneListController() {
      this.phones = [
        {
          name: 'Nexus S',
          snippet: 'Fast just got faster with Nexus S.'
        }, {
          name: 'Motorola XOOM™ with Wi-Fi',
          snippet: 'The Next, Next Generation tablet.'
        }, {
          name: 'MOTOROLA XOOM™',
          snippet: 'The Next, Next Generation tablet.'
        }
      ];
    }
  }); 

我在一个单独的文件player.js中定义了一个类。

player.js

export default class Player {
  constructor () {
    // define properties
  }
  loadVideo () {
    // do stuff
  }
  playVideo () {
    // do stuff
  }
}

我想在PhoneListController()内使用此类:

let player = new Player();
player.loadVideo();
player.playVideo();
...

如果我将它放在控制器文件中,该类就可以工作。

如果我尝试按以下方式导入,则会收到unexpected token import

import Player from './player.js';

1 个答案:

答案 0 :(得分:1)

根据您的错误。这意味着您在JavaScript Polyfill中遇到问题Try this