詳解vue2路由vue-router配置(懶加載)
vue路由配置以及按需加載模塊配置
1、首先在component文件目錄下寫(xiě)倆組件:
First.vue:
<template>
<div>我是第一個(gè)頁(yè)面</div>
</template>
<script>
export default {
name: 'first',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
Second.vue:
<template>
<div>我是第二個(gè)頁(yè)面</div>
</template>
<script>
export default {
name: 'second',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
2、router目錄下的index.js文件配置路由信息:
import Vue from 'vue'
import VueRouter from 'vue-router'
/*import First from '@/components/First'
import Second from '@/components/Second'*/
Vue.use(VueRouter)
/*const routes = [
//重定向
{
path:'/',
redirect:'first'
},
{
path: '/first',
name: 'First',
component: First
},
{
path: '/second',
name: 'Second',
component: Second
}
]*/
//懶加載路由
const routes = [
{ //當(dāng)首次進(jìn)入頁(yè)面時(shí),頁(yè)面沒(méi)有顯示任何組件;讓頁(yè)面一加載進(jìn)來(lái)就默認(rèn)顯示first頁(yè)面
path:'/', //重定向,就是給它重新指定一個(gè)方向,加載一個(gè)組件;
component:resolve => require(['@/components/First'],resolve)
},
{
path:'/first',
component:resolve => require(['@/components/First'],resolve)
},
{
path:'/second',
component: resolve => require(['@/components/Second'],resolve)
}
//這里require組件路徑根據(jù)自己的配置引入
]
//最后創(chuàng)建router 對(duì)路由進(jìn)行管理,它是由構(gòu)造函數(shù) new vueRouter() 創(chuàng)建,接受routes 參數(shù)。
const router = new VueRouter({
routes
})
export default router;
3、main.js中引入路由配置文件:
import $ from 'jquery'
import Vue from 'vue'
import App from './App'
import router from './router' //引入路由配置文件
import './assets/css/bootstrap.min.css'
import './assets/js/bootstrap.min'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router, // 注入到根實(shí)例中
render: h => h(App)
})
4、App.vue引入路由配置導(dǎo)航:
<template>
<router-link to="/first">跳轉(zhuǎn)第一個(gè)頁(yè)面</router-link>
<router-link to="/second">跳轉(zhuǎn)第二個(gè)頁(yè)面</router-link>
<div id="view">
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'app'
}
</script>
<style>
</style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
創(chuàng)建nuxt.js項(xiàng)目流程圖解
Nuxt.js是創(chuàng)建Universal Vue.js應(yīng)用程序的框架。它的主要范圍是UI渲染,同時(shí)抽象出客戶(hù)端/服務(wù)器分布。我們的目標(biāo)是創(chuàng)建一個(gè)足夠靈活的框架,以便您可以將其用作主項(xiàng)目庫(kù)或基于Node.js的當(dāng)前項(xiàng)目。2020-03-03
Vue+ElementUI前端添加展開(kāi)收起搜索框按鈕完整示例
最近一直在用element ui做后臺(tái)項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于Vue+ElementUI前端添加展開(kāi)收起搜索框按鈕的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
解決vue下載后臺(tái)傳過(guò)來(lái)的亂碼流的問(wèn)題
這篇文章主要介紹了解決vue下載后臺(tái)傳過(guò)來(lái)的亂碼流的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
vue 實(shí)現(xiàn)滾動(dòng)到底部翻頁(yè)效果(pc端)
這篇文章主要介紹了pc端vue 滾動(dòng)到底部翻頁(yè)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07
Element Carousel 走馬燈的具體實(shí)現(xiàn)
這篇文章主要介紹了Element Carousel 走馬燈的具體實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
webpack配置導(dǎo)致字體圖標(biāo)無(wú)法顯示的解決方法
下面小編就為大家分享一篇webpack配置導(dǎo)致字體圖標(biāo)無(wú)法顯示的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
vue實(shí)現(xiàn)自定義公共組件及提取公共的方法
這篇文章主要介紹了vue實(shí)現(xiàn)自定義公共組件及提取公共的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

