multiple pages in Vue.js CLI

后端 未结 5 670
温柔的废话
温柔的废话 2020-12-04 06:45

I\'m having trouble figuring out how to have multiple pages in a Vue CLI project. Right now I have my home page with a few components and I want to create another page but I

5条回答
  •  猫巷女王i
    2020-12-04 07:01

    This may not be relevant to the question, but bear with me, maybe my answer can help someone. I use webpack+vue, and I have figured out how to build multiple pages applications. Here my webpack.config.js:

    const path = require('path');
    const fs = require('fs')
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const CleanWebpackPlugin = require('clean-webpack-plugin');
    const VueLoaderPlugin = require('vue-loader/lib/plugin');
    const TerserPlugin = require('terser-webpack-plugin');
    const MiniCssExtractPlugin = require("mini-css-extract-plugin");
    const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
    
    module.exports = {
        entry: {
            app: './src/app.js',
            mgmt: ['./src/modules/mgmt/mgmt.js'],
            login: './src/modules/login/login.js'
        },
        output: {
            path: path.resolve(__dirname, 'dist'),
            // publicPath: '/ahezime/',
            filename: (chunkData) => {
                console.log('chuckData.chunk.name => ', chunkData.chunk.name)
                return chunkData.chunk.name === 'app' ? './[name].bundle.js' : './[name]/[name].bundle.js';
            }
        },
        optimization: {
            minimizer: [
                new TerserPlugin(),
                new OptimizeCSSAssetsPlugin({})
            ]
        },
        plugins: [
            new MiniCssExtractPlugin({
                filename: "[name].css",
                chunkFilename: "[id].css"
            }),
            new CleanWebpackPlugin(['dist']),
            new VueLoaderPlugin(),
            new HtmlWebpackPlugin({
                title: 'app',
                template: './src/app.html',
                // inject: false,
                chunks: ['app'],
                filename: './index.html'
            }),
            new HtmlWebpackPlugin({
                title: 'mgmt',
                template: './src/modules/mgmt/mgmt.html',
                // inject: false,
                chunks: ['mgmt'],
                filename: './mgmt/index.html'
            }),
            new HtmlWebpackPlugin({
                title: 'login',
                template: './src/modules/login/login.html',
                // inject: false,
                chunks: ['login'],
                filename: './login/index.html'
            })
        ],
        module: {
            rules: [
                {
                    test: /\.m?js$/,
                    exclude: /(node_modules|bower_components)/,
                    use: {
                        loader: 'babel-loader',
                        options: {
                            presets: ['@babel/preset-env'],
                            plugins: ['@babel/plugin-proposal-object-rest-spread']
                        }
                    }
                }
            ],
            rules: [
                {
                    test: /\.vue$/,
                    exclude: /node_modules/,
                    loader: 'vue-loader'
                },
                {
                    test: /\.css$/,
                    use: [
                        'vue-style-loader',
                        'style-loader',
                        'css-loader',
                        'sass-loader'
                    ]
                },
                {
                    test: /\.scss?$/,
                    use: ['style-loader', 'css-loader', 'sass-loader']
                },
                {
                    test: /\.(png|svg|jpg|gif)$/,
                    use: [
                        'file-loader'
                    ]
                },
                {
                    test: /\.(woff|woff2|eot|ttf|otf)$/,
                    use: [
                        'file-loader'
                    ]
                }
            ]
        }
    };
    

    And here's my directory structure:

    https://i.stack.imgur.com/uFvKx.png

    And you can jump pages:

    
    
    
    

提交回复
热议问题