lion9527
V2EX  ›  问与答

React 如何引入外部 Js 文件?

  •  
  •   lion9527 · Jul 20, 2017 · 15767 views
    This topic created in 3229 days ago, the information mentioned may be changed or developed.

    我想引入阿里云 oss 的 sdk,在 index.html 的 <head> 里引入了:

    <script src="//gosspublic.alicdn.com/aliyun-oss-sdk.min.js"></script>
    

    然后在 component 里这样使用:

    const client = new OSS.Wrapper({
      region: 'oss-cn-hongkong',
      accessKeyId: '<access-key-id>',
      accessKeySecret: '<access-key-secret>',
      bucket: 'o-image-test'
    })
    

    编译的时候就报错了: 'OSS' is not defined no-undef

    请问要怎么才能使用?

    6 replies    2017-07-20 11:54:44 +08:00
    Raincal
        1
    Raincal  
       Jul 20, 2017   ❤️ 1
    这是 ESLint 的报错,可以在上面加句 // eslint-disable-next-line
    mooncakejs
        2
    mooncakejs  
       Jul 20, 2017   ❤️ 2
    真正解决方案是,在 webpack 里加一个 externals
    lion9527
        3
    lion9527  
    OP
       Jul 20, 2017
    @Raincal
    谢谢,用这个方式解决了。

    @mooncakejs
    我没有用到 webpack, 直接用的 create-react-app.
    hronro
        4
    hronro  
       Jul 20, 2017
    @lion9527 create-react-app 就是基于 webpack 和 eslint 的
    crysislinux
        5
    crysislinux  
       Jul 20, 2017 via Android
    eslint 的规则里把 OSS 申明成 global 就好了
    66beta
        6
    66beta  
       Jul 20, 2017
    同楼上 /* global OSS */
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2832 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 14:54 · PVG 22:54 · LAX 07:54 · JFK 10:54
    ♥ Do have faith in what you're doing.