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 机制 | ||
2020年9月28日 (一) 11:10的版本
前言
路由加载
路由懒加载和自动化注册没办法同时使用
常规加载
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
}
]
})
懒加载
require
异步方法(使用了resolve的异步机制)
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: resolve => (require(["@/components/HelloWorld"], resolve))
}
]
})
nuxt 抛出错误
require.ensure
通过参数中的 webpackChunkName 将 js 分开打包(可能会有资源访问不到的情况)
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')
}
]
})
动态加载组件
比如现在我有一个需求,需要动态加载画图组件
<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>
除此之外,还可以传参进行加载
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')
}
]
})
动态加载组件
比如现在我有一个需求,需要动态加载画图组件
<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>
import
通过 Promise 的 resolve 机制
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
const HelloWorld = ()=>import("@/components/HelloWorld")
export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
}
]
})
或者
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: ()=>import("@/components/HelloWorld")
}
]
})
nuxt 抛出错误
组件加载
No categories