Angular2+ typescript 項目里面用require

在typescript里面怎么使用require方法呢?

const jQuery = require('jquery');
const fip = require( '@fonticonpicker/fonticonpicker' )( jQuery );

如果什么都不做,直接在項目里面使用,會得到以下錯誤:

`Cannot find name 'require' 

以下方法可以解決上面的錯誤:

1. Install using yarn add @types/node or npm install @types/node --save-dev
2. Add "node" to the "types" option in your tsconfig.json, so it looks like "types": ["node"]. 
This makes them available in any file in your project and you don't have to use reference comments.

那么為什么要做第二步呢?其實做不做第二步是要分情況的。想要解釋這個問題我們就需要先了解

@types, typeRoots 和 types 三者是什么意思,它們之間有什么關系。下面是官網的解釋?

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types

If?typeRoots?is specified,?only?packages under?typeRoots?will be included. For example:

{"compilerOptions": {"typeRoots" : ["./typings"] } } 

This config file will include?all?packages under?./typings, and no packages from?./node_modules/@types.

If?types?is specified, only packages listed will be included. For instance:

{"compilerOptions": {"types" : ["node", "lodash", "express"] } } 

This?tsconfig.json?file will?only?include?./node_modules/@types/node,?./node_modules/@types/lodashand?./node_modules/@types/express. Other packages under?node_modules/@types/*?will not be included.

A types package is a folder with a file called?index.d.ts?or a folder with a?package.json?that has a?types?field.

Specify?"types": []?to disable automatic inclusion of?@types?packages.

Keep in mind that automatic inclusion is only important if you’re using files with global declarations (as opposed to files declared as modules). If you use an?import "foo"?statement, for instance, TypeScript may still look through?node_modules?&?node_modules/@types?folders to find the?foo?package.

?

總結一下上面一段話的意思就是:

@types 指的是文件夾 ./node-modules/@types/... 下面含有indes.d.ts或者package.json文件的module;
"types":[] 和 "typeRoots": [] 是 tsconfig.json里面的兩個配置屬性;當types:[]屬性設置了,那么只有types屬性里面的模塊會被加載編譯,typeRoots里面的模塊會被忽略;
如果types屬性沒有被設置,那么會加載typeRoots屬性里面設置的模塊。而用import關鍵字導入的模塊,編譯器還是會在node_modules?&?node_modules/@types兩個文件夾下面尋找;
types和typeRoots這兩個屬性不會影響被import進項目的模塊的加載

上面的總結應該解決了我上面提出的問題:為什么第二步做與不做是分情況的。

如果在tsconfig.json文件里面設置了

 "typeRoots": ["node_modules/@types"]
如下代碼,那么是不需要設置的。因為編譯器會默認加載node-modules/@types下面的modules。而如果我們安裝了 @types/node,那么node模塊是存在node-modules/@types文件夾下面的,
因此是不需要額外把它的加載列在 "types": ["node"] 屬性里面。
{"compileOnSave": false,"compilerOptions": {"baseUrl": "./","outDir": "./dist/out-tsc","sourceMap": true,"declaration": true,"moduleResolution": "node","emitDecoratorMetadata": true,"experimentalDecorators": true,"target": "es5","typeRoots": ["node_modules/@types"],"lib": ["es2017","dom"]}
}

如果你的項目中的tsconfig.json文件中已經設置了 "types": [....] 屬性,那么需要把node模塊也加在里面;否則編譯器只會編譯"types":[...]屬性中列出來的模塊,而不管typeRoots里面列的模塊。

{"compileOnSave": false,"compilerOptions": {"baseUrl": "./","outDir": "./dist/out-tsc","sourceMap": true,"declaration": true,"moduleResolution": "node","emitDecoratorMetadata": true,"experimentalDecorators": true,"target": "es5","types": ["jquery", "node"],"typeRoots": ["node_modules/@types"],"lib": ["es2017","dom"]}
}

?

轉載于:https://www.cnblogs.com/juliazhang/p/10103966.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388946.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388946.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388946.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

機器學習實踐三---神經網絡學習

Neural Networks 在這個練習中,將實現神經網絡BP算法,練習的內容是手寫數字識別。Visualizing the data 這次數據還是5000個樣本,每個樣本是一張20*20的灰度圖片fig, ax_array plt.subplots(nrows10, ncols10, figsize(6, 4))for row in range(10):fo…

Microsoft Expression Blend 2 密鑰,key

Microsoft Expression Blend 2 密鑰,key,序列TJ2R3-WHW22-B848T-B78YJ-HHJWJ號

ethereumjs/ethereumjs-common-3-test

查看test能夠讓你更好滴了解其API文檔的使用 ethereumjs-common/tests/chains.js const tape require(tape) const Common require(../index.js)tape([Common]: Initialization / Chain params, function (t) {t.test(Should initialize with chain provided, function (st) …

mysql修改_mysql修改表操作

一: 修改表信息1.修改表名alter table test_a rename to sys_app;2.修改表注釋alter table sys_application comment 系統信息表;二:修改字段信息1.修改字段類型和注釋alter table sys_application modify column app_name varchar(20) COMMENT 應用的名…

機器學習實踐四--正則化線性回歸 和 偏差vs方差

這次實踐的前半部分是,用水庫水位的變化,來預測大壩的出水量。 給數據集擬合一條直線,可能得到一個邏輯回歸擬合,但它并不能很好地擬合數據,這是高偏差(high bias)的情況,也稱為“欠…

深度學習 推理 訓練_使用關系推理的自我監督學習進行訓練而無需標記數據

深度學習 推理 訓練背景與挑戰📋 (Background and challenges 📋) In a modern deep learning algorithm, the dependence on manual annotation of unlabeled data is one of the major limitations. To train a good model, usually, we have to prepa…

Android strings.xml中定義字符串顯示空格

<string name"str">字 符 串</string> 其中 就表示空格。如果直接在里面鍵入空格&#xff0c;無論多少空格都只會顯示一個。 用的XML轉義字符記錄如下&#xff1a; 空格&#xff1a; <string name"out_bound_submit">出 庫</strin…

WCF開發入門的六個步驟

在這里我就用一個據于一個簡單的場景&#xff1a;服務端為客服端提供獲取客戶信息的一個接口讀取客戶信息&#xff0c;來完成WCF開發入門的六個步驟。 1. 定義WCF服務契約 A. 項目引用節點右鍵添加引用。 B. 在代碼文件里&#xff0c;添加以下命名空間的引…

LOJ116 有源匯有上下界最大流(上下界網絡流)

考慮有源匯上下界可行流&#xff1a;由匯向源連inf邊&#xff0c;那么變成無源匯圖&#xff0c;按上題做法跑出可行流。此時該inf邊的流量即為原圖中該可行流的流量。因為可以假裝把加上去的那些邊的流量放回原圖。 此時再從原來的源向原來的匯跑最大流。超源超匯相關的邊已經流…

CentOS 7 使用 ACL 設置文件權限

Linux 系統標準的 ugo/rwx 集合并不允許為不同的用戶配置不同的權限&#xff0c;所以 ACL 便被引入了進來&#xff0c;為的是為文件和目錄定義更加詳細的訪問權限&#xff0c;而不僅僅是這些特別指定的特定權限。 ACL 可以為每個用戶&#xff0c;每個組或不在文件所屬組中的用…

機器學習實踐五---支持向量機(SVM)

之前已經學到了很多監督學習算法&#xff0c; 今天的監督學習算法是支持向量機&#xff0c;與邏輯回歸和神經網絡算法相比&#xff0c;它在學習復雜的非線性方程時提供了一種更為清晰&#xff0c;更強大的方式。 Support Vector Machines SVM hypothesis Example Dataset 1…

作為微軟技術.net 3.5的三大核心技術之一的WCF雖然沒有WPF美麗的外觀

作為微軟技術.net 3.5的三大核心技術之一的WCF雖然沒有WPF美麗的外觀 但是它卻是我們開發分布式程序的利器 但是目前關于WCF方面的資料相當稀少 希望我的這一系列文章可以幫助大家盡快入門 下面先介紹一下我的開發環境吧 操作系統&#xff1a;windows vista business版本 編譯器…

服務器安裝mysql_阿里云服務器上安裝MySQL

關閉防火墻和selinuxCentOS7以下&#xff1a;service iptables stopsetenforce 0CentOS7.xsystemctl stop firewalldsystemctl disable firewalldsystemctl status firewalldvi /etc/selinux/config把SELINUXenforcing 改成 SELINUXdisabled一、安裝依賴庫yum -y install make …

在PyTorch中轉換數據

In continuation of my previous post ,we will keep on deep diving into basic fundamentals of PyTorch. In this post we will discuss about ways to transform data in PyTorch.延續我以前的 發布后 &#xff0c;我們將繼續深入研究PyTorch的基本原理。 在這篇文章中&a…

「網絡流24題」試題庫問題

傳送門&#xff1a;>Here< 題意&#xff1a;有K種類型的共N道試題用來出卷子&#xff0c;要求卷子須有M道試題。已知每道題屬于p種類型&#xff0c;每種類型的試題必須有且僅有k[i]道。現問出這套試卷的一種具體方案 思路分析 昨天打了一天的Dinic&#xff0c;今天又打了…

機器學習實踐六---K-means聚類算法 和 主成分分析(PCA)

在這次練習中將實現K-means 聚類算法并應用它壓縮圖片&#xff0c;第二部分&#xff0c;將使用主成分分析算法去找到一個臉部圖片的低維描述。 K-means Clustering Implementing K-means K-means算法是一種自動將相似的數據樣本聚在一起的方法,K-means背后的直觀是一個迭代過…

航海家軟件公式全破解

水手突破 上趨勢:MA(LOW,20)*1.2,color0080ff,linethick2;次上趨勢:MA(LOW,20)*1.1,COLORYELLOW;次下趨勢:MA(HIGH,20)*0.9,COLORWHITE;下趨勢:MA(HIGH,20)*0.8,COLORGREEN,linethick2;ZD:(C-REF(C,1))/REF(C,1)*100;HDZF:(HHV(H,20)-C)/(HHV(H,20)-LLV(L,20));趨勢強度:IF(C&g…

打包 壓縮 命令tar zip

2019獨角獸企業重金招聘Python工程師標準>>> 打包 壓縮 命令tar zip tar語法 #壓縮 tar -czvf ***.tar.gz tar -cjvf ***.tar.bz2 #解壓縮 tar -xzvf ***.tar.gz tar -xjvf ***.tar.bz2 tar [主選項輔選項] 文件或目錄 主選項是必須要有的&#xff0c;它告訴tar要做…

mysql免安裝5.7.17_mysql免安裝5.7.17數據庫配置

首先要有 mysql-5.7.10-winx64環境: mysql-5.7.10-winx64 win10(64位)配置環境變量&#xff1a;1、把mysql-5.7.10-winx64放到D盤&#xff0c;進入D\mysql-5.7.10-winx64\bin目錄&#xff0c;復制路徑&#xff0c;配置環境變量&#xff0c;在path后面添加D\mysql-5.7.10-winx6…

tidb數據庫_異構數據庫復制到TiDB

tidb數據庫This article is based on a talk given by Tianshuang Qin at TiDB DevCon 2020.本文基于Tianshuang Qin在 TiDB DevCon 2020 上的演講 。 When we convert from a standalone system to a distributed one, one of the challenges is migrating the database. We’…