简体   繁体   English

在 HTML 中一次性使用的大型 Vue 组件

[英]Large Vue component for single use in HTML

I have an editor vue component <editor></editor> that I want to use on only a few sites.我有一个编辑器 vue 组件<editor></editor>我只想在几个网站上使用。 This component is fairly big (400kb), so I don't want to register it globally.这个组件相当大(400kb),所以我不想全局注册它。 Is there a way to do this?有没有办法做到这一点? The site is a traditional server side app that uses normal blade.该站点是使用普通刀片的传统服务器端应用程序。

I'm thinking about manually importing it only on the sites that use it.我正在考虑仅在使用它的网站上手动导入它。

Just add the editor component like this:只需像这样添加editor组件:

...
import EditorComponent from 'path/to/editor/'

export default {
  components: {
    'editor': EditorComponent
  }
}
...

Then you can use the <editor></editor> without troubles in your HTML.然后你就可以在 HTML 中使用<editor></editor>了。

Chunk your javascript with Webpack, this way you only load what you need in the front end.用 Webpack 将你的 javascript 分块,这样你只在前端加载你需要的东西。 Example of code splitting 代码拆分示例

Then use different new Vue functions to render only what is needed然后使用不同的新 Vue 函数来仅渲染需要的内容

import Editor from './Editor.vue'
import OtherVueInstance from './OtherVueInstance.vue'
window.onload = function () {
    if (pageContainsEditor) {
        new Vue({
            render: h => h(Editor)
        }).$mount('#editor');   
    } else if (somethingElse) {
        new Vue({
            render: h => h(OtherVueInstance)
        }).$mount('#OtherVueInstance');   
    }
}


In the blade file:在刀片文件中:

<div id="editor"></div>
<div id="OtherVueInstance"></div>

Also you can just create a whole new Vue application, with its own seperate JS and CSS.你也可以创建一个全新的 Vue 应用程序,它有自己独立的 JS 和 CSS。 Then just load that on the pages where it is needed.然后只需将其加载到需要它的页面上。

Just use your Editor component as async component只需将您的编辑器组件用作异步组件

Instead of this:而不是这个:

import Editor from './editor'
export default {
  components: {
    Editor
  }
}

Do this:做这个:

export default {
  components: {
    Editor: () => import('./editor')  
  }
}

Webpack will split Editor component code into separate chunk and Vue will load it on demand only when it is actually needed... Webpack 会将 Editor 组件代码拆分为单独的块,Vue 仅在实际需要时才会按需加载...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM