components
<template> <div> <h2>我是关于</h2> <p>我是关于内容, 呵呵呵</p> </div> </template> <script> export default { name: "About", created() { console.log('About created'); }, destroyed() { console.log('About destroyed'); } } </script> <style scoped> </style>
home.vue
<template> <div> <h2>我是首页</h2> <p>我是首页内容, 哈哈哈</p> <router-link to="/home/news">新闻</router-link> <router-link to="/home/message">消息</router-link> <router-view></router-view> <h2>{{message}}</h2> </div> </template> <script> export default { name: "Home", data() { return { message: '你好啊', path: '/home/news' } }, created() { console.log('home created'); }, destroyed() { console.log('home destroyed'); }, // 这两个函数, 只有该组件被保持了状态使用了keep-alive时, 才是有效的 activated() { this.$router.push(this.path); console.log('activated'); }, deactivated() { console.log('deactivated'); }, beforeRouteLeave (to, from, next) { console.log(this.$route.path); this.path = this.$route.path; next() } } </script> <style scoped> </style>
HomeMessage
<template> <div> <ul> <li>消息1</li> <li>消息2</li> <li>消息3</li> <li>消息4</li> </ul> </div> </template> <script> export default { name: "HomeMessage" } </script> <style scoped> </style>
HomeNews
<template> <div> <ul> <li>新闻1</li> <li>新闻2</li> <li>新闻3</li> <li>新闻4</li> </ul> </div> </template> <script> export default { name: "HomeNews" } </script> <style scoped> </style>
Profile
<template> <div> <h2>我是Profile组件</h2> <h2>{{$route.query.name}}</h2> <h2>{{$route.query.age}}</h2> <h2>{{$route.query.height}}</h2> </div> </template> <script> export default { name: "Profile", created() { console.log('Profile created'); }, destroyed() { console.log('Profile destroyed'); } } </script> <style scoped> </style>
User
<template> <div> <h2>我是用户界面</h2> <p>我是用户的相关信息, 嘿嘿嘿</p> <h2>{{userId}}</h2> <h2>{{$route.params.id}}</h2> <button @click="btnClick">按钮</button> </div> </template> <script> export default { name: "User", computed: { userId() { return this.$route.params.id } }, created() { console.log('User created'); }, destroyed() { console.log('User destroyed'); }, methods: { btnClick() { // 所有的组件都继承自Vue类的原型 console.log(this.$router); console.log(this.$route); console.log(this.name); } } } </script> <style scoped> </style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律