项目效果展示
提示:本项目带全程视频讲解教程+源码
提示:本项目带全程视频讲解教程+源码
本节主要讲解项目安装vue-router路由,课程采用组合api的方式使用路由;
提示:以下是本篇文章正文内容
server: {
host: '0.0.0.0', //解决控制台 :Network: use --host to expose
port: 8080, //配置端口号
hmr: true, //开启热更新
open: true //启动在浏览器打开
}
代码如下(示例):
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
server: {
host: '0.0.0.0', //解决控制台 :Network: use --host to expose
port: 8080, //配置端口号
hmr: true, //开启热更新
open: true //启动在浏览器打开
},
resolve: {
alias: [
{
find: '@',
replacement: resolve(__dirname, 'src')
}
]
}
})
{
"compilerOptions": {
"target": "esnext",
"useDefineForClassFields": true,
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"jsx": "preserve",
"sourceMap": true,
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"lib": ["esnext", "dom"],
"skipLibCheck": true,
"baseUrl": ".",// paths 路径解析起点
"paths": {
"@/*": [ // 别名路径设置
"src/*"
]
}
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json" }]
}
///
declare module '*.vue' {
import type { DefineComponent } from 'vue'
const component: DefineComponent<{}, {}, any>
export default component
}
npm install vue-router@4
src目录下新建router文件夹,然后新建index.ts文件
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Layout from '@/components/HelloWorld.vue'
const routes: Array<RouteRecordRaw> = [
{
path: '/home',
name: 'home',
component: Layout
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router
import { createApp } from 'vue'
//import './style.css'
import App from './App.vue'
import router from './router/index'
const app = createApp(App);
app.use(router).mount('#app')
<template>
<router-view/>
</template>
<style lang="scss">
</style>