gpt4 book ai didi

reactjs - 在 React 中使用 CDN

转载 作者:行者123 更新时间:2023-12-05 07:06:03 31 4
gpt4 key购买 nike

我已经通过 NPM 安装了 React,但我在使用 CDN 时遇到了困难。
我在 ./public/index.html 中包含了 CDN 脚本文件,但是当我在任何组件中使用它时,它无法识别我尝试使用的第三方包。

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.5/redux.min.js"></script>

尝试使用 Axios 但它不起作用:

axios.post("https://burger-builder-c5a0d.firebaseio.com/orders.json", order)
.then(response=>{
alert("You ordered successfully!"); //shows an alert indicating a successful process
console.log(response); //sends results to the server
this.setState({
showModal: false, //closes the modal
loading:false,
purchasable: false
})

}).catch(error=>{
this.setState({
showModal: false,
purchasable: false,
loading: false
})
console.log(error);
})

尝试使用 redux 也无法识别

const reduxStore= redux.createStore;
const store = createStore();

Here's the error that I'm getting

最佳答案

你是不是通过require引入了axios或者redux包?像这样:

const axios = require('axios');

编辑

你是对的。你甚至不需要 require 一些东西,因为你是从 CDN 导入的

我尝试使用 React.js 文档中的 HTML 样板,并从您的源导入 CDN。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello World</title>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>

<!-- Don't use this in production: -->
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function fetch() {
axios
.get("https://jsonplaceholder.typicode.com/todos/1")
.then(response => console.log(response.data));
}
ReactDOM.render(<h1>{fetch()}</h1>, document.getElementById("root"));
</script>
<!--
Note: this page is a great way to try React but it's not suitable for production.
It slowly compiles JSX with Babel in the browser and uses a large development build of React.

Read this section for a production-ready setup with JSX:
https://reactjs.org/docs/add-react-to-a-website.html#add-jsx-to-a-project

In a larger project, you can use an integrated toolchain that includes JSX instead:
https://reactjs.org/docs/create-a-new-react-app.html

You can also use React without JSX, in which case you can remove Babel:
https://reactjs.org/docs/react-without-jsx.html
-->
</body>
</html>

关于reactjs - 在 React 中使用 CDN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62608937/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com