Router_编程式路由

安装路由的依赖

Add.vue

clike 复制代码
<script setup>

</script>

<template>
  <div>
    <h1> Add </h1>
  </div>
</template>

<style scoped>

</style>

Home.vue

clike 复制代码
<script setup>

</script>

<template>
  <div>
    <h1> Home </h1>
  </div>
</template>

<style scoped>

</style>

List.vue

clike 复制代码
<script setup>

</script>

<template>
  <div>
    <h1> List </h1>
  </div>
</template>

<style scoped>

</style>

Update.vue

clike 复制代码
<script setup>

</script>

<template>
  <div>
    <h1> Update </h1>
  </div>
</template>

<style scoped>

</style>

router.js

clike 复制代码
// 导入创建路由的相关方法
import Home from '../components/Home.vue'
import List from '../components/List.vue'
import Update from '../components/Update.vue'
import Add from '../components/Add.vue';
import { createRouter,createWebHashHistory} from 'vue-router'
const router = createRouter({
    history:createWebHashHistory(),
    routes:[
        {
            path:"/",
            component:Home
        },
        {
            path:"/home",
            component:Home
        },
        {
            path:"/list",
            component:List
        },
        {
            path:"/add",
            component:Add
        },
        {
            path:"/update",
            component:Update
        }
    ]




})




export default router

main.js

clike 复制代码
import { createApp } from 'vue'


import App from './App.vue'

import router from './routers/router.js'

const app = createApp(App)

app.use(router)
app.mount('#app')

App.vue

clike 复制代码
<script setup>
import {useRouter} from 'vue-router'
const router = useRouter()
function showList(){
  router.push("/list")
  router.push({path:"/list"})
  
}

</script>

<template>
  <div>
   
   <!-- 声明式路由 -->
    <router-link to="/home">home页</router-link><br>
    <router-link to="/list">list页</router-link><br>
    <router-link to="/update">update页</router-link><br>
    <router-link to="/add">add页</router-link><br>

    <!-- 编程式路由 -->
    <button @click="showList()">list</button>

    


    <hr>
    <router-view></router-view>
  </div>



</template>

<style scoped>

</style>

更改App.vue

clike 复制代码
<script setup>
import {useRouter} from 'vue-router'
import {ref} from 'vue'
const router = useRouter()

let myPath = ref("")

function goMyPage(){
  router.push(myPath.value)
}

</script>

<template>
  <div>
   
   <!-- 声明式路由 -->
    <router-link to="/home">home页</router-link><br>
    <router-link to="/list">list页</router-link><br>
    <router-link to="/update">update页</router-link><br>
    <router-link to="/add">add页</router-link><br>

    <!-- 编程式路由 -->
    <button @click="goMyPage()">Go</button><input type="text" v-model="myPath">

    


    <hr>
    <router-view></router-view>
  </div>



</template>

<style scoped>

</style>
相关推荐
wuhen_n2 小时前
网络请求在Vite层的代理与Mock:告别跨域和后端依赖
前端·javascript·vue.js
小彭努力中2 小时前
193.Vue3 + OpenLayers 实战:圆孔相机模型推算卫星拍摄区域
vue.js·数码相机·vue·openlayers·geojson
踩着两条虫9 小时前
VTJ.PRO 核心架构全公开!从设计稿到代码,揭秘AI智能体如何“听懂人话”
前端·vue.js·ai编程
蓝冰凌11 小时前
Vue 3 中 defineExpose 的行为【defineExpose暴露ref变量】详解:自动解包、响应性与实际使用
前端·javascript·vue.js
奔跑的呱呱牛11 小时前
generate-route-vue基于文件系统的 Vue Router 动态路由生成工具
前端·javascript·vue.js
sp42a11 小时前
在 NativeScript-Vue 中实现流畅的共享元素转场动画
vue.js·nativescript·app 开发
还是大剑师兰特13 小时前
Vue3 中 computed(计算属性)完整使用指南
前端·javascript·vue.js
孜孜不倦不忘初心13 小时前
Ant Design Vue 表格组件空数据统一处理 踩坑
前端·vue.js·ant design
csdn_aspnet13 小时前
查看 vite 与 vue 版本
javascript·vue.js