有时我们通过一个名称来标识一个路由显得更方便一些,特别是在链接一个路由,或者是执行一些跳转的时候。你可以在创建 Router 实例的时候,在 routes 配置中给某个路由设置名称。
我们直接在路由下添加一个 name 即可.
注意,如果使用命名路由route-link的to属性前面有有个冒号
<!DOCTYPE html>
<!-- saved from url=(0077)https://keepfool.github.io/vue-tutorials/06.Router/basic/basic_01.html#!/home -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>abc</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="./basic_01_files/custom.css">
</head>
<body>
<div id="app">
<div class="row">
<div class="col-xs-offset-2 col-xs-8">
<div class="page-header">
<h2>Router Basic - 01</h2>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-2 col-xs-offset-2">
<div class="list-group">
<router-link class="list-group-item" :to="{ name: 'home'}">Go to Foo</router-link>
<router-link class="list-group-item" to="/about">Go to Bar</router-link>
</div>
</div>
<router-view></router-view>
</div>
</div>
<template id="home">
<div>
<h1>Home</h1>
<p>{{msg}}</p>
</div>
</template>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
<script src="https://cdn.bootcss.com/vue-router/2.7.0/vue-router.min.js"></script>
<script>
var todoItem = Vue.extend({
data: function() {
return {
todoData: [
{ id: 0, text: '蔬菜' },
{ id: 1, text: '奶酪' },
{ id: 2, text: '随便其它什么人吃的东西' }
]
};
},
template: `
<ul>
<li v-for='(d, i) in todoData' :key="i">
{{ d.text }}
</li>
</ul>
`,
});
var t_test = Vue.extend({
data:function(){
return {
msg:"hello,test"
};
},
template:"#home"
}
);
// Home = { template: '<div>foo</div>' }
// About = { template: '<div>bar</div>' }
routes = [
{ path: '/home',name:"home",component: t_test },
{ path: '/about', component: todoItem }
]
router = new VueRouter({
routes: routes // (缩写)相当于 routes: routes
});
app = new Vue({
router: router
}).$mount('#app');
</script>
</body>
</html>
- 命名动态路由:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>abc</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<h1>Hello App!</h1>
<p>
<!-- 使用 router-link 组件来导航. -->
<!-- 通过传入 `to` 属性指定链接. -->
<!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
<router-link :to="{ name: 'id',params:{id:1}}">foo1</router-link>
<router-link to="/foo/2">foo2</router-link>
</p>
<!-- 路由出口 -->
<!-- 路由匹配到的组件将渲染在这里 -->
<router-view></router-view>
</div>
</body>
<script type="text/javascript">
// 0. 如果使用模块化机制编程,导入 Vue 和 VueRouter,要调用 Vue.use(VueRouter)
// 1. 定义(路由)组件。
// 可以从其他文件 import 进来
const Foo = { template: '<div>foo:{{ $route.params.id }}</div>' }
// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
{ path: '/foo/:id', name:"id",component: Foo }
]
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
routes // (缩写)相当于 routes: routes
})
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
router // (缩写)相当于 router: router
}).$mount('#app')
</script>
</html>
- 多参数:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>abc</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<h1>Hello App!</h1>
<p>
<!-- 使用 router-link 组件来导航. -->
<!-- 通过传入 `to` 属性指定链接. -->
<!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
<router-link :to="{name:'ids',params:{id1:1,id2:11}}">foo1</router-link>
<router-link to="/foo/2/22">foo2</router-link>
</p>
<!-- 路由出口 -->
<!-- 路由匹配到的组件将渲染在这里 -->
<router-view></router-view>
</div>
</body>
<script type="text/javascript">
// 0. 如果使用模块化机制编程,导入 Vue 和 VueRouter,要调用 Vue.use(VueRouter)
// 1. 定义(路由)组件。
// 可以从其他文件 import 进来
// const Foo = { template: '<div>foo:{{ $route.params.id }}</div>' }
const Foo = Vue.extend({
template: '<div id="testzy"></div>',
methods: {
change() {
//alert("come");
document.getElementById("testzy").innerText = this.$route.params.id1+","+this.$route.params.id2;
//alert("fd");
// if (this.$route.params.id == 1) {
// document.getElementById("testzy").innerText = "hehe";
// } else {
// document.getElementById("testzy").innerText = "haha";
// }
},
},
mounted(){
document.getElementById("testzy").innerText = this.$route.params.id1+","+this.$route.params.id2;
},
watch:{$route:"change"}
});
// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
{ path: '/foo/:id1/:id2',name:"ids" ,component: Foo }
]
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
routes // (缩写)相当于 routes: routes
})
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
router, // (缩写)相当于 router: router
}).$mount('#app')
</script>
</html>
- 动态+命名+push
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>abc</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id="app">
<h1>Hello App!</h1>
<!-- 路由出口 -->
<!-- 路由匹配到的组件将渲染在这里 -->
<router-view>
</router-view>
</div>
</body>
<script type="text/javascript">
const test = Vue.extend({
template: '<a @click="pushtest" href="javascript:void(0)">Navigate to {{$route.params.name}}</a>',
methods: {
pushtest() {
//alert("bar");
if (this.$route.params.name == "bar") {
this.$router.push({ name: 'test',params:{name:"foo"} });
}else{
this.$router.push({ name: 'test',params:{name:"bar"} });
}
//alert("fdas");
},
},
});
// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
{ path: '/', redirect: "/test/bar" },
// { path: '/foo', name: "foo", component: Foo },
{ path: '/test/:name', name: "test", component: test },
]
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
routes // (缩写)相当于 routes: routes
})
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
router, // (缩写)相当于 router: router
}).$mount('#app') // 现在,应用已经启动了!
</script>
</html>
- 如果要带query参数,可以这样
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>abc</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id="app">
<h1>Hello App!</h1>
<!-- 路由出口 -->
<!-- 路由匹配到的组件将渲染在这里 -->
<router-view>
</router-view>
</div>
</body>
<script type="text/javascript">
const test = Vue.extend({
template: '<a @click="pushtest" href="javascript:void(0)">Navigate to {{$route.params.name}}</a>',
methods: {
pushtest() {
//alert("bar");
if (this.$route.params.name == "bar") {
this.$router.push({ name: 'test',params:{name:"foo"} ,query:{ param: 'foo' }});
}else{
this.$router.push({ name: 'test',params:{name:"bar"} ,query:{ param: 'bar' }});
}
//alert("fdas");
},
},
});
// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
{ path: '/', redirect: "/test/bar" },
// { path: '/foo', name: "foo", component: Foo },
{ path: '/test/:name', name: "test", component: test },
]
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
routes // (缩写)相当于 routes: routes
})
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
router, // (缩写)相当于 router: router
}).$mount('#app') // 现在,应用已经启动了!
</script>
</html>
网址是类似这样的:
xxxxx.html#/test/foo?param=foo
网友评论