学习路由的params参数

new_branch1
roger_home_pc 2 years ago
parent 1ef0699836
commit 8ec3601a2b
  1. 32
      20_脚手架/vue_code/31.src_路由的params参数/App.vue
  2. BIN
      20_脚手架/vue_code/31.src_路由的params参数/assets/logo.png
  3. 11
      20_脚手架/vue_code/31.src_路由的params参数/components/Banner.vue
  4. 23
      20_脚手架/vue_code/31.src_路由的params参数/main.js
  5. 21
      20_脚手架/vue_code/31.src_路由的params参数/pages/About.vue
  6. 16
      20_脚手架/vue_code/31.src_路由的params参数/pages/Detail.vue
  7. 36
      20_脚手架/vue_code/31.src_路由的params参数/pages/Home.vue
  8. 45
      20_脚手架/vue_code/31.src_路由的params参数/pages/Message.vue
  9. 15
      20_脚手架/vue_code/31.src_路由的params参数/pages/News.vue
  10. 40
      20_脚手架/vue_code/31.src_路由的params参数/router/index.js
  11. 4
      20_脚手架/vue_code/src/pages/Detail.vue
  12. 4
      20_脚手架/vue_code/src/pages/Message.vue
  13. 2
      20_脚手架/vue_code/src/router/index.js

@ -0,0 +1,32 @@
<template>
<div>
<div class="row">
<Banner/>
</div>
<div class="row">
<div class="col-xs-2 col-xs-offset-2">
<div class="list-group">
<router-link class="list-group-item" active-class="active" :to="{name:'guanyu'}">About</router-link>
<router-link class="list-group-item" active-class="active" to="/home">Home</router-link>
<!-- <a class="list-group-item active" href="./about.html">About</a>-->
<!-- <a class="list-group-item" href="./home.html">Home</a>-->
</div>
</div>
<div class="col-xs-6">
<div class="panel">
<div class="panel-body">
<router-view></router-view>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import Banner from "@/components/Banner.vue";
export default {
name: "App",
components: {Banner}
}
</script>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

@ -0,0 +1,11 @@
<template>
<div class="col-xs-offset-2 col-xs-8">
<div class="page-header"><h2>Vue Router Demo</h2></div>
</div>
</template>
<script>
export default {
name: "Banner"
}
</script>

@ -0,0 +1,23 @@
// 引入Vue
import Vue from "vue";
// 引入App
import App from "./App";
// 引入VueRouter
import VueRouter from "vue-router";
// 引入router
import router from "@/router";
// 设置Vue
Vue.config.productionTip = false
// 使用插件
Vue.use(VueRouter)
// 实例化Vue
new Vue({
components: {
App
},
render: h => h(App),
router: router,
}).$mount('#app')

@ -0,0 +1,21 @@
<template>
<h2>我是About的内容</h2>
</template>
<script>
export default {
name: "About",
mounted() {
console.log('About组件挂着完成', this)
window.aboutRoute = this.$route
window.aboutRouter = this.$router
},
beforeDestroy() {
console.log('About组件即将销毁')
}
}
</script>
<style scoped>
</style>

@ -0,0 +1,16 @@
<template>
<ul>
<li>消息编号{{this.$route.params.id}}</li>
<li>消息标题{{this.$route.params.title}}</li>
</ul>
</template>
<script>
export default {
name: "Detail",
mounted() {
console.log(this.$route)
}
}
</script>

@ -0,0 +1,36 @@
<template>
<div>
<h2>Home组件内容</h2>
<div>
<ul class="nav nav-tabs">
<li>
<router-link class="list-group-item" active-class="active" to="/home/news">News</router-link>
</li>
<li>
<router-link class="list-group-item" active-class="active" to="/home/message">Message</router-link>
</li>
</ul>
<ul>
<router-view></router-view>
</ul>
</div>
</div>
</template>
<script>
export default {
name: "Home",
mounted() {
console.log('Home组件挂着完成', this)
window.homeRoute = this.$route
window.homeRouter = this.$router
},
beforeDestroy() {
console.log('Home组件即将销毁')
}
}
</script>
<style scoped>
</style>

@ -0,0 +1,45 @@
<template>
<div>
<ul>
<li v-for="m in messageList" :key="m.id">
<!-- to的普通写法-->
<!-- <router-link :to="`/home/message/detail/${m.id}/${m.title}`">{{ m.title }}</router-link>-->
<!-- to的对象写法-->
<router-link :to="{
// path: '/home/message/detail',
name: 'xiangqing',
params: {
id: m.id,
title: m.title
}
}">
{{ m.title }}
</router-link>
</li>
</ul>
<hr>
<router-view></router-view>
</div>
</template>
<script>
import detail from "@/pages/Detail.vue";
export default {
name: "Message",
computed: {
detail() {
return detail
}
},
data() {
return {
messageList: [
{id: '001', title: '消息001'},
{id: '002', title: '消息002'},
{id: '003', title: '消息003'}
]
}
}
}
</script>

@ -0,0 +1,15 @@
<template>
<div>
<ul>
<li>news001</li>
<li>news002</li>
<li>news003</li>
</ul>
</div>
</template>
<script>
export default {
name: "News"
}
</script>

@ -0,0 +1,40 @@
import VueRouter from "vue-router";
// 引入组件
import About from "@/pages/About.vue"
import Home from "@/pages/Home.vue"
import News from "@/pages/News.vue"
import Message from "@/pages/Message.vue";
import Detail from "@/pages/Detail.vue";
// 创建一个路由器
export default new VueRouter({
routes: [
{
name: 'guanyu',
path: '/about',
component: About
},
{
path: '/home',
component: Home,
children: [
{
path: 'news',
component: News,
},
{
path: 'message',
component: Message,
children: [
{
name: 'xiangqing',
path: 'detail/:id/:title',
component: Detail
}
]
}
]
},
]
})

@ -1,7 +1,7 @@
<template> <template>
<ul> <ul>
<li>消息编号{{this.$route.query.id}}</li> <li>消息编号{{this.$route.params.id}}</li>
<li>消息标题{{this.$route.query.title}}</li> <li>消息标题{{this.$route.params.title}}</li>
</ul> </ul>
</template> </template>

@ -3,12 +3,12 @@
<ul> <ul>
<li v-for="m in messageList" :key="m.id"> <li v-for="m in messageList" :key="m.id">
<!-- to的普通写法--> <!-- to的普通写法-->
<!-- <router-link :to="`/home/message/detail/?id=${m.id}&title=${m.title}`">{{m.title}}</router-link>--> <!-- <router-link :to="`/home/message/detail/${m.id}/${m.title}`">{{ m.title }}</router-link>-->
<!-- to的对象写法--> <!-- to的对象写法-->
<router-link :to="{ <router-link :to="{
// path: '/home/message/detail', // path: '/home/message/detail',
name: 'xiangqing', name: 'xiangqing',
query: { params: {
id: m.id, id: m.id,
title: m.title title: m.title
} }

@ -29,7 +29,7 @@ export default new VueRouter({
children: [ children: [
{ {
name: 'xiangqing', name: 'xiangqing',
path: 'detail', path: 'detail/:id/:title',
component: Detail component: Detail
} }
] ]

Loading…
Cancel
Save