当前位置: 首页 > news >正文

前端学习——Vue (Day6)

路由进阶

路由的封装抽离

在这里插入图片描述

在这里插入图片描述

//main.jsimport Vue from 'vue'
import App from './App.vue'
import router from './router/index'// 路由的使用步骤 5 + 2
// 5个基础步骤
// 1. 下载 v3.6.5
// 2. 引入
// 3. 安装注册 Vue.use(Vue插件)
// 4. 创建路由对象
// 5. 注入到new Vue中,建立关联Vue.config.productionTip = falsenew Vue({render: h => h(App),router
}).$mount('#app')
//index.js
// 2个核心步骤
// 1. 建组件(views目录),配规则
// 2. 准备导航链接,配置路由出口(匹配的组件展示的位置) 
import Find from '@/views/Find'
import My from '../views/My'
import Friend from '../views/Friend'import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter) // VueRouter插件初始化const router = new VueRouter({// routes 路由规则们// route  一条路由规则 { path: 路径, component: 组件 }routes: [{ path: '/find', component: Find },{ path: '/my', component: My },{ path: '/friend', component: Friend },]
})export default router

声明式导航 - 导航链接

在这里插入图片描述

<template><div><div class="footer_wrap"><router-link to="/find">发现音乐</router-link><router-link to="/my">我的音乐</router-link><router-link to="/friend">朋友</router-link></div><div class="top"><!-- 路由出口 → 匹配的组件所展示的位置 --><router-view></router-view></div></div>
</template><script>
export default {};
</script><style>
body {margin: 0;padding: 0;
}
.footer_wrap {position: relative;left: 0;top: 0;display: flex;width: 100%;text-align: center;background-color: #333;color: #ccc;
}
.footer_wrap a {flex: 1;text-decoration: none;padding: 20px 0;line-height: 20px;background-color: #333;color: #ccc;border: 1px solid black;
}
.footer_wrap a.router-link-active {background-color: purple;
}
.footer_wrap a:hover {background-color: #555;
}
</style>

在这里插入图片描述
在这里插入图片描述

声明式导航 - 两个类名

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

声明式导航 - 跳转传参

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

动态路由参数可选符

在这里插入图片描述

Vue路由 - 重定向

在这里插入图片描述

import Home from '@/views/Home'
import Search from '@/views/Search'
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter) // VueRouter插件初始化// 创建了一个路由对象
const router = new VueRouter({routes: [{ path: '/', redirect: '/search' },{ path: '/home', component: Home },{ path: '/search/:words?', component: Search }]
})export default router

Vue路由 - 404

在这里插入图片描述

import Home from '@/views/Home'
import Search from '@/views/Search'
import Vue from 'vue'
import VueRouter from 'vue-router'
import NotFound from '@/views/NotFound.vue'
Vue.use(VueRouter) // VueRouter插件初始化// 创建了一个路由对象
const router = new VueRouter({routes: [{ path: '/', redirect: '/search' },{ path: '/home', component: Home },{ path: '/search/:words?', component: Search },{ path: '*', component: NotFound }]
})export default router
<template><div><h1>404 Not Found</h1></div>
</template><script>
export default {}
</script><style></style>

仅测试
在这里插入图片描述

Vue路由 - 模式设置

在这里插入图片描述

import Home from '@/views/Home'
import Search from '@/views/Search'
import Vue from 'vue'
import VueRouter from 'vue-router'
import NotFound from '@/views/NotFound.vue'
Vue.use(VueRouter) // VueRouter插件初始化// 创建了一个路由对象
const router = new VueRouter({
//一旦使用history模式,地址栏就没有#,需要后台配置访问规则mode:'history',routes: [{ path: '/', redirect: '/search' },{ path: '/home', component: Home },{ path: '/search/:words?', component: Search },{ path: '*', component: NotFound }]
})export default router

在这里插入图片描述

