我对node.js非常陌生,我计划将我们的角1指令迁移到组件上,以便更容易地转换到角2。
因此,我的问题是,我有一些代码可以使用以下几个定义:
'use strict';
var angular = require('angular');
angular.module('dashboard')
.directive('yepNope', require('./yep-nope.directive'))
//.component('comp', require('./myFirstComponent.component'));
.component('comp', new (require('./myFirstComponent.component')));
这些指令和MyFirstComponent.component都是以相同的方式定义的:
'use strict';
function MyFirstComponent() {
function componentController($element){
var vm = this;
init();
function init(){
vm.api = {
bar : function(){
console.log('bar called');
},
foo : function(){
console.log('foo called');
}
};
}
this.$onInit = function(){
console.log("$onInit");
};
this.$postLink = function(){
console.log("$postLink");
};
this.$onChanges = function(changesObj){
console.log("$onChanges");
};
}
return {
bindings: { },
controller: componentController,
//controllerAs: '$ctrl',
template:'<div><h1>My Component header</h1></div>'
}
}
module.exports = MyFirstComponent;
和
'use strict';
function YepNopeDirective() {
return {
restrict: 'E',
link: function (scope, element, attrs) {
scope.$watch(attrs.check, function (val) {
var words = val ? 'Yep' : 'Nope';
element.text(words);
});
}
}
}
module.exports = YepNopeDirective;
有任何解释为什么定义组件我需要做一个新的(要求.虽然有指令,但这是不必要的吗?
.directive('yepNope', require('./yep-nope.directive'))
//.component('comp', require('./myFirstComponent.component'));
.component('comp', new (require('./myFirstComponent.component')));
谢谢,
大卫。
发布于 2016-10-04 07:29:00
我自己回答问题。
组件的定义方式与传统指令不同。指令需要一个函数,而组件需要一个选项对象:
app.directive(name, fn)
app.component(name, options)
发布于 2016-10-03 08:43:12
你根本不需要:
.directive('yepNope', YepNopeDirective);
如果您确实需要,您可能需要检查您的项目架构。
不能将指令转换为组件。
使用.component
但使用指令中止指令将应用程序转换为组件体系结构
https://stackoverflow.com/questions/39826645
复制相似问题