当前位置:网站首页>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
边栏推荐
- Shopify seller: EDM marketing should be combined with salesmartly to easily get the conversion rate
- 【redis系列】字符串数据结构
- Why does the 20 bit address bus determine the storage space of 1MB
- three.js 第五十四用如何给shader传递结构体数组
- 实例搭建Flask服务(简易版)
- (2022 Hangdian multi school III) 1011 link is as bear (thinking + linear basis)
- Rdkit I: using rdkit to screen the structural characteristics of chemical small molecules
- (nowcoder22529C)dinner(容斥原理+排列组合)
- The list is not updated in real time when JS V-for data changes
- Practical application cases of digital Twins - smart energy
猜你喜欢

tron OUT_ OF_ ENERGY

RHCE的at,crontab的基本操作,chrony服务和对称加密和非对称加密

Shutter start white screen

Exness: dove resolution helped gold rebound, and the focus turned to U.S. GDP

Raft protocol - process demonstration

Understanding of p-type problems, NP problems, NPC problems, and NP hard problems in natural computing

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

VISO fast rendering convolution block

Violence recursion to dynamic programming 01 (robot movement)

Vs code must know and know 20 shortcut keys!
随机推荐
1. Header file - Comment - namespace - standard input / output stream
Tristate gate
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
Excel拼接数据库语句
5年多工作经验,工资给15k,要是你,你会接受吗?
Code speed optimization
Why do many programmers hate pair programming?
(2022 Hangdian multi school III) 1011 link is as bear (thinking + linear basis)
Practical application cases of digital Twins - smart energy
Batch production and upload sales NFT opensea eth polygon
Typescript from entry to mastery (XXI) generic types in classes
CUB_200鸟类数据集关键点可视化
Some notes on uniapp
(newcoder 15079)无关(容斥原理)
Flutter 启动白屏
向日葵资深产品总监技术分享:“国民远控”如何在AD域环境下应用
Inclusion exclusion principle
Division of data link layer, protocols used in data link layer and detailed introduction
Introduction to static routing and dynamic routing protocols OSPF and rip and static routing configuration commands
Ribbon principle analysis namedcontextfactory