簡體   English   中英

如何在 React js 中導出組件以在另一個項目中使用?

[英]how to export component in react js to use in another project?

我在react js中制作了一個簡單的組件hello world

import React from 'react';

function App() {
  return (
    <div >
      hello4
    </div>
  );
}

export default App;

我像這樣導出索引文件

import App from './App'
module.exports = {
    App
}

現在我導出這個組件是index.js另一個項目中使用

所以在另一個項目中,我將上面的項目用作這樣的依賴項

"dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "pubnsi": "git+https://git@github.com/naveennsit/pubs.git",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.1"
  },

但是當我在另一個像這樣的項目中使用這個組件時,它給了我語法錯誤

import React from 'react';
import './App.css';
import App from "pubnsi/src/App";
function App1() {
  return (
    <div className="App">
   hello
      <App/>
    </div>
  );
}

export default App1;

我試圖在代碼沙箱中復制這個錯誤

https://codesandbox.io/s/nifty-glitter-bj6sz?file=/package.json

但在代碼沙箱中沒有出錯。

package.json文件

{
  "name": "consukmer",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "pubnsi": "git+https://git@github.com/naveennsit/pubs.git",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

要將 Github 存儲庫用作依賴項,您必須像這樣添加它:

"pubnsi": "naveennsit/pubs"

而不是這樣:

"pubnsi": "git+https://git@github.com/naveennsit/pubs.git",

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM