簡體   English   中英

Lodash 導入 function 未找到

[英]Lodash import function not found

我正在嘗試將“管道”function 從 lodash 導入到我的項目中,但在瀏覽器控制台中出現錯誤,提示找不到 404。

索引.html:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Functional Programming</title>
    <script defer type="module" src="script.js"></script>
</head>
<body>
     <h1>Functional Programming</h1>
</body>
</html>

package.json:

{
  "devDependencies": {
    "eslint": "^8.5.0",
    "eslint-plugin-functional": "^4.0.2",
    "eslint-plugin-immutable": "^1.0.0",
  },
  "dependencies": {
    "lodash": "^4.17.21"
  }
}

JS文件:

import { pipe } from "./node_modules/lodash/fp";

const capitalize = text => text.charAt(0).toUpperCase() + text.slice(1);
const shortenText = text => text.substring(0, 8).trim();

const shortText = pipe(capitalize, shortenText)("this is a long text");

JS定位錯誤

從lodash導入pipe到javascript項目的解決方案:

import { pipe } from 'lodash/fp';

const capitalize = (text) => text.charAt(0).toUpperCase() + text.slice(1);
const shortenText = (text) => text.substring(0, 8).trim();

const shortText = pipe(capitalize, shortenText)('this is a long text');

console.log(shortText);


有關更多信息,請查看此示例: https://stackblitz.com/edit/js-zcoxit

暫無
暫無

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

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