一 :路由
路由:vue-router
是Vue的工具库 vue-router.js
下载:
npm install vue 下载vue
npm install vue-router 下载vue-router
通过不同的url访问不同的页面
spa(single page application) 单页面应用
路由:vue-router
vue的插件
下载:
npm install vue-router
作用:
根据不同的url访问不同的页面
创建单页面应用 SPA(single page application)
例:
<div id='app'>
<router-link to='/home'>首页</router-link>
<router-link to='/detail'>详情页</router-link>
<!-- 盛放每个页面对应的内容-->
<router-view></router-view>
</div>
<script src='js/vue.js'></script>
<script src='js/vue-router.js'></script>
<script>
//2.创建组件
var Home={
template:`
<h1>这是首页</h1>
`
}
var Detail={
template:`
<h1>这是详情页</h1>
`
}
//3.配置路由
const routes=[
{path:'/home',component:Home},
{path:'/detail',component:Detail}
]
//4.创建一个路由实例
const router=new VueRouter({
routes:routes
})
//把路由挂在到vue实例上
new Vue({
el:'#app',
router:router
})
</script>
路由的嵌套:
<div id='app'>
<router-link to='/home'>首页</router-link>
<router-link to='/user'>用户页</router-link>
<router-view></router-view>
</div>
<script src='js/vue.js'></script>
<script src='js/vue-router.js'></script>
<script>
//2.
var Home={
template:`<h1>这是首页</h1>`
}
var User={
template:`
<div>
<h1>这是用户页</h1>
<ul>
<li>
<router-link to='/user/regist'>注册</router-link>
</li>
<li>
<router-link to='/user/login'>登录</router-link>
</li>
</ul>
<router-view></router-view>
</div>
`
}
var Regist={
template:`<h3>这是注册页面</h3>`
}
var Login={
template:`<h3>这是登录页面</h3>`
}
//3.
const routes=[
{path:'/',component:Home},
{path:'/home',component:Home},
{
path:'/user',
component:User,
children:[
{path:'regist',component:Regist},
{path:'login',component:Login}
]
}
]
//4.
const router=new VueRouter({
routes:routes
})
//5.
new Vue({
el:'#app',
router:router
})
</script>
路由的传参:
1.查询字符串
/user/regist?name=jack&age=18
接收:{{route.params}}
<div id='app'>
<router-link to='/home'>首页</router-link>
<router-link to='/user'>用户页</router-link>
<router-view></router-view>
</div>
<script src='js/vue.js'></script>
<script src='js/vue-router.js'></script>
<script>
//2.
var Home={
template:`<h1>这是首页</h1>`
}
var User={
template:`
<div>
<h1>这是用户页</h1>
<ul>
<li>
<router-link to='/user/regist?uname=jack&&age=18'>注册</router-link>
</li>
<li>
<router-link to='/user/login/alice/20'>登录</router-link>
</li>
</ul>
<router-view></router-view>
</div>
`
}
var Regist={
template:`
<div>
<h3>这是注册页面</h3>
<a href=''>{{$route.query}}</a>
<ul>
<li>{{$route.query.uname}}</li>
<li>{{$route.query.age}}</li>
</ul>
</div>
`
}
var Login={
template:`
<div>
<h3>这是登录页面</h3>
<p>{{$route.params}}</p>
<ul>
<li>{{$route.params.userName}}</li>
<li>{{$route.params.userage}}</li>
</ul>
</div>
`
}
//3.
const routes=[
{path:'/',component:Home},
{path:'/home',component:Home},
{
path:'/user',
component:User,
children:[
{path:'regist',component:Regist},
{path:'login/:userName/:userage',component:Login}
]
}
]
//4.
const router=new VueRouter({
routes:routes
})
//5.
new Vue({
el:'#app',
router:router
})
</script>
二。 生命周期
<div id='app'>{{msg}}</div>
<script src='js/vue.js'></script>
<script>
new Vue({
el:'#app',
data:{
msg:'hello vue'
},
beforeCreate:function(){
alert('beforeCreate')
},
created:function(){
alert('Created')
},
beforeMount:function(){
alert('befroeMounted')
},
mounted:function(){
alert('mounted')
}
})
</script>