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

事先準(zhǔn)備好兩個(gè)庫(kù)文件(vue.js、vue-router.js),放到對(duì)應(yīng)路徑。
1.引入依賴庫(kù)
<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 } //默認(rèn)路徑
]
});
4.掛在實(shí)例
const vm = new Vue({
router: router
}).$mount('#app');
5.頁(yè)面<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 } //默認(rèn)路徑
]
});
const vm = new Vue({
router: router
}).$mount('#app');
</script>
</body>
</html>
如有錯(cuò)誤之處,歡迎指出,萬(wàn)分感謝!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于vue-resource jsonp跨域問(wèn)題的解決方法
下面小編就為大家分享一篇基于vue-resource jsonp跨域問(wèn)題的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
vue結(jié)合vant實(shí)現(xiàn)聯(lián)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了vue結(jié)合vant實(shí)現(xiàn)聯(lián)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Vue vxe-table使用問(wèn)題收錄小結(jié)
這篇文章主要為大家介紹了Vue vxe-table使用問(wèn)題收錄小結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Vue零基礎(chǔ)入門(mén)之模板語(yǔ)法與數(shù)據(jù)綁定及Object.defineProperty方法詳解
這篇文章主要介紹了Vue初學(xué)基礎(chǔ)中的模板語(yǔ)法、數(shù)據(jù)綁定、Object.defineProperty方法等基礎(chǔ),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09
vue 組件使用中的一些細(xì)節(jié)點(diǎn)
這篇文章主要介紹了vue 組件使用中的一些細(xì)節(jié)點(diǎn),大概有兩大細(xì)節(jié)點(diǎn),本文通過(guò)基礎(chǔ)實(shí)例給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-04-04
Vue3配置vite.config.js解決跨域問(wèn)題的方法
跨域一般出現(xiàn)在開(kāi)發(fā)階段,由于線上環(huán)境前端代碼被打包成了靜態(tài)資源,因而不會(huì)出現(xiàn)跨域問(wèn)題,這篇文章主要給大家介紹了關(guān)于Vue3配置vite.config.js解決跨域問(wèn)題的相關(guān)資料,需要的朋友可以參考下2024-07-07
vue清除動(dòng)態(tài)路由的問(wèn)題記錄
項(xiàng)目中往往都是添加動(dòng)態(tài)路由,如何刪除已經(jīng)添加進(jìn)來(lái)的路由往往被忽視,為此這里做一下記錄,感興趣的朋友跟隨小編一起看看吧2023-10-10
Vite配置路徑別名的簡(jiǎn)單實(shí)現(xiàn)方法
Vite項(xiàng)目中我們可以手動(dòng)將src路徑設(shè)置**@**路徑別名,可以省下很多引入路徑的冗余路徑,下面這篇文章主要給大家介紹了關(guān)于Vite配置路徑別名的簡(jiǎn)單實(shí)現(xiàn)方法,需要的朋友可以參考下2023-04-04
vue頁(yè)面使用js實(shí)現(xiàn)前端打印功能
這篇文章主要介紹了vue頁(yè)面使用js實(shí)現(xiàn)前端打印功能,文中通過(guò)圖文結(jié)合的方式給大家講解的非常詳細(xì),對(duì)大家實(shí)現(xiàn)打印功能有一定的幫助,需要的朋友可以參考下2024-05-05

