动态组件

通过使用保留的 <component> 组件,并对其 is 特性进行动态绑定,你可以在同一个挂载点动态切换多个组件:

  1. <template>
  2. <!--动态组件,此处的componentName为json中注册的usingComponents的key值-->
  3. <component is="{{componentName}}"></component>
  4. </template>
  5. <script>
  6. import {createComponent} from '@mpxjs/core'
  7. createComponent({
  8. data: {
  9. componentName: 'test'
  10. },
  11. ready () {
  12. setTimeout(() => {
  13. this.componentName = 'list'
  14. }, 3000)
  15. }
  16. })
  17. </script>
  18. <script type="application/json">
  19. {
  20. "usingComponents": {
  21. "list": "../components/list",
  22. "test": "../components/test"
  23. }
  24. }
  25. </script>