问题
I'm using Ember CLI 1.13 and I'm attempting to create a router mixin in an addon that binds to the willTransition
hook, but the issue I'm encountering is not limited to this event.
At this point the mixin looks like such:
import Ember from 'ember';
export default Ember.Mixin.create({
genericFunction: function(transition) {
console.log('--- generic function ---');
}.on('willTransition')
});
When attempting to run the dummy app utilising the mixin, I get the following error:
Uncaught TypeError: (intermediate value)(intermediate value)(intermediate value).on is not a function
When including and using the addon in a separate application, there are no errors and everything functions as expected. There are no warnings, errors, anything else to hint towards any issues with its use in a full application.
The environment config of the dummy app and the separate application are identical, so I've ruled out environment config as a potential issue.
I'm not sure whether or not this is just broken functionality in Ember or whether I'm required to take some additional steps to make the dummy app behave as you'd expect it to when using hooks/events.
Any help would be greatly appreciated.
Thanks!
回答1:
The alternative (and better) solution is to keep prototype extensions disabled and use the non-prototype variant. This way, you don't force apps to use the prototype extensions that don't wish to.
import Ember from 'ember';
const {
Mixin,
on
} = Ember;
export default Mixin.create({
genericFunction: on('willTransition', function(transition) {
console.log('--- generic function ---');
})
});
回答2:
So after a few hours of digging it turns out that the fix for this is to remove the package ember-disable-prototype-extensions from your addon's package.json
file.
This dependency disables prototypes in your addon (or application) and is a default inclusion in any newly generated Ember addons to encourage developers to not rely on prototypes for their addons in case the consuming application has them disabled.
The reason the addon was working when used within the application was that Ember applications have prototypes enabled by default so it had no reason to fail with them enabled.
来源:https://stackoverflow.com/questions/32041446/ember-cli-hook-event-error