【问题标题】:How to create vue file from component markup如何从组件标记创建 vue 文件
【发布时间】:2019-07-04 09:49:02
【问题描述】:

我创建了一个 vue 组件 js 文件,它加载到我的页面中来做一件简单的事情:

function CategoryProductViewModel() {
    var props = {
        id:             Number, 
        attributes:     [Array, Object], 
        categories:     Array,
        ...
    }

    var data = function(){
        return {
           quantity:       1
        }
    };

    var computed = {};
    ...


    var methods = {};
    ...

    return {
        props: props,
        computed: computed,
        methods: methods,
        template: "#category-product"
    }
}

Vue.component('category-product', new CategoryProductViewModel());

然后我的页面中还有一些匹配的 html 标记:

<script type="text/x-template" id="category-product">
    <li>bunch of html here that's irrelevant</li>
</script>

这一切都很好。我有六个像这样构建的组件,它们可以完美地协同工作。

我可以/我如何将这些转换为 .vue 文件,然后将它们编译为单个 .js 文件以包含在我的页面中,而不是几个 html 模板和几个 .js 文件?

我知道我可能需要通过 npm 使用 vue-cli,但对于我的一生,我无法弄清楚如何将我写的内容翻译成 src/dist 文件夹结构中的 .vue 文件。

如果有帮助,我会将它们用作在 PHP 上运行的大型网站中的一种独立前端应用程序。

【问题讨论】:

    标签: vue.js vue-component


    【解决方案1】:

    首先创建你的 vue 组件。

    CategoryList.vue

    <template>
     The HTML you have but make sure it has one parent node.
    </template>
    
    <script>
    export default {
        props: {
            id:             Number, 
            attributes:     [Array, Object], 
            categories:     Array,
        },
    
       data(){
            return {
               quantity:       1
            };
        },
    
       computed: {
    
        },
    
    
       methods: {
    
        },
    }
    
    </script>
    
    

    现在要构建它,您将使用 webpack 之类的东西,

    这是我的设置

    package.json(你可以删除很多。我在手机上,打字很痛苦)

    
    
    {
      "private": true,
      "scripts": {
        "build": "webpack",
        "watch": "webpack --watch --progress",
        "test": "mocha-webpack --require ./resources/assets/js/tests/setup.js ./resources/assets/js/tests/**/*.spec.js"
      },
      "dependencies": {
        "@fortawesome/fontawesome-free": "^5.3.1",
        "autoprefixer": "^9.1.5",
        "axios": "^0.18",
        "normalize.css": "^8.0.0",
        "postcss-loader": "^3.0.0",
        "vue": "^2.5.17",
        "vuex": "^3.0.1"
      },
      "devDependencies": {
        "@babel/core": "^7.0.1",
        "@babel/polyfill": "^7.0.0",
        "@babel/preset-env": "^7.0.0",
        "@vue/test-utils": "^1.0.0-beta.25",
        "babel-eslint": "^9.0.0",
        "babel-loader": "^8.0.2",
        "browser-sync": "^2.24.7",
        "browser-sync-webpack-plugin": "^2.2.2",
        "copy-webpack-plugin": "^4.5.2",
        "css-loader": "^1.0.0",
        "eslint": "^5.6.0",
        "eslint-config-airbnb-base": "^13.1.0",
        "eslint-loader": "^2.1.0",
        "eslint-plugin-import": "^2.14.0",
        "eslint-plugin-node": "^7.0.1",
        "eslint-plugin-promise": "^4.0.1",
        "eslint-plugin-standard": "^4.0.0",
        "eslint-plugin-vue": "^4.7.1",
        "expect": "^23.6.0",
        "extract-text-webpack-plugin": "^4.0.0-beta.0",
        "file-loader": "^2.0.0",
        "ignore-styles": "^5.0.1",
        "jest": "^23.6.0",
        "jest-serializer-vue": "^2.0.2",
        "jsdom": "^12.0.0",
        "jsdom-global": "^3.0.2",
        "mini-css-extract-plugin": "^0.4.2",
        "mocha": "^5.2.0",
        "mocha-webpack": "^2.0.0-beta.0",
        "node-sass": "^4.9.3",
        "sass-loader": "^7.1.0",
        "svg-inline-loader": "^0.8.0",
        "url-loader": "^1.1.1",
        "vue-jest": "^2.6.0",
        "vue-loader": "^15.4.2",
        "vue-style-loader": "^4.1.2",
        "vue-template-compiler": "^2.5.17",
        "webpack": "^4.19.0",
        "webpack-cli": "^3.1.0"
      }
    }
    
    

    webpack.config.js

    
    const path = require('path');
    
    const CopyWebpackPlugin = require('copy-webpack-plugin');
    const { VueLoaderPlugin } = require('vue-loader');
    const MiniCssExtractPlugin = require('mini-css-extract-plugin');
    const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
    
    function resolve(dir) {
      return path.join(__dirname, './', dir);
    }
    
    module.exports = {
      mode: process.env.NODE_ENV,
      entry: ['./resources/assets/entry.js'],
      output: {
        path: resolve('./public/'),
        filename: 'js/app.js',
      },
      module: {
        rules: [
          {
            test: /\.(png|jp(e*)g)$/,
            loader: 'url-loader'
          },
          {
            test: /\.(svg)$/,
            loader: 'svg-inline-loader'
          },
          {
            test: /\.(css|sass|scss)$/,
            use: ['vue-style-loader', MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
          },
          {
            test: /\.js$/,
            exclude: /(node_modules)/,
            use: {
              loader: 'babel-loader',
            },
          },
          {
            test: /\.vue$/,
            use: ['vue-loader'],
          },
        ],
      },
      plugins: [
        new VueLoaderPlugin(),
        new MiniCssExtractPlugin({ filename: 'css/app.css' }),
        new CopyWebpackPlugin([
          {
            from: resolve('resources/assets/images'),
            to: resolve('public/images'),
            toType: 'dir',
          },
          {
            from: resolve('resources/assets/icons'),
            to: resolve('public/icons'),
            toType: 'dir',
          },
        ]),
      ],
    };
    
    

    资源/资产/entry.js

    
    require('@babel/polyfill');
    require('./js/app.js');
    require('./sass/app.scss');
    
    
    

    在你的 app.is 中就可以了

    import Vue from 'vue';
    import CategoryList from './CategoryList.vue';
    
    Vue.component('CategoryList', CategoryList);
    
    
    const APP = new Vue({
      el: '#app',
    });
    
    

    现在在您的 html 页面上,您可以执行 &lt;category-list&gt;&lt;/category-list&gt;

    那是在构建之后

    例如 公共/index.HTML

    <HTML>
    <head></head>
    <body>
        <main id="app">
            <category-list></category-list>
        </main>
        <script src="js/app.js" type="text/javascript"></script>
    </body>
    </HTML>
    

    【讨论】:

    • 如果这有帮助,请考虑将其标记为已回答
    • 对于一个简单的问题来说,这是一个非常复杂的答案。而且你没有必须使用 Webpack。
    • @JoshHabdas something like webpack 我想我的回答太复杂了,你没看懂。
    • 我想你可能误解了我的反馈。你也不必使用 like Webpack 的东西。捆绑器不是必需的,并且在这样的问题的上下文中,它增加了比解决 OP 所要求的更复杂的问题。不过还是谢谢你的恶作剧。
    • 现在我很好奇,你会如何将所说的 vue 文件编译成 js
    猜你喜欢
    • 1970-01-01
    • 2022-12-26
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 2019-02-13
    • 2021-08-27
    相关资源
    最近更新 更多