Vue极简入门
1.注册路由,如果是子路由,就加一个children
import Vue from 'vue'
import Router from 'vue-router'
import Main from '../views/Main.vue'
import Login from '../views/Login.vue'import UserProfile from "../views/user/Profile.vue"
import UserList from "../views/user/List.vue"Vue.use(Router)export default new Router({routes: [{path: '/login',name: 'login',component: Login},{path: '/main',name: 'main',component: Main,children:[{path:'/user/profile',component:UserProfile},{path:'/user/list',component:UserList},]}]
})
2.使用方法
显示的页面
显示的位置
<template><div><h1>Main</h1><router-link to="/user/profile">个人信息</router-link><router-link to="/user/list">用户列表</router-link><router-view></router-view></div>
</template><script>export default{name:"Main"}
</script><style scoped></style>
3.name 传组件名 params 传递参数 需要对象 v-bind 而后path接收数据
<template><div><h1>Main</h1><router-link :to="{name:'UserProfile',params:{id:1}}">个人信息</router-link><router-link to="/user/list">用户列表</router-link><router-view></router-view></div>
</template><script>export default{name:"Main"}
</script><style scoped></style>
import Vue from 'vue'
import Router from 'vue-router'
import Main from '../views/Main.vue'
import Login from '../views/Login.vue'import UserProfile from "../views/user/Profile.vue"
import UserList from "../views/user/List.vue"Vue.use(Router)export default new Router({routes: [{path: '/login',name: 'login',component: Login},{path: '/main',name: 'main',component: Main,children:[{path:'/user/profile/:id',name:'UserProfile',component:UserProfile},{path:'/user/list',component:UserList},]}]
})
3.1展示 {{ $route.params.id }}
<template><div><h1>个人信息</h1>{{ $route.params.id }}</div></template><script>
export default{name:"UserProfile"
}
</script><style scoped></style>