P29. vue3 js页面跳转 和替换当前位置

js页面跳转

App.vue





Page.vue





index.js

// 1. 定义路由组件.
// 也可以从其他文件导入
import Home from "../views/Home.vue";
import About from "../views/About.vue";
import User from "../views/User.vue";
import NotFound from "../views/NotFound.vue";
import News from "../views/News.vue";
import Parent from "../views/Parent.vue";
import Styleone from "../views/Styleone.vue";
import Styletwo from "../views/Styletwo.vue";
import Page from "../views/Page.vue";


import {createRouter, createWebHashHistory} from "vue-router";


// 2. 定义一些路由
// 每个路由都需要映射到一个组件。
// 我们后面再讨论嵌套路由。
const routes = [
    { path: '/', component: Home },
    { path: '/about', component: About },
    { path: '/user/:id', component: User },
    {
        name: "news",
        //path: '/news/:id(\\d+)',//正则匹配
        // path: '/news/:id+',//多个参数
        //path: '/news/:id+',//参数可有可无
        //path: '/news/:id*',//参数可重复叠加
        path: '/news/:id?',//参数不可重复叠加
        component: News
    },
    {
        path: '/:path(.*)',
        component: NotFound
    },//使用正则,匹配任意path
    {
        path: "/parent",
        component: Parent,
        children: [
            {
                path: "styleone",
                component: Styleone
            },
            {
                path: "styletwo",
                component: Styletwo
            }
        ]
    },
    {
        path: "/page",
        component: Page
    }
]

// 3. 创建路由实例并传递 `routes` 配置
// 你可以在这里输入更多的配置,但我们在这里
// 暂时保持简单
const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见,我们在这里使用 hash 模式。
    history: createWebHashHistory(),
    routes, // `routes: routes` 的缩写
})


export default router

P29. vue3 js页面跳转 和替换当前位置_第1张图片

  • 通过对象

P29. vue3 js页面跳转 和替换当前位置_第2张图片

  • 带参数

    
    

P29. vue3 js页面跳转 和替换当前位置_第3张图片

  • 带问号

P29. vue3 js页面跳转 和替换当前位置_第4张图片

替换当前位置

你可能感兴趣的:(vue-router)