Node.js/nuxt/组件加载方式:修订间差异
来自康健生活
| 第58行: | 第58行: | ||
] | ] | ||
})</syntaxhighlight> | })</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 () { | |||
return () => import("@/components/echarts/pie.vue") | |||
} | |||
}, | |||
</script></syntaxhighlight> | |||
除此之外,还可以传参进行加载 | |||
<syntaxhighlight lang="javascript"> | |||
import Vue from 'vue' | |||
import Router from 'vue-router' | |||
Vue.use(Router) | |||
export default new Router({ | |||
routes: [ | |||
{ | |||
path: '/', | |||
name: 'HelloWorld', | |||
component: resolve => require.ensure([], () => resolve(require('@/components/'+componentName)), 'webpackChunkName') | |||
} | |||
] | |||
})</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> | |||
==== import ==== | ==== import ==== | ||
通过 Promise 的 resolve 机制 | 通过 Promise 的 resolve 机制 | ||