非路由文件存放在 components 文件夹下
非路由组件包含:Header【首页、登录、注册、搜索】、Footer【首页、搜索页】
非路由组件的使用步骤
- 创建组件
- 引入
- 使用
<template>
<Header>Header>
<Footer>Footer>
template>
<script setup>
import Header from '@/components/Header'
import Footer from '@/components/Footer'
script>
<style lang="scss">style>
Home首页路由、Search搜索路由、Login登录路由、Refister注册路由
src / views
文件夹下1. 配置路由
src / router / index.js
import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [
// 重定向:运行项目时,立即定位到首页,而不是 /
{
path: '/',
redirect: '/home'
}, {
path: '/home',
component: () => import('@/views/Home')
}, {
path: '/search',
component: () => import('@/views/Search')
}, {
path: '/login',
component: () => import('@/views/Login')
}, {
path: '/register',
component: () => import('@/views/Register')
}
]
const router = createRouter({
history: createWebHashHistory(),
routes
})
export default router
2. 引入和注册
main.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router' // 引入路由
import store from './store'
// 重置样式
import 'normalize.css'
import '@/assets/styles/common.css'
createApp(App).use(store).use(router).mount('#app')
3. App.vue 中设置路由出口
<template>
<Header>Header>
<router-view>router-view>
<Footer>Footer>
template>
<script setup>
import Header from '@/components/Header'
import Footer from '@/components/Footer'
script>
<style lang="scss">style>
views
文件夹,非路由组件一般存放在components
文件夹$ toute、$ router
属性$router:一般用于在编程式导航进行路由跳转(push/replace)
$route:一般用于获取路由信息,如路径、query、params
to
属性,用于指明要跳转的位置<router-link to="/login">登录</router-link>
import { useRouter } from 'vue-router'
const router = useRouter()
// 搜索按钮的回调函数
const handleSearch = () => {
router.push('/search')
}
<Footer v-if="$route.meta.showFooter">Footer>
const routes = [
// 重定向:运行项目时,立即定位到首页,而不是 /
{
path: '/',
redirect: '/home'
}, {
path: '/home',
component: () => import('@/views/Home'),
meta: {
showFooter: true
}
}, {
path: '/search',
component: () => import('@/views/Search'),
meta: {
showFooter: true
}
}, {
path: '/login',
component: () => import('@/views/Login'),
meta: {
showFooter: false
}
}, {
path: '/register',
component: () => import('@/views/Register'),
meta: {
showFooter: false
}
}
]
占位
,如path:'/search/:id'
?key=value&key1=value1
的拼接形式```javascript
// 方法一:通过字符串直接传参
// params形式
router.push('/search/'+this.searchValue)
// query形式
router.push('/search?key='+this.searchValue)
// params+query
router.push('/search/'+this.searchValue+'?key='+this.searchValue.toUpperCase())
router.push(`/search/${this.searchValue}?k=${this.searchValue.toUpperCase()}`)
特别注意:路由携带params参数时,若使用的对象写法,则不能使用path配置项,必须使用name配置!
// 通过name命名路由
{
name: 'search',
path: '/search/:keyword',
component: () => import('@/views/Search'),
meta: {
showFooter: true
}
}
// 方法三:对象的写法(常用的),要对路由命名(name)
import { ref } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
// 搜索框的数据
const keyword = ref('')
// 搜索按钮的回调函数
const handleSearch = () => {
router.push({
name: 'search', //name要和路由中配置的name一致
params: {
keyword: keyword.value
},
query: {
key: keyword.value.toUpperCase()
}
})
}
router.push({
path: '/search',
params: {
keyword: keyword.value
},
query: {
key: keyword.value.toUpperCase()
}
})
router.push({
name: 'search',
query: {
key: keyword.value.toUpperCase()
}
})
问号?
,则表示该params参数可传可不传 {
...
path: '/search/:keyword?'
...
}
1. 布尔值的写法
{
name: 'search',
path: '/search/:keyword',
component: () => import('@/views/Search'),
meta: {
showFooter: true
},
props: true // 传递props
}
params
作为路由组件的属性,组件中可以通过defineProps接收params参数// search / index.vue
import { defineProps } from 'vue'
// 接收路由组件中的props
const props = defineProps({
keyword: {
type: String
}
})
console.log(props.keyword)
2. 对象写法
{
name: 'search',
path: '/search/:keyword',
component: () => import('@/views/Search'),
// 对象写法
props: {
name: 'dudu',
age: 18
}
}
// search页面
import { defineProps } from 'vue'
// 接收路由组件中的props
const props = defineProps({
name: {
type: String
},
age: {
type: Number
}
})
console.log(props)
3. 函数写法
params
和 query
参数传递给路由组件{
name: 'search',
path: '/search/:keyword',
component: () => import('@/views/Search'),
meta: {
showFooter: true
},
props: route => {
return {
keyword: route.params.keyword,
keyy: route.query.key
}
}
}