Vue.js路由實現(xiàn)選項卡簡單實例
本文實例為大家分享了Vue.js路由實現(xiàn)選項卡的具體代碼,供大家參考,具體內(nèi)容如下
需要實現(xiàn)下圖效果,點擊上方選項卡,切換到不同內(nèi)容的組件:

事先準備好兩個庫文件(vue.js、vue-router.js),放到對應路徑。
1.引入依賴庫
<script src="vue.js" type="text/javascript" charset="GBK"></script> <script src="vue-router.js" type="text/javascript" charset="GBK"></script>
2.組件創(chuàng)建
const Html = Vue.extend({
template: '<div><h1>html</h1><p>{{msg}}</p></div>',
data: function() {
return {
msg: 'This is the html vue-router.'
}
}
});
const Css = Vue.extend({
template: '<div><h1>CSS</h1><p>{{msg}}</p></div>',
data(){
return{
msg: 'This is the CSS vue-router.'
}
}
});
const Vue1 = Vue.extend({
template: '<div><h1>Vue</h1><p>{{msg}}</p></div>',
data(){
return{
msg: 'This is the Vue vue-router.'
}
}
});
const Javascript = Vue.extend({
template: '<div><h1>Javascript</h1><p>{{msg}}</p></div>',
data(){
return{
msg: 'This is the Javascript vue-router.'
}
}
});
3.路由創(chuàng)建與映射
const router = new VueRouter({
routes: [
{ path: '/html', component: Html },
{ path: '/css', component: Css },
{ path: '/vue', component: Vue1 },
{ path: '/javascript', component: Javascript },
{ path: '/', component: Html } //默認路徑
]
});
4.掛在實例
const vm = new Vue({
router: router
}).$mount('#app');
5.頁面<router-link>,to指令跳轉(zhuǎn)到指定路徑
<div id="app">
<div class="app-tit">
<router-link to="/html">html</router-link>
<router-link to="/css">css</router-link>
<router-link to="/vue">vue</router-link>
<router-link to="/javascript">javascript</router-link>
</div>
<div class="app-con">
<router-view></router-view>
</div>
</div>
6.所用樣式
<style>
body{
font-family:"Microsoft YaHei";
}
#app{
width: 600px;
margin: 0 auto;
}
.app-tit{
font-size: 0;
width: 600px;
}
.app-tit .router-link-active {
background: #09f;
color: #fff;
}
.app-tit a{
display: inline-block;
height: 40px;
line-height: 40px;
font-size: 16px;
width: 25%;
text-align: center;
background: #ccc;
color: #333;
text-decoration: none;
}
.app-con div{
border: 1px solid #ccc;
height: 400px;
padding-top: 20px;
}
</style>
完整代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="GBK">
<title>hello world</title>
<script src="vue.js" type="text/javascript" charset="GBK"></script>
<script src="vue-router.js" type="text/javascript" charset="GBK"></script>
</head>
<style>
body{
font-family:"Microsoft YaHei";
}
#app{
width: 600px;
margin: 0 auto;
}
.app-tit{
font-size: 0;
width: 600px;
}
.app-tit .router-link-active {
background: #09f;
color: #fff;
}
.app-tit a{
display: inline-block;
height: 40px;
line-height: 40px;
font-size: 16px;
width: 25%;
text-align: center;
background: #ccc;
color: #333;
text-decoration: none;
}
.app-con div{
border: 1px solid #ccc;
height: 400px;
padding-top: 20px;
}
</style>
<body>
<div id="app">
<div class="app-tit">
<router-link to="/html">html</router-link>
<router-link to="/css">css</router-link>
<router-link to="/vue">vue</router-link>
<router-link to="/javascript">javascript</router-link>
</div>
<div class="app-con">
<router-view></router-view>
</div>
</div>
<script type="text/javascript">
const Html = Vue.extend({
template: '<div><h1>html</h1><p>{{msg}}</p></div>',
data: function() {
return {
msg: 'This is the html vue-router.'
}
}
});
const Css = Vue.extend({
template: '<div><h1>CSS</h1><p>{{msg}}</p></div>',
data(){
return{
msg: 'This is the CSS vue-router.'
}
}
});
const Vue1 = Vue.extend({
template: '<div><h1>Vue</h1><p>{{msg}}</p></div>',
data(){
return{
msg: 'This is the Vue vue-router.'
}
}
});
const Javascript = Vue.extend({
template: '<div><h1>Javascript</h1><p>{{msg}}</p></div>',
data(){
return{
msg: 'This is the Javascript vue-router.'
}
}
});
const router = new VueRouter({
routes: [
{ path: '/html', component: Html },
{ path: '/css', component: Css },
{ path: '/vue', component: Vue1 },
{ path: '/javascript', component: Javascript },
{ path: '/', component: Html } //默認路徑
]
});
const vm = new Vue({
router: router
}).$mount('#app');
</script>
</body>
</html>
如有錯誤之處,歡迎指出,萬分感謝!
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue+elementUI組件table實現(xiàn)前端分頁功能
這篇文章主要為大家詳細介紹了vue+elementUI組件table實現(xiàn)前端分頁功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12
Vue中使用video.js實現(xiàn)截圖和視頻錄制與下載
這篇文章主要為大家詳細介紹了Vue中如何使用video.js實現(xiàn)截圖和視頻錄制與下載,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-03-03
關(guān)于vue的npm run dev和npm run build的區(qū)別介紹
這篇文章主要介紹了關(guān)于vue的npm run dev和npm run build的區(qū)別介紹,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01

