Day 23 - Vue Router参数路由

昨天我们提到了如何透过Vue-Router切换页面,今天我们来谈谈如何传递参数到路由上。

new Vue({
    router: new VueRouter({
        routes: [
            {path: "", component: HelloWorld},
            {path: "/About", component: About},
            {path: "/Detail/:id", component: Detail},
        ]
    }),

假设今天有三个页面要做切换,首页渲染HelloWorld.vue、/About渲染About.vue,Detail则必须要传入名为id的参数。

<!-- Detail.vue -->
<template>
	<div>
		<div>Detail</div>
		<div>id: {{id}}</div>
	</div>
</template>

<script>
    export default {
        name: "Detail",
        computed: {
            id() {
                return this.$route.params.id;
            }
        }
    }
</script>

我们在Route上,让path上加上 :id,并定义参数id是一个变数(名称可以自订),我们可以使用 this.$route.params.id 来取得 id 的值,另外,如果想取得的值来自QueryString,可以使用 this.$route.query.id。

不过这时候如果我们没从前端传入 id ,会发现并没有载入 Detail.vue 这个component的,因为Route上指定的 id 是一个必须的参数,可以在参数後面加上问号,让这个参数变成optional,这样即时没有 id,也可以找到Detail组件并载入显示。

{path: "/Detail/:id?", component: Detail}

<<:  Android Studio初学笔记-Day23-Banner

>>:  Day 24 - [Android APP] 02-界面设计

Ruby on Rails Controller

第 1 步 - 新增 Route 别忘了,使⽤者想要看到你网站上的内容,第⼀步是要问过 Route,...

Day 10 : Docker基本操作 Volume篇

Docker 资料保存 Docker内空间的资料能不能保存下来? 当我关闭一个Container後下...

[Day 28] banana in a box!关於双向系结功能的语法糖

Okay!最後几天要看的内容是关於 Angular 的双向系结。 如果你跟我一样是从 Angular...

Day22-React Life Cycle 篇-上篇(介绍生命周期图 & Mounting)

这篇要介绍的是 React 元件的生命周期(Life Cycle)。 要认识生命周期的话我们可以先从...

Day 5 情报收集 - Information Gathering (IDS/IPS Identification)

与正文无关的前言 昨天压死线撑过了第四天,发现浏览数目比前几天要多了一些,看来某些些关键字还是有比较...