Node.js/nuxt/组件加载方式:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
无编辑摘要
 
(未显示同一用户的2个中间版本)
第58行: 第58行:
   ]
   ]
})</syntaxhighlight>
})</syntaxhighlight>
==== import ====
==== import ====
通过 Promise 的 resolve 机制
通过 Promise 的 resolve 机制
第94行: 第95行:
   ]
   ]
})</syntaxhighlight>
})</syntaxhighlight>
nuxt 抛出错误
{{Warning|render function or template not defined in component: anonymous}}


== 组件加载 ==
== 组件加载 ==
== 动态加载组件 ==
比如现在我有一个需求,需要动态加载画图组件
<syntaxhighlight lang="html5">
<template>
  <article class="user-attr-wrap">
    <component :is="componentInstance"
              :echartsConfig="echartsConfig"/>
    <!--<ly-echarts-pie :echartsConfig="echartsConfig" />-->
  </article>
</template>
<script>
// import EchartsPie from '@@/components/echarts/pie.vue';
  import { FetchProperty, FetchOtherProperty } from '@@/api/user.ts';
  /* const EchartsPie = ()=>import("@/components/echarts/pie.vue") */
  export default {
    /* components: {
      'ly-echarts-pie': EchartsPie
    }, */
    computed: {
      componentInstance () {
        return () => import("@/components/echarts/pie.vue")
      }
    },
</script></syntaxhighlight>
除此之外,还可以传参进行加载
<syntaxhighlight lang="html5">
<template>
  <article class="user-attr-wrap">
    <component :is="componentInstance"
              :echartsConfig="echartsConfig"/>
    <!--<ly-echarts-pie :echartsConfig="echartsConfig" />-->
  </article>
</template>
<script>
// import EchartsPie from '@@/components/echarts/pie.vue';
  import { FetchProperty, FetchOtherProperty } from '@@/api/user.ts';
  /* const EchartsPie = ()=>import("@/components/echarts/pie.vue") */
  export default {
    /* components: {
      'ly-echarts-pie': EchartsPie
    }, */
    computed: {
      componentInstance () {
        const tmp = 'pie'
        return () => import("@/components/echarts/" + tmp + ".vue")
      }
    },
</script></syntaxhighlight>