Lightrun Answers được thiết kế để giảm bớt việc googling liên tục đi kèm với việc gỡ lỗi các thư viện bên thứ 3. Nó thu thập các liên kết đến tất cả các địa điểm mà bạn có thể đang xem trong khi tìm kiếm một lỗi nghiêm trọng
Và, nếu cuối cùng bạn vẫn gặp khó khăn, chúng tôi rất sẵn lòng nhận cuộc gọi để xem chúng tôi có thể trợ giúp như thế nào
Nói chuyện với chuyên gia về Lightrun Answers
Cách chính xác là sử dụng tsc và biên dịch các tệp TS của bạn thành JS rồi sử dụng chúng trong html
Nhưng nếu bạn thực sự, thực sự muốn thực hiện việc biên dịch trong trình duyệt, thì có một babel-standalone có thể được sử dụng chính xác cho việc đó. Chỉ cần nhớ rằng nó rất kém hiệu quả và không nên được sử dụng trên các trang web sản xuất
mục lục. html
Trong hơn 20 năm kể từ khi được tiêu chuẩn hóa, JavaScript đã đi được một chặng đường rất dài. Mặc dù vào năm 2020, JavaScript có thể được sử dụng trên các máy chủ, trong khoa học dữ liệu và thậm chí trên các thiết bị IoT, nhưng điều quan trọng cần nhớ là trường hợp sử dụng phổ biến nhất của nó. trình duyệt web
Các trang web được tạo thành từ các tài liệu HTML và/hoặc XML. Các tài liệu này là tĩnh, chúng không thay đổi. Mô hình đối tượng tài liệu (DOM) là một giao diện lập trình được các trình duyệt triển khai để làm cho các trang web tĩnh hoạt động. API DOM có thể được sử dụng để thay đổi cấu trúc, kiểu và nội dung tài liệu. API mạnh đến mức vô số khung giao diện người dùng (jQuery, React, Angular, v.v. ) đã được phát triển xung quanh nó để làm cho các trang web động dễ phát triển hơn
TypeScript là một siêu tập hợp đã nhập của JavaScript và nó cung cấp các định nghĩa loại cho API DOM. Các định nghĩa này luôn có sẵn trong bất kỳ dự án TypeScript mặc định nào. Trong số hơn 20.000 dòng định nghĩa trong lib. nhà thờ. d. ts, một cái nổi bật trong số những cái còn lại.
ts
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
constp = document.createElement("p");
// 3. Add the text content
p.textContent = "Hello, World!";
// 4. Append the p element to the div element
app?.appendChild(p);
1. Loại này là xương sống để thao tác DOM với TypeScript
Bạn có thể khám phá mã nguồn cho các định nghĩa kiểu DOM
Ví dụ cơ bản
Đưa ra một chỉ số đơn giản hóa. tệp html
html
<!DOCTYPEhtml>
<htmllang="en">
<head><title>TypeScript Dom Manipulation</title></head>
<body>
<divid="app"></div>
<!-- Assume index.js is the compiled output of index.ts -->
<scriptsrc="index.js"></script>
</body>
</html>
Hãy cùng khám phá một tập lệnh TypeScript thêm phần tử
ts
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
constp = document.createElement("p");
// 3. Add the text content
p.textContent = "Hello, World!";
// 4. Append the p element to the div element
app?.appendChild(p);
3 vào phần tử
ts
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
constp = document.createElement("p");
// 3. Add the text content
p.textContent = "Hello, World!";
// 4. Append the p element to the div element
app?.appendChild(p);
4
ts
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
constp = document.createElement("p");
// 3. Add the text content
p.textContent = "Hello, World!";
// 4. Append the p element to the div element
app?.appendChild(p);
Sau khi biên dịch và chạy chỉ mục. trang html, HTML kết quả sẽ là
html
<divid="app">
<p>Hello, World!</p>
</div>
Giao diện ts// 1. Select the div element using the id propertyconst app = document.getElementById("app");// 2. Create a new <p></p> element programmaticallyconst p = document.createElement("p");// 3. Add the text contentp.textContent = "Hello, World!";// 4. Append the p element to the div elementapp?.appendChild(p);5
Dòng đầu tiên của mã TypeScript sử dụng biến toàn cục
ts
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
constp = document.createElement("p");
// 3. Add the text content
p.textContent = "Hello, World!";
// 4. Append the p element to the div element
app?.appendChild(p);
6. Kiểm tra biến cho thấy nó được xác định bởi giao diện
ts
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
constp = document.createElement("p");
// 3. Add the text content
p.textContent = "Hello, World!";
// 4. Append the p element to the div element
app?.appendChild(p);
5 từ lib. nhà thờ. d. tập tin ts. Đoạn mã chứa lệnh gọi đến hai phương thức,
ts
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
constp = document.createElement("p");
// 3. Add the text content
p.textContent = "Hello, World!";
// 4. Append the p element to the div element
app?.appendChild(p);
8 và
ts
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
Truyền cho nó một chuỗi id phần tử và nó sẽ trả về
ts
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
constp = document.createElement("p");
// 3. Add the text content
p.textContent = "Hello, World!";
// 4. Append the p element to the div element
app?.appendChild(p);
1 hoặc
html
<divid="app">
<p>Hello, World!</p>
</div>
2. Phương pháp này giới thiệu một trong những loại quan trọng nhất,
ts
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
constp = document.createElement("p");
// 3. Add the text content
p.textContent = "Hello, World!";
// 4. Append the p element to the div element
app?.appendChild(p);
1. Nó đóng vai trò là giao diện cơ sở cho mọi giao diện phần tử khác. Ví dụ: biến
html
<divid="app">
<p>Hello, World!</p>
</div>
4 trong ví dụ mã có kiểu
html
<divid="app">
<p>Hello, World!</p>
</div>
5. Ngoài ra, hãy lưu ý rằng phương thức này có thể trả về
html
<divid="app">
<p>Hello, World!</p>
</div>
2. Điều này là do phương thức không thể chắc chắn trước thời gian chạy nếu nó thực sự có thể tìm thấy phần tử đã chỉ định hay không. Ở dòng cuối cùng của đoạn mã, toán tử xâu chuỗi tùy chọn mới được sử dụng để gọi
html
<divid="app">
<p>Hello, World!</p>
</div>
7
html<div id="app"> <p>Hello, World!</p></div>8
Định nghĩa cho phương pháp này là (Tôi đã bỏ qua định nghĩa không dùng nữa)
6. Trình thông dịch TypeScript đủ thông minh để suy ra tham số chung từ đối số này. Điều này có nghĩa là nhà phát triển không thực sự phải chỉ định tham số chung khi sử dụng phương thức; . Đó chính xác là những gì xảy ra; . Định nghĩa này là cách biến
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
constp = document.createElement("p");
// 3. Add the text content
p.textContent = "Hello, World!";
// 4. Append the p element to the div element
app?.appendChild(p);
1
Các phương pháp ts// 1. Select the div element using the id propertyconst app = document.getElementById("app");// 2. Create a new <p></p> element programmaticallyconst p = document.createElement("p");// 3. Add the text contentp.textContent = "Hello, World!";// 4. Append the p element to the div elementapp?.appendChild(p);01 và ts// 1. Select the div element using the id propertyconst app = document.getElementById("app");// 2. Create a new <p></p> element programmaticallyconst p = document.createElement("p");// 3. Add the text contentp.textContent = "Hello, World!";// 4. Append the p element to the div elementapp?.appendChild(p);02
Cả hai phương pháp này đều là những công cụ tuyệt vời để lấy danh sách các phần tử dom phù hợp với một tập hợp các ràng buộc độc đáo hơn. Chúng được định nghĩa trong lib. nhà thờ. d. ts như
ts
/**
* Returns the first element that is a descendant of node that matches selectors.
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
constp = document.createElement("p");
// 3. Add the text content
p.textContent = "Hello, World!";
// 4. Append the p element to the div element
app?.appendChild(p);
13. Mặc dù điều này có vẻ như là một sự khác biệt, hãy lưu ý rằng giao diện
ts
interfaceHTMLElementTagNameMap {
"a": HTMLAnchorElement;
"abbr": HTMLElement;
"address": HTMLElement;
"applet": HTMLAppletElement;
"area": HTMLAreaElement;
...
}
6 kéo dài từ
ts
interfaceHTMLElementTagNameMap {
"a": HTMLAnchorElement;
"abbr": HTMLElement;
"address": HTMLElement;
"applet": HTMLAppletElement;
"area": HTMLAreaElement;
...
}
2
Để xem các phương thức này hoạt động, hãy sửa đổi mã hiện có thành
ts
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
constp = document.createElement("p");
// 3. Add the text content
p.textContent = "Hello, World!";
// 4. Append the p element to the div element
app?.appendChild(p);
0
Quan tâm đến việc tìm hiểu thêm?
Phần tốt nhất về lib. nhà thờ. d. định nghĩa loại ts là chúng phản ánh các loại được chú thích trong trang web tài liệu của Mạng nhà phát triển Mozilla (MDN). Ví dụ: giao diện
ts
// 1. Select the div element using the id property
constapp = document.getElementById("app");
// 2. Create a new <p></p> element programmatically
constp = document.createElement("p");
// 3. Add the text content
p.textContent = "Hello, World!";
// 4. Append the p element to the div element
app?.appendChild(p);
1 được ghi lại bởi trang HTMLElement này trên MDN. Các trang này liệt kê tất cả các thuộc tính, phương thức có sẵn và đôi khi là cả các ví dụ. Một khía cạnh tuyệt vời khác của các trang là chúng cung cấp liên kết đến các tài liệu tiêu chuẩn tương ứng. Đây là liên kết đến
Làm cách nào để tải tệp HTML trong TypeScript?
TypeScript - Viết chương trình đầu tiên của bạn bằng HTML và TS .
Chạy lệnh tsc trên dấu nhắc lệnh để xác minh cài đặt TypeScript
Tải xuống và cài đặt mã VS IDE. .
Mở thư mục dự án trong mã VS
Tạo hai tệp (Một. .
trong kịch bản. .
Mở thiết bị đầu cuối mã VS chạy "tập lệnh tsc. .
Bây giờ bạn có thể sử dụng máy chủ Node để chạy tập lệnh