亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

import 語句給出語法錯誤,指出“無法在模塊外部使用 import 語句”

import 語句給出語法錯誤,指出“無法在模塊外部使用 import 語句”

繁花不似錦 2022-09-02 17:19:57
我是Web開發的新手,這只是我創建的一個簡單的代碼,用于測試我安裝的新文本編輯器,我在嘗試導入react時遇到了此錯誤,因為我遇到了一些其他錯誤,例如;類型錯誤: 無法讀取未定義的屬性“createElement”請問,我該如何解決這個問題?導入語句有誤嗎?import React, { Fragment } from 'react';console.log("Testing");var document;//made this declaration because I got a document undefined error document.createElement("p"):const p = document.createElement("p")p.innerHTML = "This is to test the javascript";const body = document.querySelector("body");body.insertBefore(p,body.childNode[-1]);<!DOCTYPE html><html><head>  <title>Testing vs code</title>  <link rel="stylesheet" type="text/css" href="styles.css"/>  </head><body>  <h1>Testing the vscode html preview package</h1>  <h2>Hello</h2>  <script type="module" src="js-prac.js"></script></body></html>
查看完整描述

3 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

我知道你想用 React 創建一個簡單的應用程序。我建議你先讀一 https://kentcdodds.com/blog/how-to-react,然后再讀這個:https://reactjs.org/tutorial/tutorial.html

可以通過在開始時導入腳本來創建 react 應用程序,但這不是構建 react 應用程序的推薦方法。

完成上述帖子后,請在您選擇的平臺上找到您選擇的好教程,無論是基于博客還是基于視頻。我可以舉出一些像udemy,前端大師,復數視覺,還有更多。


查看完整回答
反對 回復 2022-09-02
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

看看 ReactJS 網站。


你應該使用 Node 包管理器創建 React 應用程序 npx create-react-app appName


或者應該將反應腳本鏈接到您的html


<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>


也不能重新定義文檔對象。這將引用您的網頁,您可以使用文檔對象訪問元素或 DOM(文檔對象模型)。


查看完整回答
反對 回復 2022-09-02
?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

根據 https://reactjs.org/docs/add-react-to-a-website.html,您需要在導入腳本之前將以下兩行添加到HTML文件中:


<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script> 

<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>

我不確定模塊加載是否會按照你想要的方式工作,而不使用像Create React App這樣的東西。您可以刪除導入語句,并且仍然可以在腳本中引用 React 和 ReactDOM。


例如:


'use strict';


const e = React.createElement;


class LikeButton extends React.Component {

  constructor(props) {

    super(props);

    this.state = { liked: false };

  }


  render() {

    if (this.state.liked) {

      return 'You liked comment number ' + this.props.commentID;

    }


    return e(

      'button',

      { onClick: () => this.setState({ liked: true }) },

      'Like'

    );

  }

}


// Find all DOM containers, and render Like buttons into them.

document.querySelectorAll('.like_button_container')

  .forEach(domContainer => {

    // Read the comment ID from a data-* attribute.

    const commentID = parseInt(domContainer.dataset.commentid, 10);

    ReactDOM.render(

      e(LikeButton, { commentID: commentID }),

      domContainer

    );

  });


查看完整回答
反對 回復 2022-09-02
  • 3 回答
  • 0 關注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號