是否可以构建独立的 Vue 应用程序? [英] Is it possible to build a Standalone Vue app?
问题描述
是否可以构建一个 Vue 应用程序,它在运行时不需要 Vue 作为依赖项?
Is it possible to build a Vue app, which does not require Vue as as a dependency at runtime?
即而不是浏览器必须像这样加载 vue.js 和应用程序
I.e. instead of the browser having to load the vue.js and the app like this
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://www.myxyzhost.com/my-app.js"></script>
有一个已经包含 vue.js 的 my-app.js,这样在网页中包含脚本就变成了一个简单的单行代码?
have a my-app.js which already includes vue.js, so that including the script in a web page becomes a simple one liner?
<script src="https://www.myxyzhost.com/my-app.js"></script>
推荐答案
所以,它实际上比我想象的要简单:
So, it was actually simpler than I thought:
我刚刚创建了一个单独的入口文件供 webpack 使用
I just created a separate entry file for webpack to consume like
my-custom-component-entry.js
并在那里猛烈抨击我需要的一切(vue 和相应的组件):
and slammed everything I needed in there (vue and the respective component):
// This is the actual solution, I was looking for.
import Vue from 'vue';
import MyCustomComponent from './components/my-custom-component.vue';
new Vue({
components: {
'my-custom-component': MyCustomComponent
}
}).$mount('#app'); // ya gotta have a #app element somewhere of course
然后我用 webpack 构建它(这里我不详细介绍).
And then I built it with webpack (I am not going into detail here).
现在一切都打包在文件中.没有单独的 Vue 运行时,客户可能需要安装.只需要:
Now everything is packed in on file. No separate Vue runtime, which a customer might need to install. All that's necessary is:
<script src="https://www.myxyzhost.com/dist/my-custom-component.js"></script>
是的,和往常一样,如果您没有为 Internet Explorer 添加适当的 polyfill,这会搞砸,但我认为这是理所当然的.
And yes, as always, this screws up, if you don't add the proper polyfills for internet explorer, but I take that for granted.
重要的是:对于现代浏览器来说,它是一种单内衬,使其更易于销售.这就是我所需要的.
The important thing: for modern browsers it's a one liner which makes it easier to sell. That was all I needed.
这篇关于是否可以构建独立的 Vue 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!