当前位置:网站首页>Typescript from introduction to proficiency (XXIV) using import syntax
Typescript from introduction to proficiency (XXIV) using import syntax
2022-07-29 03:45:00 【Haha, APE】
Last section we learned Namespace
, In fact, our code can also be used import
Imported
modify component.ts file
Now get rid of components.ts
Inside namespace
Namespace code , It's written in ES6
Of export
Export mode . The code is as follows :
export class Header {
constructor(){
const cdiv = document.createElement("div")
cdiv.innerText ="This is Header"
document.body.appendChild(cdiv)
}
}
export class Content {
constructor(){
const cdiv = document.createElement("div")
cdiv.innerText ="This is Content"
document.body.appendChild(cdiv)
}
}
export class Footer {
constructor(){
const cdiv = document.createElement("div")
cdiv.innerText ="This is Footer"
document.body.appendChild(cdiv)
}
}
These three classes have been used export
It is derived that , That is to say, it can be used import
Introduced .
modify page.ts file
stay page.ts
file , Get rid of namespace
Code corresponding to namespace , And then use import
Syntax to import Header
、Content
and Footer
, The code is as follows :
import {
Header,Content,Footer } from "./Componets"
export class Page {
constructor(){
new Header()
new Content()
new Footer()
}
}
Now you can use tsc
Compile . Then you can see that the compiled code is define
At the beginning ( This is a amd
Standard code , Can't run directly in the browser , Can be in Node
Run directly in ), This kind of code cannot be run directly in the browser , Need other libraries (require.js
) Support for .
introduce require.js
Used a ready-made CDN Of require.js
, The address can be copied directly , And then use <script>
Tag to introduce
Require.js Of CDN Address : https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.js
Good copy URL After the address , Use <script>
Tags introduced
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.js"></script>
At this time, it can be parsed define
This kind of grammar . And then put page.ts Add default
keyword , If you don't add it, you can't quote it directly .
import {
Header,Content,Footer } from "./Componets"
export default class Page {
constructor(){
new Header()
new Content()
new Footer()
}
}
This time tsc
Compile , You will find that there are still problems . Because use export default
The grammar of this form , Need to be in html
In the use require
To introduce .
requir Method introduction
It can now be used directly in code require
了 , The specific code is as follows
<script>
require(["page"], function (page) {
new page.default();
});
</script>
In this way, after the configuration is completed, the page can display the content normally , But the configuration is quite troublesome
If you use webpack
or Parcel
It's much easier to deal with these packaging tools
边栏推荐
- 实例搭建Flask服务(简易版)
- Some notes on uniapp
- VISO fast rendering convolution block
- Realize multi-level linkage through recursion
- CUB_ Visualization of key points in 200 bird dataset
- 3.2 model saving and loading
- 1. Mx6u driver development-2-led driver
- EMD empirical mode decomposition
- Rdkit: introduce smiles code, smart code and Morgan fingerprint (ECFP)
- Introduction and comparison of unicast, multicast (target broadcast, multicast), broadcast, flooding, flooding
猜你喜欢
MOS tube - rapid recovery application notes (II) [parameters and applications]
数字孪生实际应用案例-智慧能源篇
Use of leak scanning (vulnerability scanning) tool burpsuite or burp Suite (with installation and installation package download of burpsuite+1.7.26)
Rdkit: introduce smiles code, smart code and Morgan fingerprint (ECFP)
向日葵远程控制为何采用BGP服务器?自动最优路线、跨运营商高速传输
MOS管 —— 快速复苏应用笔记(贰)[参数与应用]
Beijing post network research 2015 problem2
大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒
The latest second edition of comic novels, listening to books, three in one, complete source code / integrated visa free interface / building tutorials / with acquisition interface
Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
随机推荐
(newcoder 15079) irrelevant (inclusion exclusion principle)
Naive Bayes -- continuous data
Practical application cases of digital Twins - smart energy
(nowcoder22529c) diner (inclusion exclusion principle + permutation and combination)
Flask framework operation database_ Add, delete, modify and query statements
The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
Various minor problems of jupyter notebook, configuration environment, code completion, remote connection, etc
MOS管 —— 快速复苏应用笔记(贰)[参数与应用]
(codeforce547)C-Mike and Foam(质因子+容斥原理)
Typescript from entry to mastery (XVIII) joint type and type protection
Analysis of new retail o2o e-commerce model
What have I learned from 200 machine learning tools?
KNN method predicts pregnancy, KNN principle simple code
EMD 经验模态分解
@Configuration (proxybeanmethods = false) what's the use of setting this to false
How fast does it take to implement a super simple language
从2019 年开始,你一定停止使用了这个营销策略…
大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒
Understanding of p-type problems, NP problems, NPC problems, and NP hard problems in natural computing
(newcoder 15079)无关(容斥原理)