当前位置:网站首页>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
边栏推荐
- Flutter 启动白屏
- What you see and think in Microsoft
- RTP 发送 和接收 h265
- Raft protocol - process demonstration
- Typescript from getting started to mastering (XX) function generics
- Why do many programmers hate pair programming?
- Microcomputer principle and interface technology
- 小马智行进军前装量产,从自研域控制器入手?
- Simple code implementation of decision tree
- What have I learned from 200 machine learning tools?
猜你喜欢

Analysis of new retail o2o e-commerce model

Raft protocol - process demonstration

(2022杭电多校三)1011-Link is as bear(思维+线性基)

The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!

(codeforce547)C-Mike and Foam(质因子+容斥原理)

大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒

Sleuth+zipkin to track distributed service links

Connect with third-party QQ login

最新二开版漫画小说听书三合一完整源码/整合免签接口/搭建教程/带采集接口

小马智行进军前装量产,从自研域控制器入手?
随机推荐
What you see and think in Microsoft
Casbin入门
Solve the problem of garbled code when opening the project code in idea
消费行业数字化升级成 “刚需”,weiit 新零售 SaaS 为企业赋能!
HDU multi School Game 2 1011 DOS card
Connection broken by 'readtimc rt-443): read timed out (read timeout=l5)“)‘: /pac
Overestimated test driven development?
Since 2019, you must have stopped using this marketing strategy
《陌路曾相逢》夏陌沈疏晏_夏陌沈疏晏最新章节
(newcoder 15079)无关(容斥原理)
Typescript from entry to mastery (XVIII) joint type and type protection
Typescript from getting started to mastering (XX) function generics
Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
Connect with third-party QQ login
深入C语言(3)—— C的输入输出流
Some notes on uniapp
2. Variables and scope
MOS tube - rapid recovery application notes (II) [parameters and applications]
How to understand "page storage management scheme"
How to understand clock cycle and formula CPU execution time = number of CPU clock cycles / dominant frequency