vue-router基本使用

运行环境:
操作系统: Win11
Node.js: v16.13.0
Vue.js: v3.4.21
原文链接:https://blog.knowlesea.top/post/3

安装 vue-router

npm install vue-router -s

创建路由对象

import {createRouter, createWebHistory} from "vue-router";
import HelloWorld from "@/components/HelloWorld.vue";

const router = createRouter({
    // history属性设置路由的工作模式
    history:createWebHistory(),
    // routes 数组指定路由规则
    // path标识路由地址
    // component标识要展示的组件
    routes:[
        {
            path:'/',
            component:HelloWorld
        }
    ]
})

// 向外共享路由对象
export default router

挂载路由对象

import './assets/main.css'

import { createApp } from 'vue'
import App from './App.vue'
import router from "@/routers/index.js";

// 使用use方法挂载路由模块
createApp(App).use(router).mount('#app')

设置路由页面

将路由页面放置的地方放置<router-view></router-view>

<script setup>
import TheWelcome from './components/TheWelcome.vue'
</script>

<template>
  <header>
    <img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" />

    <div class="wrapper">
      <router-view></router-view>
    </div>
  </header>

  <main>
    <TheWelcome />
  </main>
</template>

<style scoped>
header {
  line-height: 1.5;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }
}
</style>

说明

更改在默认创建的Vue项目上进行修改的

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值