路由Vue Router基本用法

路由的作用是根据URL来匹配对应的组件,并且无刷新切换模板的内容。vue.js中,可使用Vue Router来管理路由,让构建单页应用更加简单。

一、效果

二、实现

1.项目中安装Vue Router插件

pnpm install vue-router@lastest

2.main.js

javascript 复制代码
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

const app = createApp(App);
app.use(router)
app.mount('#app')

3. scr/views/Home.vue

javascript 复制代码
<template>
    <div id="app">
        <h1>hello vite</h1>
    </div>
</template>

src/views/About.vue

javascript 复制代码
<template>
    <div id="app">
        <h1>hello vite about</h1>
    </div>
</template>

src/views/news.vue

javascript 复制代码
<template>
    <div id="app">
        <h1>hello vite news</h1>
    </div>
</template>

4.src/router/index.js:

javascript 复制代码
import { createRouter, createWebHistory } from 'vue-router'

import Home from '../views/Home.vue'
import News from '../views/News.vue'
import About from '../views/About.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: '首页',
      redirect: '/home'
    },
    {
      path: '/home',
      name: '首页',
      component: Home
    },
    {
      path: '/news',
      name: '新闻',
      component: News
    },
    {
      path: '/about',
      name: 'Contact',
      component: About
    }
  ]
})

export default router

​

5.scr/app.vue

javascript 复制代码
<template>
  <h1 class="title">Vue路由测试</h1>
  <div class="container">
    <RouterLink to="/home"  class="item"  >首页</RouterLink>
    <RouterLink to="/home"  class="item">产品</RouterLink>
    <RouterLink :to="{ path: '/news' }" class="item" >新闻</RouterLink>
    <RouterLink :to="{ name: 'Contact' }" class="item" >联系我们</RouterLink>
  </div>
  <div class="routerbox">
    <RouterView></RouterView>
  </div>
</template>

<script  setup >
import { RouterLink, RouterView } from 'vue-router'
import { ref, reactive, watch, onMounted } from 'vue';
</script>

 <style  scoped>
h1 {
  text-align: center;
}
.container {
  margin: 0 auto;
  width: 70%;
  display: flex;
  justify-content: space-evenly; 
} 
  .container>.item {
    flex: 0 0 auto;
    background-color: #df6565;
    color: #fff;
    text-decoration: none;
    width: 100px;
    height: 50px;
    border-radius: 10%;
    text-align: center;
    line-height: 50px;
    font-size: 20px;
  }

  .container>.item:hover {
    background-color: #bec9c2;
    color: #ECC980;
  } 
.routerbox {
  margin: 0 auto;
  margin-top: 20px;
  width: 60%;
  height: 200px;
  border: 1px solid #000;
  border-radius: 10px;
  padding: 20px;
} 
</style>
相关推荐
没资格抱怨23 分钟前
vue3中如何缓存路由组件
vue.js·spring·缓存
萌萌哒草头将军1 小时前
⚡⚡⚡Vite 被发现存在安全漏洞🕷,请及时升级到安全版本
前端·javascript·vue.js
会功夫的李白2 小时前
Electron + Vite + Vue 桌面应用模板
javascript·vue.js·electron·vite·模版
小兵张健2 小时前
运用 AI,看这一篇就够了(上)
前端·后端·cursor
不怕麻烦的鹿丸3 小时前
node.js判断在线图片链接是否是webp,并将其转格式后上传
前端·javascript·node.js
bst@微胖子3 小时前
阿里云平台Vue项目打包发布
vue.js·阿里云
vvilkim3 小时前
控制CSS中的继承:灵活管理样式传递
前端·css
南城巷陌3 小时前
Next.js中not-found.js触发方式详解
前端·next.js
No Silver Bullet3 小时前
React Native进阶(六十一): WebView 替代方案 react-native-webview 应用详解
javascript·react native·react.js
武昌库里写JAVA3 小时前
Vue3生态工具:Volar语言服务与Unplugin自动化导入配置
vue.js·spring boot·毕业设计·layui·课程设计