一:router路由跳转
1.app.vue里直接使用<router-view/>组件(不需要import和components)
<template>
<div>
<router-view/>
<Footer/>
</div>
</template>
2.要设置哪个标签点了就会跳转就设置哪一个:其实就是套一个
<router-link :to="{path:'/itemMusic'}">XXX</router-link>
<router-link :to="{path:'/itemMusic'}">
<img :src="image.pic" />
</router-link>
3.router/index.js里设置页面路由
const routes = [
/*{
path: '/',
name: 'home',
component: HomeView
},*/
{
path: '/itemMusic', //路径
name: 'itemMusic', //名字
component: () => import('../views/itemMusic') //显示什么组件页面
}
]
二:axios获取后端数据
分三步走
//封装基础路径,便于上线服务器IP
import axios from 'axios';
let service=axios.create({
baseURL:"http://localhost:3000/",
timeout:3000
})
export default service
//逐个封装请求,这里演示的是请求轮播图
import service from "..";
// 获取首页轮播图的数据
export function getBanner(){
return service({
method:"GET",
url:"/banner?type=2",
})
}
//在这里使用,前端请求并且拿到数据
import { getBanner } from "@/request/api/home.js";
onMounted(async () => {
// axios.get('http://localhost:3000/banner?type=2').then((res)=>{
// console.log(res);
// state.images=res.data.banners
// console.log(state.images);
// })
let res = await getBanner();
state.images=res.data.banners
console.log(res);
});
return { state };
},