Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

vuejs3 - How to build a multi pages application by vite2 and vue3?

I built a multi-page app with Vue CLI and Vue 2 by changing vue.config.js like below:

pages: {
    index: {
      entry: './src/pages/index/main.js',
      template: 'public/index.html',
      title: 'index page',
      chunks: ['chunk-vendors', 'chunk-common', 'index']
    },
    admin: {
      entry: './src/pages/admin/main.js',
      template: 'public/index.html',
      title: 'admin page',
      chunks: ['chunk-vendors', 'chunk-common', 'admin']
    }
},
...

But how do I build a multi-page app with Vite and Vue 3?

enter image description here

This is my Directory Structure. I edited the vite.config.js like this:

import vue from '@vitejs/plugin-vue'
const { resolve } = require('path')
/**
 * @type {import('vite').UserConfig}
 */
export default {
  plugins: [vue()],
  build:{
    rollupOptions:{
      input:{
        main:resolve(__dirname,'index.html'),
        admin:resolve(__dirname,'src/admin/index.html')
      }
    }
  }
}

But it returns errors when i build and i counld not open the admin page by localhost:3000/admin/index.html.

question from:https://stackoverflow.com/questions/65868976/how-to-build-a-multi-pages-application-by-vite2-and-vue3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could create two separate index.html for each entry point. For example, <projectRoot>/index.html imports <projectRoot>/main.js, while a nested <projectRoot>/admin/index.html imports <projectRoot>/admin/main.js.

Consider the following directory structure:

|-package.json
|-vite.config.js
|-index.html
|-main.js
|-admin/
|---index.html
|---main.js

You'd use this config to create the multi-page app:

// vite.config.js
const { resolve } = require('path')

module.exports = {
  build: {
    rollupOptions: {
      input: {
        main: resolve(__dirname, 'index.html'),
        admin: resolve(__dirname, 'admin/index.html')
      }
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...