• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Vue3+vite多个html页面配置多入口文件,以和单个页面路由配置,宝妈级教学,值得一看

武飞扬头像
江.同学
帮助1

Vue3 Vite正式开始咯 多页面配置

第一步(创建项目)

我们打开编辑工具打开命令

1.输入创建命令

npm create vite@latest

2.为你的项目起个名字

  1.  
    PS I:\Web\练手\Test> npm create vite@latest
  2.  
    npx: installed 6 in 1.549s
  3.  
    ? Project name: » vite-project

3.选择框架vue,上下移动建,回车确认

  1.  
    npx: installed 6 in 1.549s
  2.  
    √ Project name: ... test
  3.  
    ? Select a framework: » - Use arrow-keys. Return to submit.
  4.  
    > vanilla
  5.  
    vue
  6.  
    react
  7.  
    preact
  8.  
    lit
  9.  
    svelte

3.这里我们选择vue,不适用typescript,上下移动建,回车确认

  1.  
    √ Project name: ... test
  2.  
    Select a framework: » vue
  3.  
    ? Select a variant: » - Use arrow-keys. Return to submit.
  4.  
    > vue
  5.  
    vue-ts

4.我们得到一个全新项目

学新通

5.接下来我们还需要初始化项目 

  1.  
    cd ./项目名
  2.  
     
  3.  
    npm install

6.接下里我们启动项目,下面就是项目在浏览器访问的路径了,打开浏览器访问即可

npm run dev

学新通

现在项目创建完成了,我们开始正式的配置,以上为小白准备

第二步(多页面配置)

        1.把项目跟目录下面src目录的文件全部删除,删除项目根目录下的index.html

        3.在项目根目录下创建一个index.html

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8" />
  5.  
    <link rel="icon" href="/favicon.ico" />
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7.  
    <title>Vite App</title>
  8.  
    </head>
  9.  
    <body>
  10.  
    <div id="app"></div>
  11.  
    <script type="module" src="/src/index/index_main.js"></script>
  12.  
    </body>
  13.  
    </html>

        3.在项目根目录下创建一个about.html,这里引入的js文件

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <title>Title</title>
  6.  
    </head>
  7.  
    <body>
  8.  
    <div id="app"></div>
  9.  
    <script type="module" src="/src/about/about-main.js"></script>
  10.  
    </body>
  11.  
    </html>

        4.在项目根目录下src创建index目录,在里面创建Index.vue以及index_main.js文件

                Index.vue

  1.  
    <template>
  2.  
    <div>
  3.  
    index 页面
  4.  
    </div>
  5.  
    </template>
  6.  
     
  7.  
    <script setup>
  8.  
     
  9.  
    </script>
  10.  
     
  11.  
    <style scoped>
  12.  
     
  13.  
    </style>

       index_main.js

  1.  
    import { createApp } from 'vue'
  2.  
    import App from './Index.vue'
  3.  
     
  4.  
     
  5.  
    createApp(App).mount('#app')

        5.在项目根目录下src创建about目录,在里面创建About.html以及about_main.js文件

                About.html

  1.  
    <template>
  2.  
    about 页面
  3.  
    </template>
  4.  
     
  5.  
    <script>
  6.  
     
  7.  
    </script>
  8.  
     
  9.  
    <style scoped>
  10.  
     
  11.  
    </style>

                About_main.js

  1.  
    import {createApp} from 'vue'
  2.  
    import App from './About.vue'
  3.  
    createApp(App).use(router).mount('#app')

        6.页面以及创建完成我们只需要在vite.config.js配置多文件入口

  1.  
    import {defineConfig} from 'vite'
  2.  
    import vue from '@vitejs/plugin-vue'
  3.  
    import path from 'path';
  4.  
    export default defineConfig({
  5.  
    plugins: [vue()],
  6.  
    build: {
  7.  
    rollupOptions: {
  8.  
    input: {
  9.  
    index: path.resolve(__dirname, 'index.html'),
  10.  
    about: path.resolve(__dirname, 'about.html'),
  11.  
    }, output: {
  12.  
    chunkFileNames: 'static/js/[name]-[hash].js',
  13.  
    entryFileNames: "static/js/[name]-[hash].js",
  14.  
    assetFileNames: "static/[ext]/name-[hash].[ext]"
  15.  
    }
  16.  
    },
  17.  
    }
  18.  
    })
学新通

在命令中连按两次 Ctrl C,然后 npm run dev 重新启动项目,各位端口的配置可能与我不同,输入自己配置的端口 或者显示的端口就好了

http://localhost:3002/   index.html 页面

http://localhost:3002/About.html  About.html 页面

第三步(页面路由配置

        1.我依然打开命令 输入 进行安装路由配置

npm install vue-router -s

        2.我们只是在About.html配置路由,我们打开about目录创建views目录下面创建01.vue 02.vue组件

              01.vue

  1.  
    <template>
  2.  
    about 01组件
  3.  
    </template>
  4.  
     
  5.  
    <script>
  6.  
    export default {
  7.  
    name: "01"
  8.  
    }
  9.  
    </script>
  10.  
     
  11.  
    <style scoped>
  12.  
     
  13.  
    </style>

        02.vue

  1.  
    <template>
  2.  
    about 02组件
  3.  
    </template>
  4.  
     
  5.  
    <script>
  6.  
    export default {
  7.  
    name: "02"
  8.  
    }
  9.  
    </script>
  10.  
     
  11.  
    <style scoped>
  12.  
     
  13.  
    </style>

        3.我们打开about目录创建router目录下在创建index.js

  1.  
    import {createRouter, createWebHashHistory} from 'vue-router'
  2.  
    const a1=()=>import('../views/01.vue')
  3.  
    const a2=()=>import('../views/02.vue')
  4.  
    const routes = [{
  5.  
    path:'',
  6.  
    name:'a1',
  7.  
    component:a1
  8.  
    },{
  9.  
    path:'/a2',
  10.  
    name:'a2',
  11.  
    component:a2
  12.  
    },]
  13.  
     
  14.  
    const router = createRouter({
  15.  
    history: createWebHashHistory(),
  16.  
    routes
  17.  
    })
  18.  
    export default router
学新通

        4.我们打开about目录下的about_main.js引入router目录下的idnex.js

  1.  
    import {createApp} from 'vue'
  2.  
    import App from './About.vue'
  3.  
    import router from './router'
  4.  
    createApp(App).use(router).mount('#app')

        5.我们最后一步只需要在about目录下打开About.vue 

  1.  
    <template>
  2.  
    about 页面
  3.  
    <div>
  4.  
    <router-link to="/">组件1</router-link>
  5.  
    ---
  6.  
    <router-link to="/a2">组件2</router-link>
  7.  
    <router-view></router-view>
  8.  
    </div>
  9.  
    </template>
  10.  
     
  11.  
    <script>
  12.  
     
  13.  
    </script>
  14.  
     
  15.  
    <style scoped>
  16.  
    .router-link-active {
  17.  
    background-color: #42b983;
  18.  
    }
  19.  
    </style>
学新通

        我们直接打开浏览器,输入About.html 地址就能看到路由效果

如果小伙伴嘛在web编程中遇见的困难都可以积极留言,我会尽量为你解决,大概每天中午1左右在线,其余时间处理更新文章之外很少在线

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgekkii
系列文章
更多 icon
同类精品
更多 icon
继续加载