如何在 AngularJS 1.6 组件控制器中导入 class?
How to import a class in AngularJS 1.6 component controller?
如果我想在 AngularJS 1.6 component 控制器中导入 class,我必须使用什么语法?
例如,我有以下组件。
app/phone-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
中定义了一个 class。
player.js
export default class Player {
constructor () {
// define properties
}
loadVideo () {
// do stuff
}
playVideo () {
// do stuff
}
}
我想像这样在 PhoneListController()
中使用这个 class:
let player = new Player();
player.loadVideo();
player.playVideo();
...
如果我把它放在控制器文件中,class 就可以工作。
如果我尝试按以下方式导入它,我会收到 unexpected token import
:
import Player from './player.js';
根据您遇到的错误。这意味着你在 JavaScript Polyfill Try this
中有问题
如果我想在 AngularJS 1.6 component 控制器中导入 class,我必须使用什么语法?
例如,我有以下组件。
app/phone-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
中定义了一个 class。
player.js
export default class Player {
constructor () {
// define properties
}
loadVideo () {
// do stuff
}
playVideo () {
// do stuff
}
}
我想像这样在 PhoneListController()
中使用这个 class:
let player = new Player();
player.loadVideo();
player.playVideo();
...
如果我把它放在控制器文件中,class 就可以工作。
如果我尝试按以下方式导入它,我会收到 unexpected token import
:
import Player from './player.js';
根据您遇到的错误。这意味着你在 JavaScript Polyfill Try this
中有问题