我在使用生成器时遇到问题。我在控制台中收到以下错误:
ERROR in ./app/redux/sagas/tracking.saga.js Module build failed: SyntaxError: C:/Workspace/teamable-frontend/app/redux/sagas/tracking.saga.js: Unexpected token (18:4)
这是 package.json
:
{
"devDependencies": {
"autoprefixer-loader": "^3.2.0",
"babel-cli": "^6.4.5",
"babel-core": "^6.4.5",
"babel-loader": "^6.2.1",
"babel-plugin-transform-runtime": "^6.12.0",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-stage-0": "^6.5.0",
"babel-runtime": "^6.11.6",
...
}
...
}
以及 webpack.config
中的加载器:
module: {
loaders: [{
test: /.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react', 'stage-0'],
plugins: ["transform-runtime"]
}
},
...
}
以及使用生成器的函数:
import {put, call} from 'redux-saga/effects';
import {takeEvery} from 'redux-saga';
import {LOAD} from '../../constants/ActionTypes';
import {loadTrackingItemsSuccess, loadTrackingItemsFail} from '../actions/tracking.actions';
import {getTrackingItems} from '../../mocks/ListMock'
function* loadTrackingItems() {
try {
const trackingItems = yield call(getTrackingItems);
yield put(loadTrackingItemsSuccess(trackingItems));
} catch(ex) {
yield put(loadTrackingItemsFail(ex.toString()));
}
}
export function watchTrackingItemsLoad() {
yield* takeEvery(LOAD, loadTrackingItems);
}
我做错了什么?
最佳答案
yield
和 yield*
只能在生成器函数中使用。这里:
export function watchTrackingItemsLoad() {
yield* takeEvery(LOAD, loadTrackingItems);
}
您正在普通函数中使用 yield*
。该函数也应该是一个生成器(function* watchTrackingItemsLoad
),或者您应该返回生成器对象并让调用者处理它(return takeEvery(LOAD, loadTrackingItems);
).
关于控制台中的 Javascript 生成器语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38898614/