在 2.5.0 及以上版本中,如果你使用了單文件組件,那么基於模板的函數式組件可以這樣聲明:
<template functional> <div class="cell"> <div v-if="props.value" class="on"></div> <section v-else class="off"></section> </div> </template> <script> export default { props: ['value'] } </script>
我們標記組件為 functional
,這意味它是無狀態 (沒有響應式數據),無實例 (沒有 this
上下文)。
提示:函數式組件比普通組件性能更好,缺點是定義的數據沒有響應式。