最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Top level javascript imports - Redux - Stack Overflow

programmeradmin5浏览0评论

I'm trying to learn redux and I've run into an error. I only have two files, an index.html file and a main.js file, there are links to the jquery and redux cdns in the html file. I've only gotten to 2.3 in the redux tutorial(.html) and am stuck. I have

import {createStore} from 'redux';

at the top of my main.js file, but when I load the application, I get an error pointing to line above saying

SyntaxError: import declarations may only appear at top level

What is a 'top level import declaration'?

Here is a gist to my code if that helps.

I'm trying to learn redux and I've run into an error. I only have two files, an index.html file and a main.js file, there are links to the jquery and redux cdns in the html file. I've only gotten to 2.3 in the redux tutorial(http://redux.js/docs/basics/Store.html) and am stuck. I have

import {createStore} from 'redux';

at the top of my main.js file, but when I load the application, I get an error pointing to line above saying

SyntaxError: import declarations may only appear at top level

What is a 'top level import declaration'?

Here is a gist to my code if that helps. https://gist.github./austincarvey/6e6c8fdc2640b0f9bbfb

Share Improve this question asked Dec 31, 2015 at 0:00 user3813472user3813472 1011 gold badge1 silver badge3 bronze badges 1
  • I am not sure there is enough information to figure this out. What browser produces this error? Also, Redux uses ES6 sysntax which means you are transpiling. Could you create a fiddle of this? – Nathaniel Johnson Commented Dec 31, 2015 at 3:00
Add a ment  | 

2 Answers 2

Reset to default 9

The import directive is not recognised by web browsers. It's used at the pilation stage, to tie together different source files and third party modules into a single web bundle. If that doesn't make sense, then I highly remended learning Babel and either Webpack or Browserify. Babel transpiles ES6 and React syntax to browser friendly ES5 code, whilst Webpack/Browserify bundle your modules.

For now however, if you just want to get on with learning Redux, you can simply remove the import statement and instead use the global variable Redux exposed by the redux CDN script in your gist. i.e.

var store = Redux.createStore(counterReducer);

import is used when you are including a file via es6 modules in a context that supports them (Browserify/Webpack/etc, future versions of browsers).

Since you are including the Redux lib via <script> tag, that takes care of including Redux in the global scope.

In the case of your gist, if you erase line one and change the createStore invocation to Redux.createStore on 29, everything should work.

发布评论

评论列表(0)

  1. 暂无评论