JavaScript/vue.js/defineAsyncComponent:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
 
(未显示同一用户的4个中间版本)
第1行: 第1行:
== 标题文本 ==
== 标题文本 ==
[https://vuejs.org/guide/components/async Async Components]
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
import { defineAsyncComponent } from 'vue'
import { defineAsyncComponent } from 'vue'
第83行: 第85行:
const Select = defineAsyncComponent(async () => await import(/* selecct component by es url*/))
const Select = defineAsyncComponent(async () => await import(/* selecct component by es url*/))
</syntaxhighlight>
</syntaxhighlight>
==== package.json ====
<blockquote>
// ...
  "main": "./bundle/linking-kwy.cjs.js",
  "module": "./bundle/linking-kwy.es.js",
  "exports": {
    ".": {
      "import": "./bundle/linking-kwy.es.js",
      "require": "./bundle/linking-kwy.cjs.js"
    }
  },
// ...</blockquote>
=== CSS ===
=== CSS ===
在打包过程中可以通过组件 vite-plugin-css-injected-by-js 将 CSS 合并入 JS 文件中,使用起来也很简单
在打包过程中可以通过组件 vite-plugin-css-injected-by-js 将 CSS 合并入 JS 文件中,使用起来也很简单
第130行: 第145行:
</syntaxhighlight>
</syntaxhighlight>
{{Unknown|https://stackoverflow.com/a/77637766}}
{{Unknown|https://stackoverflow.com/a/77637766}}
== other ==
在思考远程组件加载这个方案的时候,[https://segmentfault.com/a/1190000044039990 Vue 加载远程组件的解决方案] 这篇文章给与我极大的帮助。在当今中国普遍 CTRL C 和 CRTL V 复制粘贴垃圾文的现象下,仍有愿意思考并实践的博主真的不多了。点赞。
在此方案下我的一个成功实现
<syntaxhighlight lang="javascript">
const AsyncFwdSearchCardTop = defineAsyncComponent({
  loader: async () => {
    console.log('Loading component...')
    const module = await import('https://www.unpkg.com/linking-kwy@0.0.8/bundle/linking-kwy.es.js')
    return module.default.Linking
  },
// ...
})</syntaxhighlight>