Vue Router
## 安装 ``` npm install vue-router@4 ``` ## 快速使用 App.vue ``` <div id="app"> <router-link to="/">Index</router-link> <router-view></router-view> </div> ``` main.js ``` import { createApp } from 'vue' import App from './App.vue' import { createRouter, createWebHashHistory } from 'vue-router' import Index from "./Index.vue"; const routes = [ { path: '/', component: Index, meta :{ title: '首页' } }, ] const router = createRouter({ history: createWebHashHistory(), routes, }) router.afterEach((to, from) => { document.title = to.meta.title || 'CoolCool'; }); const app = createApp(App) app.use(router) app.mount('#app') ``` ## 动态路由匹配 路由: ``` { path: '/article/:id', component: Article, meta: { title: '文章' } }, ``` 调用: ``` $route.params.id this.$route.params.id ``` 监听路由参数变化: ``` created() { this.$watch( () => this.$route.params, (toParams, previousParams) => { if (toParams.id) { // xxx } } ) }, ```
创建时间:2024-03-17
|
最后修改:2024-04-08
|
©允许规范转载
酷酷番茄
首页
文章
友链