router
main.js
import Vue from 'vue' import App from './App' import router from './router' Vue.config.productionTip = false // Vue.prototype.test = function () { // console.log('test'); // } // Vue.prototype.name = "coderwhy" new Vue({ el: '#app', router, render: h => h(App) }) // console.log(router); // const obj = { // name: 'why' // } // // Object.defineProperty(obj, 'age', 18)
app.vue
<template> <div id="app"> <h2>我是APP组件</h2> <!--<router-link to="/home" tag="button" replace active-class="active">首页</router-link>--> <!--<router-link to="/about" tag="button" replace active-class="active">关于</router-link>--> <!--<router-link to="/home" tag="button" replace>首页</router-link>--> <!--<router-link to="/about" tag="button" replace>关于</router-link>--> <!--<button @click="homeClick">首页</button>--> <!--<button @click="aboutClick">关于</button>--> <router-link to="/home">首页</router-link> <router-link to="/about">关于</router-link> <!--<router-link :to="'/user/'+userId">用户</router-link>--> <!--<!–<router-link to="/profile">档案</router-link>–>--> <!--<router-link :to="{path: '/profile', query: {name: 'why', age: 18, height: 1.88}}">--> <!--档案</router-link>--> <button @click="userClick">用户</button> <button @click="profileClick">档案</button> <keep-alive exclude="Profile,User"> <router-view/> </keep-alive> </div> </template> <script> import HomeNews from "./components/HomeNews"; import Home from "./components/Home"; export default { name: 'App', components: {Home, HomeNews}, data() { return { userId: 'zhangsan', imgURL: 'http://www.baidu.com/logo.png' } }, methods: { homeClick() { // 通过代码的方式修改路由 vue-router // push => pushState // this.$router.push('/home') this.$router.replace('/home') console.log('homeClick'); }, aboutClick() { // this.$router.push('/about') this.$router.replace('/about') console.log('aboutClick'); }, userClick() { this.$router.push('/user/' + this.userId) }, profileClick() { this.$router.push({ path: '/profile', query: { name: 'kobe', age: 19, height: 1.87 } }) } } } </script> <style> /*.router-link-active {*/ /*color: #f00;*/ /*}*/ .active { color: #f00; } </style>
router 下边的 index.js
// 配置路由相关的信息 import VueRouter from 'vue-router' import Vue from 'vue' // import Home from '../components/Home' // import About from '../components/About' // import User from '../components/User' const Home = () => import('../components/Home') const HomeNews = () => import('../components/HomeNews') const HomeMessage = () => import('../components/HomeMessage') const About = () => import('../components/About') const User = () => import('../components/User') const Profile = () => import('../components/Profile') // 1.通过Vue.use(插件), 安装插件 Vue.use(VueRouter) // 2.创建VueRouter对象 const routes = [ { path: '', // redirect重定向 redirect: '/home' }, { path: '/home', component: Home, meta: { title: '首页' }, children: [ // { // path: '', // redirect: 'news' // }, { path: 'news', component: HomeNews }, { path: 'message', component: HomeMessage } ] }, { path: '/about', component: About, meta: { title: '关于' }, beforeEnter: (to, from, next) => { // console.log('about beforeEnter'); next() } }, { path: '/user/:id', component: User, meta: { title: '用户' }, }, { path: '/profile', component: Profile, meta: { title: '档案' }, } ] const router = new VueRouter({ // 配置路由和组件之间的应用关系 routes, mode: 'history', linkActiveClass: 'active' }) // 前置守卫(guard) router.beforeEach((to, from, next) => { // 从from跳转到to document.title = to.matched[0].meta.title // console.log(to); // console.log('++++'); next() }) // 后置钩子(hook) router.afterEach((to, from) => { // console.log('----'); }) // 3.将router对象传入到Vue实例 export default router
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律