vue router-link,Vue-router路由使用,單頁面的實現

 2023-10-21 阅读 24 评论 0

摘要:1.安裝路由系統 NPM npm install vue-router 2.在main.js中進入引用 import VueRouter from 'vue-router' 3.創建三個空的組件: Vcourse.vue <template> <div class="course">課程 </div> </template><script>expor

1.安裝路由系統

NPM
npm install vue-router

2.在main.js中進入引用

import VueRouter from 'vue-router'

3.創建三個空的組件:

Vcourse.vue

<template>
<div class="course">課程
</div>
</template><script>export default {name: "vcourse",data(){return {}}}
</script><style scoped></style>
View Code

vue router-link,Vmain.vue

<template>
<div class="course">課程
</div>
</template><script>export default {name: "vcourse",data(){return {}}}
</script><style scoped></style>
View Code

Vmarked.vue

<template>
<div class="course">課程
</div>
</template><script>export default {name: "vcourse",data(){return {}}}
</script><style scoped></style>
View Code

4.在main.js中引入這三個組件

//引入的三個組件
//定義路由組件。可以從其他文件 import過來
import Vmain from './components/Vmain'
import Vcourse from './components/Vcourse'
import Vmarked from './components/Vmarked'

5.使用vuerouter.use()方法

Vue.use(VueRouter);

6.定義我們的路由對象:

//定義我們的路由對象,每一個路由映射一個組件
const router = new VueRouter({mode:'history',routes:[{path:'/',component:Vmain},{path:'/course',component:Vcourse},{path:'/mark',component:Vmarked}]
});

7.掛載

//4 創建和掛載實例
new Vue({el: '#app',router,render: h => h(App)
});

8.渲染

 <div class="app"><link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"><ul><li><router-link to="/">首頁</router-link></li></ul><ul><li><router-link to="/course">課程</router-link><button class="btn-success">88888</button></li></ul><ul><li><router-link to="/mark">編輯器</router-link></li></ul><!--路由出口--><!--路由匹配到的組件將渲染在這里--><router-view></router-view></div>

這里把router-link渲染成了a標簽,to渲染成了href.

這樣一個單頁面應用就出來了。

效果就是點擊三個a標簽,分別加載不同的組件,而頁面不會刷新,路由在更新。

?

?那個888,是在最后測試了一下bootstrap的 button,看bootstrap導入了沒有。

最后附上所有的全部代碼:

app.vue

<!-- 一個組件由三部分組成 -->
<template><!-- 頁面的結構 --><div class="app"><link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"><ul><li><router-link to="/">首頁</router-link></li></ul><ul><li><router-link to="/course">課程</router-link><button class="btn-success">88888</button></li></ul><ul><li><router-link to="/mark">編輯器</router-link></li></ul><!--路由出口--><!--路由匹配到的組件將渲染在這里--><router-view></router-view></div>
</template><script>//頁面的業務邏輯
  export default {name: 'app',data() {       //data必須是一個函數return {    //必須return。
        msg:"hello"}},}
</script><style scoped>*{padding: 0;margin: 0;}
</style>
View Code

main.js

import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'//引入的三個組件//定義路由組件。可以從其他文件 import過來import Vmain from './components/Vmain'import Vcourse from './components/Vcourse'import Vmarked from './components/Vmarked'Vue.use(VueRouter);//定義我們的路由對象,每一個路由映射一個組件
const router = new VueRouter({mode:'history',routes:[{path:'/',component:Vmain},{path:'/course',component:Vcourse},{path:'/mark',component:Vmarked}]
});//4 創建和掛載實例
new Vue({el: '#app',router,render: h => h(App)
});
View Code

三個組件代碼在上面已有。其實就是三個空模板。

?

轉載于:https://www.cnblogs.com/geogre123/p/9776823.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/154852.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息