编程式导航 - 基本跳转

在这里插入图片描述
在这里插入图片描述

<template><div class="home"><div class="logo-box"></div><div class="search-box"><input type="text" /><button @click="goSearch">搜索一下</button></div><div class="hot-link">热门搜索:<router-link to="/search/黑马程序员">黑马程序员</router-link><router-link to="/search/前端培训">前端培训</router-link><router-link to="/search/如何成为前端大牛">如何成为前端大牛</router-link></div></div>
</template><script>
export default {name: "FindMusic",methods: {goSearch() {// 第一种写法// this.$router.push("./search");// 第二种写法this.$router.push({path: "./search",});},},
};
</script><style>
.logo-box {height: 150px;background: url("@/assets/logo.jpeg") no-repeat center;
}
.search-box {display: flex;justify-content: center;
}
.search-box input {width: 400px;height: 30px;line-height: 30px;border: 2px solid #c4c7ce;border-radius: 4px 0 0 4px;outline: none;
}
.search-box input:focus {border: 2px solid #ad2a26;
}
.search-box button {width: 100px;height: 36px;border: none;background-color: #ad2a26;color: #fff;position: relative;left: -2px;border-radius: 0 4px 4px 0;
}
.hot-link {width: 508px;height: 60px;line-height: 60px;margin: 0 auto;
}
.hot-link a {margin: 0 5px;
}
</style>

在这里插入图片描述
在这里插入图片描述

<template><div class="home"><div class="logo-box"></div><div class="search-box"><input type="text" /><button @click="goSearch">搜索一下</button></div><div class="hot-link">热门搜索:<router-link to="/search/黑马程序员">黑马程序员</router-link><router-link to="/search/前端培训">前端培训</router-link><router-link to="/search/如何成为前端大牛">如何成为前端大牛</router-link></div></div>
</template><script>
export default {name: "FindMusic",methods: {goSearch() {// 第一种写法// this.$router.push("./search");// 第二种写法// this.$router.push({//   path: "./search",// });// 第三种写法 (需要给路由器名字)this.$router.push({name:'search'})},},
};
</script><style>
.logo-box {height: 150px;background: url("@/assets/logo.jpeg") no-repeat center;
}
.search-box {display: flex;justify-content: center;
}
.search-box input {width: 400px;height: 30px;line-height: 30px;border: 2px solid #c4c7ce;border-radius: 4px 0 0 4px;outline: none;
}
.search-box input:focus {border: 2px solid #ad2a26;
}
.search-box button {width: 100px;height: 36px;border: none;background-color: #ad2a26;color: #fff;position: relative;left: -2px;border-radius: 0 4px 4px 0;
}
.hot-link {width: 508px;height: 60px;line-height: 60px;margin: 0 auto;
}
.hot-link a {margin: 0 5px;
}
</style>

在这里插入图片描述

编程式导航 - 路由传参

在这里插入图片描述
在这里插入图片描述

<template><div class="home"><div class="logo-box"></div><div class="search-box"><input v-model="inpValue" type="text" /><button @click="goSearch">搜索一下</button></div><div class="hot-link">热门搜索:<router-link to="/search/黑马程序员">黑马程序员</router-link><router-link to="/search/前端培训">前端培训</router-link><router-link to="/search/如何成为前端大牛">如何成为前端大牛</router-link></div></div>
</template><script>
export default {name: "FindMusic",data(){return {inpValue:''}},methods: {goSearch() {// 第一种写法this.$router.push(`./search?key=${this.inpValue}`);// 第二种写法// this.$router.push({//   path: "./search",// });// 第三种写法 (需要给路由器名字)// this.$router.push({//   name:'search'// })},},
};
</script><style>
.logo-box {height: 150px;background: url("@/assets/logo.jpeg") no-repeat center;
}
.search-box {display: flex;justify-content: center;
}
.search-box input {width: 400px;height: 30px;line-height: 30px;border: 2px solid #c4c7ce;border-radius: 4px 0 0 4px;outline: none;
}
.search-box input:focus {border: 2px solid #ad2a26;
}
.search-box button {width: 100px;height: 36px;border: none;background-color: #ad2a26;color: #fff;position: relative;left: -2px;border-radius: 0 4px 4px 0;
}
.hot-link {width: 508px;height: 60px;line-height: 60px;margin: 0 auto;
}
.hot-link a {margin: 0 5px;
}
</style>
<template><div class="search"><p>搜索关键字: {{ $route.query.key }} </p><p>搜索结果: </p><ul><li>.............</li><li>.............</li><li>.............</li><li>.............</li></ul></div>
</template><script>
export default {name: 'MyFriend',created () {// 在created中,获取路由参数// this.$route.query.参数名 获取查询参数// this.$route.params.参数名 获取动态路由参数console.log(this.$route.params.words);}
}
</script><style>
.search {width: 400px;height: 240px;padding: 0 20px;margin: 0 auto;border: 2px solid #c4c7ce;border-radius: 5px;
}
</style>

在这里插入图片描述

在这里插入图片描述

<template><div class="home"><div class="logo-box"></div><div class="search-box"><input v-model="inpValue" type="text" /><button @click="goSearch">搜索一下</button></div><div class="hot-link">热门搜索:<router-link to="/search/黑马程序员">黑马程序员</router-link><router-link to="/search/前端培训">前端培训</router-link><router-link to="/search/如何成为前端大牛">如何成为前端大牛</router-link></div></div>
</template><script>
export default {name: "FindMusic",data(){return {inpValue:''}},methods: {goSearch() {// 第一种写法// this.$router.push(`./search?key=${this.inpValue}`);// 第二种写法(完整写法:适合传多个参数)this.$router.push({path: "./search",query: {key:this.inpValue}});this.router.push({path: `/search/${this.inpValue}`})// 第三种写法 (需要给路由器名字)// this.$router.push({//   name:'search'// })},},
};
</script><style>
.logo-box {height: 150px;background: url("@/assets/logo.jpeg") no-repeat center;
}
.search-box {display: flex;justify-content: center;
}
.search-box input {width: 400px;height: 30px;line-height: 30px;border: 2px solid #c4c7ce;border-radius: 4px 0 0 4px;outline: none;
}
.search-box input:focus {border: 2px solid #ad2a26;
}
.search-box button {width: 100px;height: 36px;border: none;background-color: #ad2a26;color: #fff;position: relative;left: -2px;border-radius: 0 4px 4px 0;
}
.hot-link {width: 508px;height: 60px;line-height: 60px;margin: 0 auto;
}
.hot-link a {margin: 0 5px;
}
</style>
<template><div class="search"><p>搜索关键字: {{ $route.params.words }} </p><p>搜索结果: </p><ul><li>.............</li><li>.............</li><li>.............</li><li>.............</li></ul></div>
</template><script>
export default {name: 'MyFriend',created () {// 在created中,获取路由参数// this.$route.query.参数名 获取查询参数// this.$route.params.参数名 获取动态路由参数console.log(this.$route.params.words);}
}
</script><style>
.search {width: 400px;height: 240px;padding: 0 20px;margin: 0 auto;border: 2px solid #c4c7ce;border-radius: 5px;
}
</style>

在这里插入图片描述在这里插入图片描述

<template><div class="home"><div class="logo-box"></div><div class="search-box"><input v-model="inpValue" type="text" /><button @click="goSearch">搜索一下</button></div><div class="hot-link">热门搜索:<router-link to="/search/黑马程序员">黑马程序员</router-link><router-link to="/search/前端培训">前端培训</router-link><router-link to="/search/如何成为前端大牛">如何成为前端大牛</router-link></div></div>
</template><script>
export default {name: "FindMusic",data(){return {inpValue:''}},methods: {goSearch() {// 第一种写法// this.$router.push(`./search?key=${this.inpValue}`);// 第二种写法(完整写法:适合传多个参数)// this.$router.push({//   path: "./search",//   query: {//     key:this.inpValue//   }// });// this.router.push({//   path: `/search/${this.inpValue}`// })// 第三种写法 (需要给路由器名字)this.$router.push({name:'search',query: {key:this.inpValue,params:{words:this.inpValue}}})},},
};
</script><style>
.logo-box {height: 150px;background: url("@/assets/logo.jpeg") no-repeat center;
}
.search-box {display: flex;justify-content: center;
}
.search-box input {width: 400px;height: 30px;line-height: 30px;border: 2px solid #c4c7ce;border-radius: 4px 0 0 4px;outline: none;
}
.search-box input:focus {border: 2px solid #ad2a26;
}
.search-box button {width: 100px;height: 36px;border: none;background-color: #ad2a26;color: #fff;position: relative;left: -2px;border-radius: 0 4px 4px 0;
}
.hot-link {width: 508px;height: 60px;line-height: 60px;margin: 0 auto;
}
.hot-link a {margin: 0 5px;
}
</style>
<template><div class="search"><p>搜索关键字: {{ $route.params.words }} </p><p>搜索结果: </p><ul><li>.............</li><li>.............</li><li>.............</li><li>.............</li></ul></div>
</template><script>
export default {name: 'MyFriend',created () {// 在created中,获取路由参数// this.$route.query.参数名 获取查询参数// this.$route.params.参数名 获取动态路由参数console.log(this.$route.params.words);}
}
</script><style>
.search {width: 400px;height: 240px;padding: 0 20px;margin: 0 auto;border: 2px solid #c4c7ce;border-radius: 5px;
}
</style>

在这里插入图片描述
在这里插入图片描述

综合案例

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

自定义创建项目

在这里插入图片描述
shift+鼠标右键——在powershell中打开
vue create + 项目名
在这里插入图片描述
空格表示选中

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

ESlint 代码规范

在这里插入图片描述

代码规范错误

在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/100906.html

相关文章:

  • STM32MP157驱动开发——按键驱动(tasklet)
  • PostgreSQL构建时间
  • 2023-将jar包上传至阿里云maven私有仓库(云效制品仓库)
  • 嵌入式linux之OLED显示屏SPI驱动实现(SH1106,ssd1306)
  • 关于element ui 安装失败的问题解决方法、查看是否安装成功及如何引入
  • Selenium多浏览器处理
  • 浅谈 AI 大模型的崛起与未来展望:马斯克的 xAI 与中国产业发展
  • 【CesiumJS材质】(1)圆扩散
  • 实战-单例模式和创建生产者相结合
  • [SQL挖掘机] - 窗口函数介绍
  • 原生js实现锚点滚动顶部
  • 使用mysql接口遇到点问题
  • excel绘制折线图或者散点图
  • ChatGPT长文本对话输入方法
  • FFmpeg-swresample的更新
  • 回答网友 修改一个exe
  • 数据可视化 - 动态柱状图
  • 【JVM】JVM五大内存区域介绍
  • 自动驾驶感知系统--惯性导航定位系统
  • Netty简介
  • 基于TCP/IP对等模型对计算机网络知识点的整合
  • 【SQL应知应会】表分区(一)• Oracle版
  • PostgreSQL 常用空间处理函数
  • ubuntu初始化/修改root密码
  • 【Linux后端服务器开发】select多路转接IO服务器
  • 支持向量机(iris)
  • 24考研数据结构-第二章:线性表
  • Mybatis 动态 sql 是做什么的?都有哪些动态 sql?能简述动态 sql 的执行原理不?
  • 250_C++_typedef std::function<int(std::vector<int> vtBits)> fnChkSstStt
  • 无涯教程-jQuery - Transfer方法函数