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

来自康健生活
Kangkk​(留言 | 贡献)
创建页面,内容为“== 前言 == == 路由加载 == 路由懒加载和自动化注册没办法同时使用 === 常规加载 == <syntaxhighlight lang="javascript"> import Vue from 'vue'…”
 
Kangkk​(留言 | 贡献)
无编辑摘要
 
(未显示同一用户的4个中间版本)
第2行: 第2行:
== 路由加载 ==
== 路由加载 ==
路由懒加载和自动化注册没办法同时使用
路由懒加载和自动化注册没办法同时使用
=== 常规加载 ==
== 常规加载 ==
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
import Vue from 'vue'
import Vue from 'vue'
第37行: 第37行:
   ]
   ]
})</syntaxhighlight>
})</syntaxhighlight>
nuxt 抛出错误
{{Warning|Cannot read property 'apply' of undefined}}
==== require.ensure ====
==== require.ensure ====
通过参数中的 webpackChunkName 将 js 分开打包(可能会有资源访问不到的情况)
通过参数中的 webpackChunkName 将 js 分开打包(可能会有资源访问不到的情况)
第54行: 第58行:
   ]
   ]
})</syntaxhighlight>
})</syntaxhighlight>
==== import ====
==== import ====
通过 Promise 的 resolve 机制
通过 Promise 的 resolve 机制
第90行: 第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>