vue中組件占位 is屬性的使用


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="js/vue.js"></script>
</head>

 

<body>
  <div id="app">
    <!-- 配合is屬性使用的標簽只是一個占位符,不會渲染到頁面 -->
    <h2 is="custom-component"></h2>
    <!-- 
      select標簽中只能出現option標簽
      ul,ol標簽中只能出現li標簽
      thead,tbody標簽中只能出現tr標簽
      dl標簽中只能出現dt和dd標簽
     -->

 

    <select>
      <!-- 無效 -->
      <!-- <option-component></option-component> -->

 

      <!-- 需要使用option做占位符,is指定組件名稱 -->
      <option is="option-component"></option>
    </select>

 

    <ul>
      <!-- <li is="li-component"></li> -->
      <!-- <li-component></li-component> -->
    </ul>
  </div>

 

  <script>
    Vue.component('custom-component', {
      template: `
        <div>這是一個自定義組件</div>
      `
    })
    Vue.component('option-component', {
      template: `
        <option>這是一個option組件</option>
      `
    })
    Vue.component('li-component', {
      template: `
        <li>這是一個li組件</li>
      `
    })
    new Vue({
      el: '#app'
    })
  </script>
</body>

 

</html>
 
執行結果:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM