본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

인클루드 (Include)

원문 보기 위키 갱신

외부 HTML 파일을 페이지 안에 삽입할 수 있게 해 주는 컴포넌트예요. 재사용 가능한 조각(partial)을 한 파일로 모아두고 여러 곳에서 불러올 때 유용해요. _ | SlInclude, Shoelace 2.0부터 제공되고 있으며 stable 상태예요.

포함되는 파일은 window.fetch()로 비동기 요청돼요. 요청은 캐시되므로 같은 파일을 여러 번 포함해도 실제 요청은 한 번만 이루어져요. 포함된 콘텐츠는 `` 요소의 기본 슬롯에 삽입되어 light DOM을 통해 쉽게 접근하고 스타일링할 수 있어요.

출처: 문서

본문

| SlInclude — Since 2.0, stable

인클루드(Include)는 외부 HTML 파일을 페이지 안에 삽입할 수 있게 해 줘요.

포함되는 파일은 window.fetch()로 비동기 요청돼요. 요청은 캐시되므로 같은 파일을 여러 번 포함해도 실제 요청은 한 번만 이루어져요.

포함된 콘텐츠는 `` 요소의 기본 슬롯에 삽입되므로, light DOM을 통해 쉽게 접근하고 스타일링할 수 있어요.

<sl-include src="https://shoelace.style/assets/examples/include.html"></sl-include>
import SlInclude from '@shoelace-style/shoelace/dist/react/include';
const App = () => <SlInclude src="https://shoelace.style/assets/examples/include.html" />;

Examples

Listening for Events

포함 파일이 성공적으로 불러와지면 sl-load 이벤트가 발생해요. 이 이벤트를 들어서 인클루드에 커스텀 로딩 로직을 추가할 수 있어요.

요청이 실패하면 sl-error 이벤트가 발생해요. 이 경우 event.detail.status에 요청의 결과 HTTP 상태 코드(예: 404 Not Found)가 담겨요.

<sl-include src="https://shoelace.style/assets/examples/include.html"></sl-include>
<script>
  const include = document.querySelector('sl-include');
  include.addEventListener('sl-load', event => {
    if (event.eventPhase === Event.AT_TARGET) {
      console.log('Success');
    }
  });
  include.addEventListener('sl-error', event => {
    if (event.eventPhase === Event.AT_TARGET) {
      console.log('Error', event.detail.status);
    }
  });
</script>

Importing

autoloader나 전통적인 loader를 사용하고 있다면 이 부분은 건너뛰어도 돼요. 그 외에는 아래 스니펫 중 원하는 걸 골라 이 컴포넌트만 골라 가져올 수 있어요.

Script / Import / Bundler / React 방식이 준비되어 있어요.

CDN에서 <script> 태그로 이 컴포넌트를 가져오려면:

<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/include/include.js"></script>

CDN에서 JavaScript import로 가져오려면:

import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/include/include.js';

Bundler를 사용해 가져오려면:

import '@shoelace-style/shoelace/dist/components/include/include.js';

React 컴포넌트로 가져오려면:

import SlInclude from '@shoelace-style/shoelace/dist/react/include';

Properties

Name Description Reflects Type Default
src The location of the HTML file to include. Be sure you trust the content you are including as it will
be executed as code and can result in XSS attacks. string -
mode The fetch mode to use. 'cors' | 'no-cors' | 'same-origin' 'cors'
allowScripts
allow-scripts Allows included scripts to be executed. Be sure you trust the content you are including as it will
be executed as code and can result in XSS attacks. boolean false
updateComplete A read-only promise that resolves when the component has
finished updating.

속성과 프로퍼티에 대해 더 알아보세요.

Events

Name React Event Description Event Detail
sl-load onSlLoad Emitted when the included file is loaded. -
sl-error onSlError Emitted when the included file fails to load due to an error. { status: number }

이벤트에 대해 더 알아보세요.

더 알아보기 (Learn more)