component 动态渲染组件

component接受两个属性

属性名说明
is接受一个计算属性作为动态渲染的标签名
shrinkcomponents接受 usingComponents 中的key值组成的字符串作为动态组件选择的范围
  1. <template>
  2. <view class="page-container">
  3. <view v-bind:tap="handleElementClick"><text>组件改变</text></view>
  4. <component :is="currentComp" shrinkcomponents="comp,comp1"></component>
  5. </view>
  6. </template>
  7. <script>
  8. class Index {
  9. data = {
  10. dataComp:'comp',
  11. }
  12. computed ={
  13. currentComp(){
  14. return this.dataComp === 'comp' ? 'comp1' : 'comp';
  15. }
  16. }
  17. methods = {
  18. handleElementClick (a,b) {
  19. console.log('handleElementClick',arguments,a,b)
  20. this.dataComp = (this.dataComp === 'comp' ? 'comp1' : 'comp')
  21. }
  22. }
  23. }
  24. export default new Index();
  25. </script>
  26. <script cml-type="json">
  27. {
  28. "base": {
  29. "usingComponents": {
  30. "comp":"./comp",
  31. "comp1":"./comp1",
  32. "comp2":"./comp2",
  33. "comp3":"./comp3",
  34. }
  35. },
  36. "wx": {
  37. "navigationBarTitleText": "index",
  38. "backgroundTextStyle": "dark",
  39. "backgroundColor": "#E2E2E2"
  40. },
  41. "alipay": {
  42. "defaultTitle": "index",
  43. "pullRefresh": false,
  44. "allowsBounceVertical": "YES",
  45. "titleBarColor": "#ffffff"
  46. },
  47. "baidu": {
  48. "navigationBarBackgroundColor": "#ffffff",
  49. "navigationBarTextStyle": "white",
  50. "navigationBarTitleText": "index",
  51. "backgroundColor": "#ffffff",
  52. "backgroundTextStyle": "dark",
  53. "enablePullDownRefresh": false,
  54. "onReachBottomDistance": 50
  55. }
  56. }
  57. </script>

Bug&Tips

注意 : 小程序端是通过条件判断来模拟component is的效果的,所以不要在component 标签上在在写c-if c-else c-else-if等条件判断