我遇到这个问题,用babel和webpack运行es6:
Module build failed: SyntaxError: /Users/agarcia/Projects/app/services/router/router.js: Unexpected token (9:10)
> 9 | const requestAuth = [
| ^
10 | '#profile'
11 | ];
12 |
有谁知道那里有什么问题? 我不确定我是否可以用这种方式定义常量。
文件:
import Backbone from 'backbone';
import Marionette from 'backbone.marionette';
import Session from '../session/session';
class BaseRouter extends Marionette.AppRouter {
// Routes that need authentication and if user is not authenticated
// gets redirect to login page
const requestAuth = [
'#profile'
];
// Routes that should not be accessible if user is authenticated
// for example, login, register, forget-password ...
const preventAccessWhenAuth = [
'#login'
];
before(params, next) {
// Checking if user is authenticated or not
// then check the path if the path requires authentication
let isAuth, path, needAuth, cancelAccess;
isAuth = Session.get('authenticated');
path = Backbone.history.location.hash;
needAuth = _.contains(this.requestAuth, path);
cancelAccess = _.contains(this.preventAccessWhenAuth, path);
if(needAuth && !isAuth) {
// If user gets redirect to login because wanted to access
// to a route that requires login, save the path in session
// to redirect the user back to path after successful login
Session.set('redirectFrom', path);
Backbone.history.navigate('login', { trigger : true });
} else if (isAuth && cancelAccess) {
// User is authenticated and tries to go to login, register ...
// so redirect the user to home page
Backbone.history.navigate('', { trigger : true });
} else {
// No problem, handle the route!!
return next();
}
}
after() {}
onRoute(route, name, callback) {
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
if (_.isFunction(name)) {
callback = name;
name = '';
}
if (!callback) callback = this[name];
var router = this;
Backbone.history.route(route, function(fragment) {
let args = router._extractParameters(route, fragment);
let next = function() {
callback && callback.apply(router, args);
router.trigger.apply(router, ['route:' + name].concat(args));
router.trigger('route', name, args);
Backbone.history.trigger('route', router, name, args);
router.after.apply(router, args);
};
router.before.apply(router, [args, next]);
});
return this;
}
}
导出默认BaseRouter;
答案 0 :(得分:0)
我修正了从类中取出那些常量:
import Backbone from 'backbone';
import Marionette from 'backbone.marionette';
import Session from '../session/session';
// Routes that need authentication and if user is not authenticated
// gets redirect to login page
const requestAuth = [
'#profile
];
// Routes that should not be accessible if user is authenticated
// for example, login, register, forget-password ...
const preventAccessWhenAuth = [
'#login'
];
class BaseRouter extends Marionette.AppRouter {
before(params, next) {
// Checking if user is authenticated or not
// then check the path if the path requires authentication
let isAuth, path, needAuth, cancelAccess;
isAuth = Session.get('authenticated');
path = Backbone.history.location.hash;
needAuth = _.contains(requestAuth, path);
cancelAccess = _.contains(preventAccessWhenAuth, path);
if(needAuth && !isAuth) {
// If user gets redirect to login because wanted to access
// to a route that requires login, save the path in session
// to redirect the user back to path after successful login
Session.set('redirectFrom', path);
Backbone.history.navigate('login', { trigger : true });
} else if (isAuth && cancelAccess) {
// User is authenticated and tries to go to login, register ...
// so redirect the user to home page
Backbone.history.navigate('', { trigger : true });
} else {
// No problem, handle the route!!
return next();
}
}
after() {}
onRoute(route, name, callback) {
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
if (_.isFunction(name)) {
callback = name;
name = '';
}
if (!callback) callback = this[name];
var router = this;
Backbone.history.route(route, function(fragment) {
let args = router._extractParameters(route, fragment);
let next = function() {
callback && callback.apply(router, args);
router.trigger.apply(router, ['route:' + name].concat(args));
router.trigger('route', name, args);
Backbone.history.trigger('route', router, name, args);
router.after.apply(router, args);
};
router.before.apply(router, [args, next]);
});
return this;
}
}
导出默认BaseRouter;
答案 1 :(得分:0)
您可以阅读有关ES6 const
es。
您可以将set
移出课程,也可以只使用属性并限制get MY_CONST() {
return {a: 1};
}
set MY_CONST(value) {
console.warn('Readonly!');
}
render() {
this.MY_CONST = 2;
}
部分属性。它也将只读:
statics
如果需要,您还可以添加{{1}}