简体   繁体   English

当您不构建单页应用程序时,如何构建 Vue.js 单文件组件并导入它们?

[英]How do you build Vue.js single file components and import them when you're not building a single page application?

I am building a web application (it's ASP.NET Core 2 MVC but it shouldn't really make any difference here) that is NOT a single page application.我正在构建一个不是单页应用程序的 Web 应用程序(它是 ASP.NET Core 2 MVC,但在这里应该没有任何区别)。 I am using Vue.js quite happily to provide functionality on the front end via an Vue instance.我很高兴地使用 Vue.js 通过Vue实例在前端提供功能。 It's got to the point where I need to extract stuff into reusable components, which I can do along side the Vue instance like this:到了我需要将内容提取到可重用组件中的地步,我可以像这样与Vue实例一起做:

<script type="text/javascript" src="/js/dev/vue.js"></script>

<div id='app'>
    <my-component message="Hi there!" />
</div>

<script>
    Vue.component('my-component', {
        template: '<p>{{ message }}</p>',
        props: { message: String }
    });

    var app = new Vue({ el: '#app' });
</script>

I would love to be able to define my component as one of Vue's single file components (with a .vue extension) instead of declaring them like this but all the docs and tutorials I can find focus on single page applications and I am not sure how to modify that information to work for me.我希望能够将我的组件定义为 Vue 的单个文件组件之一(带有.vue扩展名),而不是像这样声明它们,但是我能找到的所有文档和教程都专注于单页应用程序,我不确定如何修改该信息为我工作。

I have had a go with the Vue CLI and worked out how to tell it to build a library rather than a SPA using it's bundled webpack config, but that builds a single UMD or CommonJS bundle file and I do not know how to make use of it!我已经使用了 Vue CLI 并研究了如何告诉它使用它捆绑的 webpack 配置来构建一个库而不是一个 SPA,但是它构建了一个单一的UMDCommonJS包文件,我不知道如何使用它!

I am unfamiliar with webpack, but I am willing to learn.我对webpack不熟悉,但我愿意学习。 I am currently using Gulp for other stuff in this project, so if I can get away with sticking with Gulp then great, but if I need to switch to webpack I will.我目前正在将 Gulp 用于这个项目中的其他东西,所以如果我能坚持使用 Gulp,那就太好了,但如果我需要切换到 webpack,我会的。

Being able to write your vue components in an SFC-like way is something that can be achieved by using Vue blocks.能够以类似 SFC 的方式编写 vue 组件是可以通过使用 Vue 块来实现的。 It allows you to write your multiple components in HTML as follows, without requiring build tools:它允许您按如下方式在 HTML 中编写多个组件,而无需构建工具:

<template component="component-name">
    <div>
        <!-- HTML Template here -->
    </div>
    <style scoped>
        /* Scoped styles here */
    </style>
    <script>
        // Vue component definition like in Single File Components.
        export default {
            data() {   
            },
            mounted() {
            },
            methods: {
             
            }
        }
    </script>
</template>

More info can be found at:可以在以下位置找到更多信息:

I have no example code because I actually develop SPAs only.我没有示例代码,因为我实际上只开发 SPA。 But you could try to install vue-cli (the old 2.x one, not the new 3.x) and then initialize an empty webpack template但是您可以尝试安装vue-cli (旧的 2.x 版本,而不是新的 3.x),然后初始化一个空的 webpack 模板

$ npm install -g vue-cli
$ vue init webpack my-project
$ cd my-project
$ npm install

Then you can create your SFCs in the src subfolder, then edit src/main.js然后你可以在src子文件夹中创建你的 SFC,然后编辑src/main.js

import component1 from './component1.vue'
import component2 from './component2.vue'

and then run npm run build which will create a dist subfolder with app.js and some CSS.然后运行npm run build这将创建一个带有app.js和一些 CSS 的dist子文件夹。 I am speculating theoretically here as I am using Webpack in different way - it should supposedly work.我在这里进行理论上的推测,因为我以不同的方式使用 Webpack - 它应该可以工作。

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

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