首页 / VUE / Vue-Router-嵌套路由
Vue-Router-嵌套路由
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Vue-Router-嵌套路由,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3069字,纯文字阅读大概需要5分钟。
内容图文
嵌套路由
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="http://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<title>Vue-Router</title>
</head>
<body>
<div id="box">
<p>
<router-link to="/home/888">home888</router-link> |
<router-link to="/home/666">home666</router-link> |
<router-link to="/news/zs/user/zs123">news(zs123)</router-link>
<router-link to="/news/ls/user/ls456">news(ls456)</router-link>
</p>
<router-view></router-view>
</div>
<!-- 模板抽离 -->
<template id="login">
<div class="login">
{{msg}}
</div>
</template>
<template id="home">
<div class="home">
<h2>{{msg}}</h2>
<p>{{$route.params.id}}</p>
<router-link to='/home/888/about'>about</router-link>
<router-link to='/home/999/archives'>archives</router-link>
<router-view></router-view>
</div>
</template>
<template id="empty">
<div class="empty">
<h2>{{msg}}</h2>
</div>
</template>
<template id="about">
<div class="about">
<h2>{{msg}}</h2>
</div>
</template>
<template id="archives">
<div class="about">
<h2>{{msg}}</h2>
</div>
</template>
<template id="news">
<div class="news">
<h2>{{msg}}</h2>
<p>{{$route.params.userName}}-{{$route.params.userPass}}</p>
</div>
</template>
<template id="com404">
<div class="com404">
{{msg}}
</div>
</template>
<script type="text/javascript">
// 1.定义路由组件
const Login = {
data() {
return {
msg: "这里是login组件"
}
},
template: "#login"
};
const Home = {
data() {
return {
msg: "这里是home组件"
}
},
template: "#home"
};
const Empty = {
data() {
return {
msg: '这里是空组件'
}
},
template: "#empty"
}
const About = {
data() {
return {
msg: '这里是关于我们组件'
}
},
template: "#about"
}
const Archives = {
data() {
return {
msg: '这里是档案组件'
}
},
template: "#archives"
}
const News = {
data() {
return {
msg: "这里是news组件"
}
},
template: "#news"
};
const Com404 = {
data() {
return {
msg: "这里是com404组件"
}
},
template: "#com404"
};
// 2.定义路由
const routes = [
{//匹配根路径
path: '/',
name: 'Login',
component: Login,
},
{//动态路由匹配 匹配一个动态参数
path: '/home/:id',
name: 'Home',
component: Home,
children: [
{
// 当/home/:id/任何路由;home的<router-view>中不会渲染任何东西
// 如果像渲染一些东西,就提供一个空的子路由
// 当 /home/:id 匹配成功,
// Empty组件 会被渲染在 Home 的 <router-view> 中
path: '',
name: 'Empty',
component: Empty,
},
{
// 要注意,以 / 开头的嵌套路径会被当作根路径;所以,这里面无需添加/
// 添加了/就相当于在根路径下,就不存在路由嵌套了
// 当 /home/:id/about 匹配成功,
// About组件 会被渲染在 home 的 <router-view> 中
path: 'about',
name: 'About',
component: About,
},
{
path: 'archives',
name: 'Archives',
component: Archives,
}
]
},
{//动态路由匹配 匹配多个动态参数
path: '/news/:userName/user/:userPass',
name: 'News',
component: News,
},
{//捕获所有路由或 404 Not found 路由
path: '*',
name: 'Com404',
component: Com404,
}
];
// 3. 创建 router 实例,然后传 `routes` 配置
const router = new VueRouter({
// mode: 'history',
routes // (缩写)相当于 routes: routes
});
// 4. 创建和挂载根实例。
const app = new Vue({
router
}).$mount('#box')
</script>
</body>
</html>
内容总结
以上是互联网集市为您收集整理的Vue-Router-嵌套路由全部内容,希望文章能够帮你解决Vue-Router-嵌套路由所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。