동적 & 비동기 컴포넌트

카탈로그
  1. 1. keep-alive 동적 컴포넌트
  2. 2. Async Components

이 페이지는 컴포넌트 기초를 읽었다는 것을 가정합니다. 컴포넌트가 처음이라면 먼저 읽어야 합니다.

keep-alive 동적 컴포넌트

초기에는, 탭 인터페이스에서 컴포넌트들을 전환하기 위해서 is 특성을 사용했습니다. :

1
<component v-bind:is="currentTabComponent"></component>

컴포넌트들을 전환할 때 가끔 성능상의 이유로 상태를 유지하거나 재-렌더링을 피하길 원할 수 있습니다. 예를 들면, 탭 인터페이스를 약간 확장 할 때 :

게시물을 선택하고, Archive 탭으로 전환하고, 다시 Posts로 전환할 때, 선택했던 게시물이 더는 보지 않는 것 알아차릴 수 있습니다. 그 이유는 매번 새로운 탭을 선택할 때, Vue는 currentTabComponent의 새로운 인스턴스를 생성하기 때문입니다.

동적 컴포넌트를 재생성하는 것은 보통은 유용한 동작입니다. 하지만 이 경우에는, 탭 컴포넌트 인스턴스가 처음 생성될 때 캐시 되는 것을 선호합니다. 이런 문제를 해결하기 위해서, 동적 컴포넌트를 <keep-alive> 엘리먼트로 둘러쌀 수 있습니다. :

1
2
3
4
<!-- Inactive components will be cached! -->
<keep-alive>
<component v-bind:is="currentTabComponent"></component>
</keep-alive>

아래 결괴를 확인하세요. :

이제 Posts 탭은 (게시물이 선택된) 상태를 유지할 수 있고 심지어 렌더링하지 않습니다. 완성된 코드는 this fiddle에서 볼 수 있습니다.

``가 컴포넌트에서 `name` 옵션을 사용하거나 로컬/글로벌 등록을 하여 정의된 모든 것들로 전환되고 있는 컴포넌트들을 요구한다는 것을 유의하세요.

더 자세한 내용은 [API reference](../api/#keep-alive)에서 ` 확인해주세요.

Async Components

In large applications, we may need to divide the app into smaller chunks and only load a component from the server when it's needed. To make that easier, Vue allows you to define your component as a factory function that asynchronously resolves your component definition. Vue will only trigger the factory function when the component needs to be rendered and will cache the result for future re-renders. For example:
1
2
3
4
5
6
7
8
Vue.component("async-example", function(resolve, reject) {
setTimeout(function() {
// Pass the component definition to the resolve callback
resolve({
template: "<div>I am async!</div>"
});
}, 1000);
});

As you can see, the factory function receives a resolve callback, which should be called when you have retrieved your component definition from the server. You can also call reject(reason) to indicate the load has failed. The setTimeout here is for demonstration; how to retrieve the component is up to you. One recommended approach is to use async components together with Webpack’s code-splitting feature:

1
2
3
4
5
6
Vue.component("async-webpack-example", function(resolve) {
// This special require syntax will instruct Webpack to
// automatically split your built code into bundles which
// are loaded over Ajax requests.
require(["./my-async-component"], resolve);
});

You can also return a Promise in the factory function, so with Webpack 2 and ES2015 syntax you can do:

1
2
3
4
5
Vue.component(
"async-webpack-example",
// The `import` function returns a Promise.
() => import("./my-async-component")
);

When using local registration, you can also directly provide a function that returns a Promise:

1
2
3
4
5
6
new Vue({
// ...
components: {
"my-component": () => import("./my-async-component")
}
});

If you're a Browserify user that would like to use async components, its creator has unfortunately [made it clear](https://github.com/substack/node-browserify/issues/58#issuecomment-21978224) that async loading "is not something that Browserify will ever support." Officially, at least. The Browserify community has found [some workarounds](https://github.com/vuejs/vuejs.org/issues/620), which may be helpful for existing and complex applications. For all other scenarios, we recommend using Webpack for built-in, first-class async support.

### Handling Loading State

New in 2.3.0+

The async component factory can also return an object of the following format:

1
2
3
4
5
6
7
8
9
10
11
12
13
const AsyncComponent = () => ({
// The component to load (should be a Promise)
component: import("./MyComponent.vue"),
// A component to use while the async component is loading
loading: LoadingComponent,
// A component to use if the load fails
error: ErrorComponent,
// Delay before showing the loading component. Default: 200ms.
delay: 200,
// The error component will be displayed if a timeout is
// provided and exceeded. Default: Infinity.
timeout: 3000
});

Note that you must use Vue Router 2.4.0+ if you wish to use the above syntax for route components.