parent
c5b2de57e3
commit
3c336e67c5
13 changed files with 141 additions and 102 deletions
@ -0,0 +1,12 @@ |
||||
<template> |
||||
<nav> |
||||
<router-link to="/"><span>Home</span></router-link> |
||||
| |
||||
<router-link :to="{name:'about'}"><span>About</span></router-link> |
||||
</nav> |
||||
<router-view name="header"/> |
||||
<router-view/> |
||||
</template> |
||||
<script setup> |
||||
|
||||
</script> |
@ -0,0 +1,13 @@ |
||||
<template> |
||||
<h3>这是Header1</h3> |
||||
</template> |
||||
|
||||
<script> |
||||
export default { |
||||
name: "Header1" |
||||
} |
||||
</script> |
||||
|
||||
<style scoped> |
||||
|
||||
</style> |
@ -0,0 +1,13 @@ |
||||
<template> |
||||
<h3>这是Header2</h3> |
||||
</template> |
||||
|
||||
<script> |
||||
export default { |
||||
name: "Header2" |
||||
} |
||||
</script> |
||||
|
||||
<style scoped> |
||||
|
||||
</style> |
@ -0,0 +1,5 @@ |
||||
import {createApp} from "vue"; |
||||
import App from "./App.vue" |
||||
import router from './router' |
||||
|
||||
createApp(App).use(router).mount('#app') |
@ -0,0 +1,39 @@ |
||||
// 引入必要的router组件
|
||||
import {createRouter, createWebHistory} from 'vue-router' |
||||
// 引入视图
|
||||
|
||||
const Home = ()=>import('../views/HomeView.vue') |
||||
const About = () => import('../views/AboutView.vue') |
||||
const Header1 = () => import('../components/Header1.vue') |
||||
const Header2 = () => import('../components/Header2.vue') |
||||
|
||||
|
||||
// 创建路由,如果同一个路由中包含多个组件,则将component改名为components(加s),已对像的形式加添加多个组件,对象中的key是对象的名称, 可以在视图中使用此名称指定组件
|
||||
const routes = [ |
||||
{ |
||||
path: '/', |
||||
name: 'home', |
||||
components: { |
||||
header: Header1, |
||||
default: Home, |
||||
} |
||||
}, |
||||
{ |
||||
path: '/about', |
||||
name: 'about', |
||||
components: { |
||||
header: Header2, |
||||
default: About, |
||||
} |
||||
}, |
||||
] |
||||
|
||||
// 创建路由器,使用createRouter方法,传入history(路由类型)和 创建好的路由routes
|
||||
// 路由类型分为hash型和html5型,分别为createWebHashHistory和createWebHistory
|
||||
const router = createRouter({ |
||||
history: createWebHistory(), |
||||
routes |
||||
}) |
||||
|
||||
// 导出路由
|
||||
export default router |
@ -0,0 +1,12 @@ |
||||
<template> |
||||
<h3>这是About</h3> |
||||
</template> |
||||
|
||||
<script> |
||||
export default { |
||||
updated() { |
||||
console.log(this.$route.query) |
||||
console.log(this.$route.params) |
||||
} |
||||
} |
||||
</script> |
@ -0,0 +1,3 @@ |
||||
<template> |
||||
<h3>这是Home</h3> |
||||
</template> |
@ -0,0 +1,13 @@ |
||||
<template> |
||||
<h3>这是Header1</h3> |
||||
</template> |
||||
|
||||
<script> |
||||
export default { |
||||
name: "Header1" |
||||
} |
||||
</script> |
||||
|
||||
<style scoped> |
||||
|
||||
</style> |
@ -0,0 +1,13 @@ |
||||
<template> |
||||
<h3>这是Header2</h3> |
||||
</template> |
||||
|
||||
<script> |
||||
export default { |
||||
name: "Header2" |
||||
} |
||||
</script> |
||||
|
||||
<style scoped> |
||||
|
||||
</style> |
@ -1,18 +0,0 @@ |
||||
<template> |
||||
<h3>这是Header</h3> |
||||
<button @click="GoHome">GoHome</button> |
||||
</template> |
||||
|
||||
<script setup> |
||||
import {useRoute, useRouter} from "vue-router"; |
||||
|
||||
const route = useRoute() |
||||
const router = useRouter() |
||||
|
||||
function GoHome() { |
||||
router.push({ |
||||
name: 'home', |
||||
query: {...route.query} |
||||
}) |
||||
} |
||||
</script> |
@ -1,43 +0,0 @@ |
||||
<template> |
||||
<h3>这事Test</h3> |
||||
<button @click="goAboutParams">GotoAbout</button> |
||||
</template> |
||||
|
||||
<script> |
||||
export default { |
||||
name: "TestView", |
||||
methods: { |
||||
// 字符串路径 |
||||
goAbout: function () { |
||||
this.$router.push('/about') |
||||
}, |
||||
//对象形式的path |
||||
goAboutPath: function () { |
||||
this.$router.push({path: '/about'}) |
||||
}, |
||||
// 带params的push请求 |
||||
goAboutParams: function () { |
||||
this.$router.push({ |
||||
name: 'about-params', |
||||
params: { |
||||
id: 1, |
||||
name: 'roger' |
||||
} |
||||
}) |
||||
}, |
||||
// 带参数的router.push |
||||
goAboutQuery: function () { |
||||
this.$router.push({ |
||||
name: 'about', |
||||
// query: {...this.$route.query} // 映射query |
||||
query: {id: 1} |
||||
}) |
||||
} |
||||
} |
||||
} |
||||
</script> |
||||
|
||||
|
||||
<style scoped> |
||||
|
||||
</style> |
Loading…
Reference in new issue