Node.js/nuxt/组件加载方式:修订间差异
来自康健生活
无编辑摘要 |
|||
| 第58行: | 第58行: | ||
] | ] | ||
})</syntaxhighlight> | })</syntaxhighlight> | ||
==== import ==== | |||
通过 Promise 的 resolve 机制 | |||
<syntaxhighlight lang="javascript"> | |||
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 | |||
} | |||
] | |||
}) | |||
</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: ()=>import("@/components/HelloWorld") | |||
} | |||
] | |||
})</syntaxhighlight> | |||
nuxt 抛出错误 | |||
{{Warning|render function or template not defined in component: anonymous}} | |||
== 组件加载 == | |||
== 动态加载组件 == | == 动态加载组件 == | ||
比如现在我有一个需求,需要动态加载画图组件 | 比如现在我有一个需求,需要动态加载画图组件 | ||
| 第84行: | 第127行: | ||
}, | }, | ||
</script></syntaxhighlight> | </script></syntaxhighlight> | ||
除此之外,还可以传参进行加载 | 除此之外,还可以传参进行加载 | ||
<syntaxhighlight lang="html5"> | <syntaxhighlight lang="html5"> | ||
<template> | <template> | ||
| 第127行: | 第155行: | ||
}, | }, | ||
</script></syntaxhighlight> | </script></syntaxhighlight> | ||
2020年9月28日 (一) 11:22的最新版本
前言
路由加载
路由懒加载和自动化注册没办法同时使用
常规加载
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')
}
]
})
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 抛出错误
组件加载
动态加载组件
比如现在我有一个需求,需要动态加载画图组件
<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>
除此之外,还可以传参进行加载
<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>
No categories