- <div id="app">
- <h1>Hello App!</h1>
- <p>
-
-
-
- <router-link to="/foo">Go to Foo</router-link>
- <router-link to="/bar">Go to Bar</router-link>
- </p>
-
-
- <router-view></router-view>
- </div>
-
- <template id=‘foo‘>
- <p>this is foo!</p>
- </template>
- <template id=‘bar‘>
- <p>this is bar!</p>
- </template>
- const Foo = { template:‘#foo‘ };
- const Bar = { template:‘#bar‘ };
- const routes = [
- { path: ‘/foo‘, component: Foo },
- { path: ‘/bar‘, component: Bar }
- ];
- const router = new VueRouter({ routes:routes });
- const app = new Vue({ router:router }).$mount(‘#app‘);
2)动态路由匹配:
- <div id="app">
- <h1>Hello App!</h1>
- <p>
- <router-link to="/user/foo/post/123">Go to Foo</router-link>
- <router-link to="/user/bar/post/456">Go to Bar</router-link>
- </p>
- <router-view></router-view>
- </div>
-
- <template id=‘user‘>
- <p>User:{{ $route.params.id }},Post:{{$route.params.post_id}}</p>
- </template>
- const User = {
- template:‘#user‘,
- watch:{
- ‘$route‘(to,from){
- console.log(‘从‘+from.params.id+‘到‘+to.params.id);
- }
- }
- };
- const router = new VueRouter({
- routes:[
- { path:‘/user/:id/post/:post_id‘,component:User }
- ]
- });
- const app = new Vue({ router:router }).$mount(‘#app‘);
3)嵌套路由:
- <div id="app">
- <h1>Hello App!</h1>
- <p>
- <router-link to="/user/foo">Go to Foo</router-link>
- <router-link to="/user/foo/profile">Go to profile</router-link>
- <router-link to="/user/foo/posts">Go to posts</router-link>
- </p>
- <router-view></router-view>
- </div>
-
- <template id=‘user‘>
- <div>
- <h2>User:{{ $route.params.id }}</h2>
- <router-view></router-view>
- </div>
- </template>
-
- <template id="userHome">
- <p>主页</p>
- </template>
-
- <template id="userProfile">
- <p>概况</p>
- </template>
-
- <template id="userPosts">
- <p>登录信息</p>
- </template>
- const User = {
- template:‘#user‘
- };
- const UserHome = {
- template:‘#userHome‘
- };
- const UserProfile = {
- template:‘#userProfile‘
- };
- const UserPosts = {
- template:‘#userPosts‘
- };
- const router = new VueRouter({
- routes:[
- { path:‘/user/:id‘, component:User,
- children:[
-
-
- { path: ‘‘, component: UserHome},
-
-
- { path:‘profile‘, component:UserProfile },
-
-
- { path: ‘posts‘, component: UserPosts }
- ]
- }
- ]
- });
- const app = new Vue({ router:router }).$mount(‘#app‘);
4)编程式路由:
- <div id="app">
- <h1>Hello App!</h1>
- <p>
- <router-link to="/user/foo">Go to Foo</router-link>
- </p>
- <router-view></router-view>
- </div>
-
- <template id=‘user‘>
- <h2>User:{{ $route.params.id }}</h2>
- </template>
-
- <template id="register">
- <p>注册</p>
- </template>
- const User = {
- template:‘#user‘
- };
- const Register = {
- template:‘#register‘
- };
- const router = new VueRouter({
- routes:[
- { path:‘/user/:id‘, component:User },
- { path:‘/register‘, component:Register }
- ]
- });
- const app = new Vue({ router:router }).$mount(‘#app‘);
-
- router.push({ path: ‘register‘, query: { plan: ‘private‘ }});
5)命名路由:
- <div id="app">
- <h1>Named Routes</h1>
- <p>Current route name: {{ $route.name }}</p>
- <ul>
- <li><router-link :to="{ name: ‘home‘ }">home</router-link></li>
- <li><router-link :to="{ name: ‘foo‘ }">foo</router-link></li>
- <li><router-link :to="{ name: ‘bar‘, params: { id: 123 }}">bar</router-link></li>
- </ul>
- <router-view class="view"></router-view>
- </div>
-
- <template id=‘home‘>
- <div>This is Home</div>
- </template>
-
- <template id=‘foo‘>
- <div>This is Foo</div>
- </template>
-
- <template id=‘bar‘>
- <div>This is Bar {{ $route.params.id }}</div>
- </template>
- const Home = { template: ‘#home‘ };
- const Foo = { template: ‘#foo‘ };
- const Bar = { template: ‘#bar‘ };
-
- const router = new VueRouter({
- routes: [
- { path: ‘/‘, name: ‘home‘, component: Home },
- { path: ‘/foo‘, name: ‘foo‘, component: Foo },
- { path: ‘/bar/:id‘, name: ‘bar‘, component: Bar }
- ]
- });
-
- new Vue({ router:router }).$mount(‘#app‘);
6)命名视图:
- <div id="app">
- <router-link to="/">Go to Foo</router-link>
- <router-view class="view one"></router-view>
- <router-view class="view two" name="a"></router-view>
- <router-view class="view three" name="b"></router-view>
- </div>
-
- <template id=‘foo‘>
- <div>This is Foo</div>
- </template>
-
- <template id=‘bar‘>
- <div>This is Bar {{ $route.params.id }}</div>
- </template>
-
- <template id=‘baz‘>
- <div>This is baz</div>
- </template>
- const Foo = { template: ‘#foo‘ };
- const Bar = { template: ‘#bar‘ };
- const Baz = { template: ‘#baz‘ };
-
- const router = new VueRouter({
- routes: [
- {
- path: ‘/‘,
- components: {
- default:Foo,
- a:Bar,
- b:Baz
- }
- }
- ]
- });
-
- new Vue({ router:router }).$mount(‘#app‘);
vue router 几种方式对比 (转载)
原文:http://www.cnblogs.com/daiwenru/p/7736480.html