1.路由
vue-router
vue的核心插件
vue-router.js
npm install vue
npm install vue-router
根据不同的url访问不同的页面
创建单页面SPA(SINGLE PAGE APPLICATION)应用
2.vue的一些指令
v-html
v-text
v-once 只绑定一次
v-pre 原样输出
v-cloak
案例:
<div id='app'>
<input type="text" v-model='msg'>
<p v-html='msg'>{{msg}}</p>
<h3 v-text='msg'>{{msg}}</h3>
<a href="" v-once>{{msg}}</a>
<h1 v-pre>{{msg}}</h1>
</div>
<script src='vue/dist/vue.js'></script>
<script>
new Vue({
el:'#app',
data:{
msg:'hello'
}
})
</script>
效果如下:
案例:一个简单的路由
<a></a>
<div id='app'>
<!--1.-->
<router-link to='/home'>首页</router-link>
<router-link to='/user'>用户页</router-link>
<!-- 盛放链接对应的内容-->
<router-view></router-view>
</div>
<script src='vue/dist/vue.js'></script>
<script src='vue-router/dist/vue-router.js'></script>
<script>
//2.创建组件
var Home={
template:`
<h1>这是首页</h1>
`
}
var User={
template:`
<h1>这是用户页</h1>
`
}
//3.配置路由
const routes=[
{path:'/home',component:Home},
{path:'/user',component:User}
]
//4.创建路由实例
const router=new VueRouter({
routes:routes
})
//5.路由实例挂载到vue实例上
new Vue({
el:'#app',
router:router
})
</script>
效果如下:
案例:路由的嵌套
<div id='app'>
<!--1.-->
<router-link to='/index'>首页</router-link>
<router-link to='/user'>用户页</router-link>
<router-view></router-view>
</div>
<script src='vue/dist/vue.js'></script>
<script src='vue-router/dist/vue-router.js'></script>
<script>
//2.创建组件
var Index={
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:Index},
{path:'/index',component:Index},
{
path:'/user',
component:User,
children:[
{path:'regist',component:Regist},
{path:'login',component:Login}
]
}
]
//4.创建路由实例
const router=new VueRouter({
routes:routes
})
//5.把路由实例挂载到vue实例上
new Vue({
el:'#app',
router:router//注册路由
})
</script>
效果如下: