安装
npm i vue-router@4.X.X
配置(在src下新建router/index.js,放入以下内容)
import { createRouter, createWebHashHistory } from "vue-router";
const router = createRouter({
history: createWebHashHistory(),
routes: [
{
path: "/",
name: "shou",
component: () => import("/src/views/home.vue"),
},
{
path: "/fen",
name: "fen",
component: () => import("/src/views/fenlei.vue"),
},
{
path: "/room/:id",
name: "room",
component: () => import("/src/views/room.vue"),
},
{
path: "/:pathMatch(.*)*",
name: "noPage",
component: () => import("../page/404.vue"),
},
],
});
export default router;
然后去main.js注册一下
import { createApp } from "vue";
import "./style.css";
import router from "/src/router/index.js";
import App from "./App.vue";
const app = createApp(App);
app.use(router);
app.mount("#app");
跳转
import { useRouter } from "vue-router";
const router = useRouter();
const xj = () => {
router.push({
name: "room",
query: {
id: 000000,
},
});
接收参数
import { useRoute } from "vue-router";
const route = useRoute();
console.log(route.query.id)
获取地址栏里面的动态参数,就是上面的:id,如localhost:8080/room/123
import { useRoute } from "vue-router";
const route = useRoute();
console.log("route", route.params);