使用发电机时遇到问题。我在控制台中收到以下错误:
> ./app/redux/sagas/tracking.saga.js中的错误模块构建失败: 语法错误: C:/Workspace/teamable-frontend/app/redux/sagas/tracking.saga.js: 意外的令牌(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);
}
我做错了什么?
答案 0 :(得分:4)
yield
和yield*
只能在生成器函数中使用。这里:
export function watchTrackingItemsLoad() {
yield* takeEvery(LOAD, loadTrackingItems);
}
您在正常功能中使用yield*
。该函数也应该是生成器(function* watchTrackingItemsLoad
),或者您应该返回生成器对象并让调用者处理它(return takeEvery(LOAD, loadTrackingItems);
)。