React - component in seperate script does not work

前端 未结 3 437
名媛妹妹
名媛妹妹 2020-12-11 18:27

I\'m trying to learn react.js, but got stuck on \"Hello World\" script.

My index.html:



  
    

        
3条回答
  •  鱼传尺愫
    2020-12-11 19:12

    You get that error because:

    1. You have loaded the index.html from your local file system (e.g. by double clicking on it), instead of loading it via a web server
    2. The JSX transformer, the one responsible of text/jsx scripts is a javascript component that tries to fetch the file specified by the src attribute of the script tag
    3. Javascript is allowed to fetch external resources only from the protocols enumerated in the error message (and even for those has further limitation like cross-domain requess); files loaded from the local file system have the file:// protocol which is not within that list.

    When you included the jsx script in the index.html file it worked as no requests were needed in order to retrieve the jsx script.

    What you need to do is grab your hands on a web server, place the hello world files into the document root of that server, and load them from the web server, e.g. from an URL like http://localhost/index.html.

提交回复
热议问题